tty-option 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/README.md +1200 -488
- data/lib/tty/option/conversions.rb +27 -17
- data/lib/tty/option/deep_dup.rb +43 -19
- data/lib/tty/option/errors.rb +33 -3
- data/lib/tty/option/formatter.rb +364 -69
- data/lib/tty/option/param_permitted.rb +7 -3
- data/lib/tty/option/param_validation.rb +124 -20
- data/lib/tty/option/parameter.rb +2 -1
- data/lib/tty/option/parser/options.rb +6 -1
- data/lib/tty/option/version.rb +1 -1
- metadata +3 -2
data/README.md
CHANGED
@@ -9,34 +9,35 @@
|
|
9
9
|
[![Build status](https://ci.appveyor.com/api/projects/status/gxml9ttyvgpeasy5?svg=true)][appveyor]
|
10
10
|
[![Maintainability](https://api.codeclimate.com/v1/badges/1083a2fd114d6faf5d65/maintainability)][codeclimate]
|
11
11
|
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-option/badge.svg)][coverage]
|
12
|
-
[![Inline docs](https://inch-ci.org/github/piotrmurach/tty-option.svg?branch=master)][inchpages]
|
13
12
|
|
14
13
|
[gem]: https://badge.fury.io/rb/tty-option
|
15
14
|
[gh_actions_ci]: https://github.com/piotrmurach/tty-option/actions?query=workflow%3ACI
|
16
15
|
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-option
|
17
16
|
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-option/maintainability
|
18
17
|
[coverage]: https://coveralls.io/github/piotrmurach/tty-option
|
19
|
-
[inchpages]: https://inch-ci.org/github/piotrmurach/tty-option
|
20
18
|
|
21
19
|
> Parser for command line arguments, keywords, options and environment variables
|
22
20
|
|
23
21
|
## Features
|
24
22
|
|
25
|
-
*
|
26
|
-
|
27
|
-
*
|
28
|
-
*
|
29
|
-
*
|
30
|
-
*
|
31
|
-
*
|
32
|
-
*
|
23
|
+
* Parse command line **arguments**, **keywords**, **flags**, **options**
|
24
|
+
and **environment variables**.
|
25
|
+
* Define command line parameters with **DSL** or **keyword arguments**.
|
26
|
+
* Access all parameter values from hash-like **params**.
|
27
|
+
* Define **global parameters** with inheritance.
|
28
|
+
* Accept command line parameters in **any order**.
|
29
|
+
* Handle complex inputs like **lists** and **maps**.
|
30
|
+
* **Convert** inputs to basic and more complex object types.
|
31
|
+
* Generate **help** from parameter definitions.
|
32
|
+
* Customise help with **usage** methods such as **header**, **example** and more.
|
33
|
+
* Collect parsing **errors** for a better user experience.
|
33
34
|
|
34
35
|
## Installation
|
35
36
|
|
36
37
|
Add this line to your application's Gemfile:
|
37
38
|
|
38
39
|
```ruby
|
39
|
-
gem
|
40
|
+
gem "tty-option"
|
40
41
|
```
|
41
42
|
|
42
43
|
And then execute:
|
@@ -61,25 +62,27 @@ Or install it yourself as:
|
|
61
62
|
* [2.5.3 default](#253-default)
|
62
63
|
* [2.5.4 description](#254-description)
|
63
64
|
* [2.5.5 hidden](#255-hidden)
|
64
|
-
* [2.5.6
|
65
|
-
* [2.5.7
|
66
|
-
* [2.5.8
|
67
|
-
* [2.5.9
|
68
|
-
* [2.5.10
|
65
|
+
* [2.5.6 long](#256-long)
|
66
|
+
* [2.5.7 name](#257-name)
|
67
|
+
* [2.5.8 optional](#258-optional)
|
68
|
+
* [2.5.9 permit](#259-permit)
|
69
|
+
* [2.5.10 required](#2510-required)
|
70
|
+
* [2.5.11 short](#2511-short)
|
71
|
+
* [2.5.12 validate](#2512-validate)
|
69
72
|
* [2.6 parse](#26-parse)
|
70
73
|
* [2.6.1 :raise_on_parse_error](#261-raise_on_parse_error)
|
71
74
|
* [2.6.2 :check_invalid_params](#262-check_invalid_params)
|
72
75
|
* [2.7 params](#27-params)
|
73
76
|
* [2.7.1 errors](#271-errors)
|
74
77
|
* [2.7.2 remaining](#272-remaining)
|
75
|
-
* [2.7.3 valid?](#273-valid
|
78
|
+
* [2.7.3 valid?](#273-valid)
|
76
79
|
* [2.8 usage](#28-usage)
|
77
80
|
* [2.8.1 header](#281-header)
|
78
81
|
* [2.8.2 program](#282-program)
|
79
82
|
* [2.8.3 command](#283-command)
|
80
83
|
* [2.8.4 banner](#284-banner)
|
81
84
|
* [2.8.5 description](#285-description)
|
82
|
-
* [2.8.6 example](#286-
|
85
|
+
* [2.8.6 example](#286-example)
|
83
86
|
* [2.8.7 footer](#287-footer)
|
84
87
|
* [2.9 help](#29-help)
|
85
88
|
* [2.9.1 sections](#291-sections)
|
@@ -90,11 +93,15 @@ Or install it yourself as:
|
|
90
93
|
|
91
94
|
## 1. Usage
|
92
95
|
|
93
|
-
|
96
|
+
Include the `TTY::Option` module and define parameters to parse command
|
97
|
+
line input.
|
94
98
|
|
95
|
-
|
99
|
+
Choose from [arguments](#21-argument), [keywords](#22-keyword),
|
100
|
+
[flags](#23-option), [options](#23-option) and
|
101
|
+
[environment variables](#24-environment).
|
96
102
|
|
97
|
-
For example, a quick demo to create a command that mixes
|
103
|
+
For example, here is a quick demo of how to create a command that mixes
|
104
|
+
all parameter types:
|
98
105
|
|
99
106
|
```ruby
|
100
107
|
class Command
|
@@ -132,18 +139,18 @@ class Command
|
|
132
139
|
desc "Restart policy to apply when a container exits"
|
133
140
|
end
|
134
141
|
|
135
|
-
flag :help do
|
136
|
-
short "-h"
|
137
|
-
long "--help"
|
138
|
-
desc "Print usage"
|
139
|
-
end
|
140
|
-
|
141
142
|
flag :detach do
|
142
143
|
short "-d"
|
143
144
|
long "--detach"
|
144
145
|
desc "Run container in background and print container ID"
|
145
146
|
end
|
146
147
|
|
148
|
+
flag :help do
|
149
|
+
short "-h"
|
150
|
+
long "--help"
|
151
|
+
desc "Print usage"
|
152
|
+
end
|
153
|
+
|
147
154
|
option :name do
|
148
155
|
required
|
149
156
|
long "--name string"
|
@@ -161,7 +168,8 @@ class Command
|
|
161
168
|
def run
|
162
169
|
if params[:help]
|
163
170
|
print help
|
164
|
-
|
171
|
+
elsif params.errors.any?
|
172
|
+
puts params.errors.summary
|
165
173
|
else
|
166
174
|
pp params.to_h
|
167
175
|
end
|
@@ -175,21 +183,25 @@ Then create a command instance:
|
|
175
183
|
cmd = Command.new
|
176
184
|
```
|
177
185
|
|
178
|
-
And
|
186
|
+
And given the following input on the command line:
|
179
187
|
|
180
188
|
```
|
181
189
|
restart=always -d -p 5000:3000 5001:8080 --name web ubuntu:16.4 bash
|
182
190
|
```
|
183
191
|
|
184
|
-
|
192
|
+
Read the command line input (aka `ARGV`) with [parse](#26-parse):
|
185
193
|
|
186
194
|
```ruby
|
187
195
|
cmd.parse
|
188
|
-
|
196
|
+
```
|
197
|
+
|
198
|
+
Or provide an array of inputs:
|
199
|
+
|
200
|
+
```ruby
|
189
201
|
cmd.parse(%w[restart=always -d -p 5000:3000 5001:8080 --name web ubuntu:16.4 bash])
|
190
202
|
```
|
191
203
|
|
192
|
-
|
204
|
+
Finally, run the command to see parsed values:
|
193
205
|
|
194
206
|
```ruby
|
195
207
|
cmd.run
|
@@ -201,22 +213,22 @@ cmd.run
|
|
201
213
|
# :restart=>"always",
|
202
214
|
# :image=>"ubuntu:16.4",
|
203
215
|
# :command=>"bash"}
|
204
|
-
|
216
|
+
```
|
205
217
|
|
206
|
-
|
218
|
+
Use the [params](#27-params) to access all parameters:
|
207
219
|
|
208
220
|
```ruby
|
209
221
|
cmd.params[:name] # => "web"
|
210
222
|
cmd.params["command"] # => "bash
|
211
|
-
|
223
|
+
```
|
212
224
|
|
213
|
-
|
225
|
+
Given the `--help` flag on the command line:
|
214
226
|
|
215
227
|
```ruby
|
216
|
-
cmd.
|
228
|
+
cmd.parse(%w[--help])
|
217
229
|
```
|
218
230
|
|
219
|
-
|
231
|
+
Use the [help](#29-help) method to print help information to the terminal:
|
220
232
|
|
221
233
|
```ruby
|
222
234
|
print cmd.help
|
@@ -251,335 +263,435 @@ Examples:
|
|
251
263
|
$ dock run -v `pwd`:`pwd` -w `pwd` ubuntu pwd
|
252
264
|
```
|
253
265
|
|
266
|
+
Given an invalid command line input:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
cmd.parse(%w[--unknown])
|
270
|
+
```
|
271
|
+
|
272
|
+
Use the [errors](#271-errors) method to print all errors:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
puts params.errors.summary
|
276
|
+
```
|
277
|
+
|
278
|
+
This will print a summary of all errors:
|
279
|
+
|
280
|
+
```
|
281
|
+
Errors:
|
282
|
+
1) Invalid option '--unknown'
|
283
|
+
2) Option '--publish' should appear at least 1 time but appeared 0 times
|
284
|
+
3) Option '--name' must be provided
|
285
|
+
4) Argument 'image' must be provided
|
286
|
+
```
|
287
|
+
|
254
288
|
## 2. API
|
255
289
|
|
256
290
|
### 2.1 argument
|
257
291
|
|
258
|
-
|
292
|
+
Use the `argument` method to parse positional arguments.
|
293
|
+
|
294
|
+
Provide a name as a string or symbol to define an argument. The name will
|
295
|
+
serve as a default label for the help display and a key to retrieve
|
296
|
+
a value from the [params](#27-params):
|
259
297
|
|
260
298
|
```ruby
|
261
299
|
argument :foo
|
262
300
|
```
|
263
301
|
|
264
|
-
|
302
|
+
Given the following command line input:
|
265
303
|
|
266
304
|
```
|
267
305
|
11 12 13
|
268
306
|
```
|
269
307
|
|
270
|
-
|
308
|
+
This would result only in one argument parsed and the remaining ignored:
|
271
309
|
|
272
310
|
```ruby
|
273
311
|
params[:foo] # => "11"
|
274
312
|
```
|
275
313
|
|
276
|
-
|
314
|
+
The `argument` method accepts a block to define
|
315
|
+
[parameter settings](#25-parameter-settings).
|
316
|
+
|
317
|
+
For example, use the [arity](#251-arity) and [convert](#252-convert) settings
|
318
|
+
to parse many positional arguments:
|
277
319
|
|
278
320
|
```ruby
|
279
321
|
argument :foo do
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
desc "Some foo desc" # description for the usage display
|
322
|
+
name "foo(int)" # name for help display
|
323
|
+
arity one_or_more # how many times can appear
|
324
|
+
convert :int_list # convert input to a list of integers
|
325
|
+
validate ->(v) { v < 14 } # validation rule
|
326
|
+
desc "Argument description" # description for help display
|
286
327
|
end
|
287
328
|
```
|
288
329
|
|
289
|
-
|
290
|
-
|
291
|
-
```bash
|
292
|
-
11 12 13
|
293
|
-
```
|
294
|
-
|
295
|
-
Would result in all values being collected and converted to integers:
|
330
|
+
Parser would collect all values and convert them to integers given
|
331
|
+
previous input:
|
296
332
|
|
297
333
|
```ruby
|
298
|
-
params[:foo] # => [11,12,13]
|
334
|
+
params[:foo] # => [11, 12, 13]
|
299
335
|
```
|
300
336
|
|
301
|
-
The
|
337
|
+
The `argument` method can also accept settings as keyword arguments:
|
302
338
|
|
303
339
|
```ruby
|
304
340
|
argument :foo,
|
305
|
-
|
306
|
-
variable: "foo(int)",
|
341
|
+
name: "foo(int)",
|
307
342
|
arity: "+",
|
308
|
-
convert: :
|
309
|
-
validate: -> {
|
310
|
-
desc: "
|
343
|
+
convert: :int_list,
|
344
|
+
validate: ->(v) { v < 14 },
|
345
|
+
desc: "Argument description"
|
311
346
|
```
|
312
347
|
|
313
|
-
To read more about available settings see [parameter settings](#25-parameter-settings).
|
314
|
-
|
315
348
|
### 2.2 keyword
|
316
349
|
|
317
|
-
|
350
|
+
Use the `keyword` method to parse keyword arguments.
|
351
|
+
|
352
|
+
Provide a name as a string or symbol to define a keyword argument. The name
|
353
|
+
will serve as a command line input name, a default label for the help
|
354
|
+
display and a key to retrieve a value from the [params](#27-params):
|
318
355
|
|
319
356
|
```ruby
|
320
357
|
keyword :foo
|
321
358
|
```
|
322
359
|
|
323
|
-
|
360
|
+
Parser will use the parameter name to match the input name on the command
|
361
|
+
line by default.
|
362
|
+
|
363
|
+
Given the following command line input:
|
324
364
|
|
325
|
-
```
|
365
|
+
```
|
326
366
|
foo=11
|
327
367
|
```
|
328
368
|
|
329
|
-
|
369
|
+
This would result in:
|
330
370
|
|
331
371
|
```ruby
|
332
372
|
params[:foo] # => "11"
|
333
373
|
```
|
334
374
|
|
335
|
-
|
336
|
-
|
337
|
-
```ruby
|
338
|
-
keyword :foo do
|
339
|
-
required # by default keywrod is not required
|
340
|
-
arity one_or_more # how many times to occur
|
341
|
-
convert :int # values converted to integer
|
342
|
-
validate -> { |v| v < 14 } # validation rule
|
343
|
-
desc "Some foo desc" # description for the usage display
|
344
|
-
end
|
345
|
-
```
|
346
|
-
|
347
|
-
Then provided the following command line input:
|
348
|
-
|
349
|
-
```bash
|
350
|
-
foo=11 foo=12 foo=13
|
351
|
-
```
|
352
|
-
|
353
|
-
The result would be:
|
375
|
+
Note that the parser performs no conversion of the value.
|
354
376
|
|
355
|
-
|
356
|
-
|
357
|
-
```
|
377
|
+
The `keyword` method accepts a block to define
|
378
|
+
[parameter settings](#25-parameter-settings).
|
358
379
|
|
359
|
-
|
380
|
+
For example, use the [arity](#251-arity) and [convert](#252-convert)
|
381
|
+
settings to parse many keyword arguments:
|
360
382
|
|
361
383
|
```ruby
|
362
384
|
keyword :foo do
|
363
385
|
required # by default keyword is not required
|
364
|
-
arity one_or_more # how many times
|
365
|
-
convert :int_list # input
|
366
|
-
validate -> {
|
367
|
-
desc "
|
386
|
+
arity one_or_more # how many times can appear
|
387
|
+
convert :int_list # convert input to a list of integers
|
388
|
+
validate ->(v) { v < 14 } # validation rule
|
389
|
+
desc "Keyword description" # description for help display
|
368
390
|
end
|
369
391
|
```
|
370
392
|
|
371
|
-
|
393
|
+
Given the following command line input:
|
372
394
|
|
373
|
-
```
|
374
|
-
foo=11 12
|
395
|
+
```
|
396
|
+
foo=10,11 foo=12 13
|
375
397
|
```
|
376
398
|
|
377
|
-
|
399
|
+
This would result in an array of integers:
|
378
400
|
|
379
401
|
```ruby
|
380
|
-
params[:foo] # => [11,12,13]
|
402
|
+
params[:foo] # => [10, 11, 12, 13]
|
381
403
|
```
|
382
404
|
|
383
|
-
|
405
|
+
The `keyword` method can also accept settings as keyword arguments:
|
384
406
|
|
385
407
|
```ruby
|
386
408
|
keyword :foo,
|
387
409
|
required: true,
|
388
410
|
arity: :+,
|
389
411
|
convert: :int_list,
|
390
|
-
validate: -> {
|
391
|
-
desc: "
|
412
|
+
validate: ->(v) { v < 14 },
|
413
|
+
desc: "Keyword description"
|
392
414
|
```
|
393
415
|
|
394
|
-
To read more about available settings see [parameter settings](#25-parameter-settings).
|
395
|
-
|
396
416
|
### 2.3 option
|
397
417
|
|
398
|
-
|
418
|
+
Use the `flag` or `option` methods to parse options.
|
399
419
|
|
400
|
-
|
420
|
+
Provide a name as a string or symbol to define an option. The name will
|
421
|
+
serve as a command line input name, a label for the help display and
|
422
|
+
a key to retrieve a value from the [params](#27-params):
|
401
423
|
|
402
424
|
```ruby
|
403
425
|
option :foo
|
404
426
|
```
|
405
427
|
|
406
|
-
|
428
|
+
Parser will use the parameter name to generate a long option name by default.
|
429
|
+
|
430
|
+
Given the following command line input:
|
407
431
|
|
408
432
|
```
|
409
|
-
--foo
|
433
|
+
--foo
|
410
434
|
```
|
411
435
|
|
412
|
-
|
436
|
+
This would result in:
|
413
437
|
|
414
438
|
```ruby
|
415
|
-
params[:foo] # =>
|
439
|
+
params[:foo] # => true
|
416
440
|
```
|
417
441
|
|
418
|
-
|
442
|
+
The `flag` and `option` methods accept a block to define
|
443
|
+
[parameter settings](#25-parameter-settings).
|
444
|
+
|
445
|
+
For example, to specify a different name for the parsed option,
|
446
|
+
use the [short](#2511-short) and [long](#256-long) settings:
|
419
447
|
|
420
448
|
```ruby
|
421
449
|
option :foo do
|
422
|
-
short "-f"
|
423
|
-
long
|
450
|
+
short "-f" # define a short name
|
451
|
+
long "--foo" # define a long name
|
424
452
|
end
|
425
453
|
```
|
426
454
|
|
427
|
-
|
455
|
+
Given the following short name on the command line:
|
456
|
+
|
457
|
+
```
|
458
|
+
-f
|
459
|
+
```
|
460
|
+
|
461
|
+
This would result in:
|
462
|
+
|
463
|
+
```
|
464
|
+
params[:foo] # => true
|
465
|
+
```
|
466
|
+
|
467
|
+
An option can accept an argument. The argument can be either required
|
468
|
+
or optional. To define a required argument, provide an extra label
|
469
|
+
in `short` or `long` settings. The label can be any string. When
|
470
|
+
both `short` and `long` names are present, only specify an argument
|
471
|
+
for the long name.
|
428
472
|
|
429
|
-
For example, for both short and long
|
473
|
+
For example, for both short and long names to accept a required
|
474
|
+
integer argument:
|
430
475
|
|
431
476
|
```ruby
|
432
477
|
option :foo do
|
433
478
|
short "-f"
|
434
|
-
long
|
479
|
+
long "--foo int"
|
435
480
|
# or
|
436
|
-
long
|
481
|
+
long "--foo=int"
|
437
482
|
end
|
438
483
|
```
|
439
484
|
|
440
|
-
|
485
|
+
Given the following command line input:
|
486
|
+
|
487
|
+
```
|
488
|
+
--foo=11
|
489
|
+
```
|
490
|
+
|
491
|
+
This would result in:
|
492
|
+
|
493
|
+
```ruby
|
494
|
+
params[:foo] # => "11"
|
495
|
+
```
|
496
|
+
|
497
|
+
Note that the parser performs no conversion of the argument.
|
498
|
+
|
499
|
+
To define an optional argument, surround it with square brackets.
|
500
|
+
|
501
|
+
For example, to accept an optional integer argument:
|
441
502
|
|
442
503
|
```ruby
|
443
504
|
option :foo do
|
444
|
-
long "--foo [
|
505
|
+
long "--foo [int]"
|
445
506
|
end
|
446
507
|
```
|
447
508
|
|
448
|
-
|
509
|
+
Use the [arity](#251-arity) and [convert](#252-convert) settings to parse
|
510
|
+
many options given as a list of integers:
|
449
511
|
|
450
512
|
```ruby
|
451
513
|
option :foo do
|
452
514
|
required # by default option is not required
|
453
|
-
arity one_or_more # how many times option can
|
454
|
-
short "-f" #
|
455
|
-
long
|
456
|
-
convert :int_list # input
|
457
|
-
validate -> {
|
458
|
-
desc "
|
515
|
+
arity one_or_more # how many times option can appear
|
516
|
+
short "-f" # declare a short flag name
|
517
|
+
long "--foo ints" # declare a long flag with a required argument
|
518
|
+
convert :int_list # convert input to a list of integers
|
519
|
+
validate ->(v) { v < 14 } # validation rule
|
520
|
+
desc "Option description" # description for help display
|
459
521
|
end
|
460
522
|
```
|
461
523
|
|
462
|
-
Given command line input:
|
524
|
+
Given the following command line input:
|
463
525
|
|
464
|
-
```
|
526
|
+
```
|
465
527
|
--foo=10,11 -f 12 13
|
466
528
|
```
|
467
529
|
|
468
|
-
|
530
|
+
This would result in an array of integers:
|
469
531
|
|
470
532
|
```ruby
|
471
|
-
params[:foo] # => [10,11,12,13]
|
533
|
+
params[:foo] # => [10, 11, 12, 13]
|
472
534
|
```
|
473
535
|
|
474
|
-
|
536
|
+
The option method can also accept settings as keyword arguments:
|
475
537
|
|
476
538
|
```ruby
|
477
539
|
option :foo,
|
478
540
|
required: true,
|
479
541
|
arity: :+,
|
480
542
|
short: "-f",
|
481
|
-
long: "--foo
|
543
|
+
long: "--foo ints",
|
482
544
|
convert: :int_list,
|
483
545
|
validate: -> { |v| v < 14 },
|
484
|
-
desc: "
|
546
|
+
desc: "Option description"
|
547
|
+
```
|
548
|
+
|
549
|
+
There is a convenience `flag` method to specify a command line option that
|
550
|
+
accepts no argument:
|
551
|
+
|
552
|
+
```ruby
|
553
|
+
flag :foo
|
485
554
|
```
|
486
555
|
|
487
|
-
|
556
|
+
For example, a typical scenario is to specify the help flag:
|
557
|
+
|
558
|
+
```ruby
|
559
|
+
flag :help do
|
560
|
+
short "-h"
|
561
|
+
long "--help"
|
562
|
+
desc "Print usage"
|
563
|
+
end
|
564
|
+
```
|
488
565
|
|
489
566
|
### 2.4 environment
|
490
567
|
|
491
|
-
|
568
|
+
Use the `environment` or `env` methods to parse environment variables.
|
492
569
|
|
493
|
-
|
570
|
+
Provide a name as a string or symbol to define an environment variable.
|
571
|
+
The name will serve as a command line input name, a default label for the
|
572
|
+
help display and a key to retrieve a value from the [params](#27-params):
|
494
573
|
|
495
574
|
```ruby
|
575
|
+
environment :foo
|
576
|
+
# or
|
496
577
|
env :foo
|
497
578
|
```
|
498
579
|
|
499
|
-
|
580
|
+
Parser will use the parameter name to match the input name on the command
|
581
|
+
line by default.
|
582
|
+
|
583
|
+
Given the following command line input:
|
500
584
|
|
501
585
|
```
|
502
|
-
FOO=
|
586
|
+
FOO=11
|
503
587
|
```
|
504
588
|
|
505
|
-
The
|
589
|
+
The result would be:
|
506
590
|
|
507
591
|
```ruby
|
508
|
-
params[:foo] # => "
|
509
|
-
|
592
|
+
params[:foo] # => "11"
|
593
|
+
```
|
594
|
+
|
595
|
+
Note that the parser performs no conversion of the value.
|
510
596
|
|
511
|
-
|
597
|
+
The `environment` method accepts a block to define
|
598
|
+
[parameter settings](#25-parameter-settings).
|
599
|
+
|
600
|
+
For example, use the [name](#257-name) setting to change a default
|
601
|
+
variable name:
|
512
602
|
|
513
603
|
```ruby
|
514
|
-
|
515
|
-
|
604
|
+
environment :foo do
|
605
|
+
name "FOO_ENV"
|
516
606
|
end
|
517
607
|
```
|
518
608
|
|
519
|
-
|
609
|
+
Given the following command line input:
|
610
|
+
|
611
|
+
```
|
612
|
+
FOO_ENV=11
|
613
|
+
```
|
614
|
+
|
615
|
+
This would result in:
|
520
616
|
|
521
617
|
```ruby
|
522
|
-
params[:foo] # => "
|
618
|
+
params[:foo] # => "11"
|
523
619
|
```
|
524
620
|
|
525
|
-
|
621
|
+
For example, use the [arity](#251-arity) and [convert](#252-convert) settings
|
622
|
+
to parse many environment variables given as a list of integers:
|
526
623
|
|
527
624
|
```ruby
|
528
625
|
environment :foo do
|
529
|
-
required
|
530
|
-
arity one_or_more
|
531
|
-
|
532
|
-
convert
|
533
|
-
validate -> {
|
534
|
-
desc "
|
626
|
+
required # by default environment is not required
|
627
|
+
arity one_or_more # how many times env var can appear
|
628
|
+
name "FOO_ENV" # the command line input name
|
629
|
+
convert :int_list # convert input to a map of integers
|
630
|
+
validate ->(v) { v < 14 } # validation rule
|
631
|
+
desc "Environment description" # description for help display
|
535
632
|
end
|
536
633
|
```
|
537
634
|
|
538
|
-
Given command line input:
|
635
|
+
Given the following command line input:
|
539
636
|
|
540
|
-
```
|
541
|
-
FOO_ENV=
|
637
|
+
```
|
638
|
+
FOO_ENV=10,11 FOO_ENV=12 13
|
542
639
|
```
|
543
640
|
|
544
|
-
|
641
|
+
This would result in an array of integers:
|
545
642
|
|
546
643
|
```ruby
|
547
|
-
params[:foo] # =>
|
644
|
+
params[:foo] # => [10, 11, 12, 13]
|
548
645
|
```
|
549
646
|
|
550
|
-
|
647
|
+
The `environment` method can also accept settings as keyword arguments:
|
648
|
+
|
649
|
+
```ruby
|
650
|
+
environment :foo,
|
651
|
+
required: true,
|
652
|
+
arity: :+,
|
653
|
+
name: "FOO_ENV",
|
654
|
+
convert: :int_list,
|
655
|
+
validate: ->(v) { v < 14 },
|
656
|
+
desc: "Environment description"
|
657
|
+
```
|
551
658
|
|
552
659
|
### 2.5 parameter settings
|
553
660
|
|
554
|
-
|
661
|
+
All parameter types support the following settings except for
|
662
|
+
`short` and `long`, which are [option](#23-option) specific.
|
555
663
|
|
556
664
|
#### 2.5.1 arity
|
557
665
|
|
558
|
-
|
666
|
+
Use the `arity` setting to describe how many times a given parameter may
|
667
|
+
appear on the command line.
|
559
668
|
|
560
|
-
|
669
|
+
Every parameter can appear only once by default. In the case of arguments,
|
670
|
+
the parser will match the first input and ignore the rest. For other
|
671
|
+
parameter types, any extra parameter occurrence will override previously
|
672
|
+
parsed input. Setting the arity requirement overrides this behaviour.
|
561
673
|
|
562
|
-
For example, to match argument exactly
|
674
|
+
For example, to match an argument exactly two times:
|
563
675
|
|
564
676
|
```ruby
|
565
677
|
argument :foo do
|
566
678
|
arity 2
|
567
679
|
end
|
568
|
-
|
680
|
+
```
|
569
681
|
|
570
|
-
|
682
|
+
Given the following command line input:
|
571
683
|
|
572
684
|
```ruby
|
573
685
|
bar baz
|
574
686
|
```
|
575
687
|
|
576
|
-
|
688
|
+
This would result in an array of strings:
|
577
689
|
|
578
690
|
```ruby
|
579
691
|
params[:foo] # => ["bar", "baz"]
|
580
692
|
```
|
581
693
|
|
582
|
-
|
694
|
+
Another example is to match exactly three occurrences of a keyword:
|
583
695
|
|
584
696
|
```ruby
|
585
697
|
keyword :foo do
|
@@ -587,19 +699,22 @@ keyword :foo do
|
|
587
699
|
end
|
588
700
|
```
|
589
701
|
|
590
|
-
And then
|
702
|
+
And then given the following on the command line:
|
591
703
|
|
592
704
|
```
|
593
705
|
foo=1 foo=2 foo=3
|
594
706
|
```
|
595
707
|
|
596
|
-
|
708
|
+
This would result in an array of strings:
|
597
709
|
|
598
710
|
```ruby
|
599
711
|
params[:foo] # => ["1", "2", "3"]
|
600
712
|
```
|
601
713
|
|
602
|
-
|
714
|
+
Use `:any`, `:*`, `-1`, `any` or `zero_or_more` to specify that parameter
|
715
|
+
may appear any number of times.
|
716
|
+
|
717
|
+
For example, to expect an argument to appear zero or more times:
|
603
718
|
|
604
719
|
```ruby
|
605
720
|
argument :foo do
|
@@ -607,17 +722,21 @@ argument :foo do
|
|
607
722
|
end
|
608
723
|
```
|
609
724
|
|
610
|
-
|
725
|
+
Use `:+` or `one_or_more` to specify that parameter must appear at least once.
|
726
|
+
|
727
|
+
For example, to expect an option with an argument to appear one or more times:
|
611
728
|
|
612
729
|
```ruby
|
613
730
|
option :foo do
|
614
731
|
arity one_or_more
|
615
|
-
short "-
|
616
|
-
long "--
|
732
|
+
short "-f"
|
733
|
+
long "--foo string"
|
617
734
|
end
|
618
735
|
```
|
619
736
|
|
620
|
-
|
737
|
+
Use `at_least` to specify the least number of times a parameter can appear:
|
738
|
+
|
739
|
+
For example, to expect a keyword to appear at least three times:
|
621
740
|
|
622
741
|
```ruby
|
623
742
|
keyword :foo do
|
@@ -625,7 +744,9 @@ keyword :foo do
|
|
625
744
|
end
|
626
745
|
```
|
627
746
|
|
628
|
-
The [help](#29-help) method will handle the arity for the
|
747
|
+
The [help](#29-help) method will handle the arity for the usage banner.
|
748
|
+
|
749
|
+
For example, given the following argument definition:
|
629
750
|
|
630
751
|
```ruby
|
631
752
|
argument :foo do
|
@@ -633,7 +754,7 @@ argument :foo do
|
|
633
754
|
end
|
634
755
|
```
|
635
756
|
|
636
|
-
The usage banner
|
757
|
+
The usage banner would display:
|
637
758
|
|
638
759
|
```
|
639
760
|
Usage: foobar FOO [FOO...]
|
@@ -641,7 +762,11 @@ Usage: foobar FOO [FOO...]
|
|
641
762
|
|
642
763
|
#### 2.5.2 convert
|
643
764
|
|
644
|
-
|
765
|
+
Use the `convert` setting to transform any parameter argument to another type.
|
766
|
+
|
767
|
+
The `convert` accepts a conversion name as a predefined symbol or class.
|
768
|
+
|
769
|
+
For example, to convert an argument to an integer:
|
645
770
|
|
646
771
|
```ruby
|
647
772
|
argument :foo do
|
@@ -651,75 +776,89 @@ argument :foo do
|
|
651
776
|
end
|
652
777
|
```
|
653
778
|
|
654
|
-
The conversion types
|
779
|
+
The supported conversion types are:
|
655
780
|
|
656
|
-
* `:
|
657
|
-
|
781
|
+
* `:bool` or `:boolean` - e.g. `yes,1,y,t` becomes `true`,
|
782
|
+
`no,0,n,f` becomes `false`
|
783
|
+
* `:date` - e.g. `28/03/2020` becomes `#<Date: 2020-03-28...>`
|
658
784
|
* `:float` - e.g. `-1` becomes `-1.0`
|
659
|
-
* `:int
|
660
|
-
* `:path
|
661
|
-
|
662
|
-
* `:
|
663
|
-
* `:
|
664
|
-
* `:
|
665
|
-
* `:
|
785
|
+
* `:int` or `:integer` - e.g. `+1` becomes `1`
|
786
|
+
* `:path` or `:pathname` - e.g. `/foo/bar/baz` becomes
|
787
|
+
`#<Pathname:/foo/bar/baz>`
|
788
|
+
* `:regex` or `:regexp` - e.g. `foo|bar` becomes `/foo|bar/`
|
789
|
+
* `:uri` - e.g. `foo.com` becomes `#<URI::Generic foo.com>`
|
790
|
+
* `:sym` or `:symbol` - e.g. `foo` becomes `:foo`
|
791
|
+
* `:list` or `:array` - e.g. `a,b,c` becomes `["a", "b", "c"]`
|
792
|
+
* `:map` or `:hash` - e.g. `a:1 b:2 c:3` becomes `{a: "1", b: "2", c: "3"}`
|
666
793
|
|
667
|
-
|
794
|
+
To convert to an array of a given type, specify plural or append an `array`
|
795
|
+
or`list` to any base type:
|
668
796
|
|
669
|
-
* `:
|
670
|
-
|
671
|
-
* `:
|
797
|
+
* `:bools`, `:bool_array` or `:bool_list` - e.g. `t,f,t` becomes
|
798
|
+
`[true, false, true]`
|
799
|
+
* `:floats`, `:float_array` or `:float_list` - e.g. `1,2,3` becomes
|
800
|
+
`[1.0, 2.0, 3.0]`
|
801
|
+
* `:ints`, `:int_array` or `:int_list` - e.g. `1,2,3` becomes `[1, 2, 3]`
|
672
802
|
|
673
|
-
|
803
|
+
Or, use the `list_of` method and pass the type as a first argument.
|
674
804
|
|
675
|
-
|
805
|
+
To convert to a hash with values of a given type, append a `hash` or `map` to
|
806
|
+
any base type:
|
676
807
|
|
677
|
-
* `:
|
678
|
-
|
808
|
+
* `:bool_hash` or `:bool_map` - e.g `a:t b:f c:t` becomes
|
809
|
+
`{a: true, b: false, c: true}`
|
810
|
+
* `:float_hash` or `:float_map` - e.g `a:1 b:2 c:3` becomes
|
811
|
+
`{a: 1.0, b: 2.0, c: 3.0}`
|
812
|
+
* `:int_hash` or `:int_map` - e.g `a:1 b:2 c:3` becomes `{a: 1, b: 2, c: 3}`
|
679
813
|
|
680
|
-
|
814
|
+
Or, use the `map_of` method and pass the type as a first argument.
|
681
815
|
|
682
|
-
For example,
|
816
|
+
For example, given options with a required list and map arguments:
|
683
817
|
|
684
818
|
```ruby
|
685
819
|
option :foo do
|
686
|
-
long "--foo
|
687
|
-
convert :bools
|
820
|
+
long "--foo list"
|
821
|
+
convert :bools
|
822
|
+
# or
|
823
|
+
convert list_of(:bool)
|
688
824
|
end
|
689
825
|
|
690
826
|
option :bar do
|
691
|
-
long "--bar
|
692
|
-
convert :int_map
|
827
|
+
long "--bar map"
|
828
|
+
convert :int_map
|
829
|
+
# or
|
830
|
+
convert map_of(:int)
|
693
831
|
end
|
694
|
-
|
832
|
+
```
|
695
833
|
|
696
|
-
And then parsing the following:
|
834
|
+
And then parsing the following command line input:
|
697
835
|
|
698
|
-
```
|
836
|
+
```
|
699
837
|
--foo t,f,t --bar a:1 b:2 c:3
|
700
838
|
```
|
701
839
|
|
702
|
-
|
840
|
+
This would result in an array of booleans and a hash with integer values:
|
703
841
|
|
704
842
|
```ruby
|
705
|
-
params[:foo]
|
706
|
-
# =>
|
707
|
-
|
708
|
-
# => {:a=>1, :b=>2, :c=>3}
|
709
|
-
````
|
843
|
+
params[:foo] # => [true, false, true]
|
844
|
+
params[:bar] # => {:a=>1, :b=>2, :c=>3}
|
845
|
+
```
|
710
846
|
|
711
|
-
|
847
|
+
Use a `Proc` object to define custom conversion.
|
848
|
+
|
849
|
+
For example, to convert the command line input to uppercase:
|
712
850
|
|
713
851
|
```ruby
|
714
|
-
option :
|
715
|
-
long "--
|
852
|
+
option :foo do
|
853
|
+
long "--foo string"
|
716
854
|
convert ->(val) { val.upcase }
|
717
855
|
end
|
718
856
|
```
|
719
857
|
|
720
858
|
#### 2.5.3 default
|
721
859
|
|
722
|
-
|
860
|
+
Use the `default` setting to specify a default value for an optional parameter.
|
861
|
+
The parser will use default when the command line input isn't present.
|
723
862
|
|
724
863
|
For example, given the following option definition:
|
725
864
|
|
@@ -730,13 +869,14 @@ option :foo do
|
|
730
869
|
end
|
731
870
|
```
|
732
871
|
|
733
|
-
When
|
872
|
+
When the option `--foo` isn't present on the command line, the `params`
|
873
|
+
will have a default value set:
|
734
874
|
|
735
875
|
```ruby
|
736
876
|
params[:foo] # => "bar"
|
737
877
|
```
|
738
878
|
|
739
|
-
|
879
|
+
Or, use a `Proc` object to specify a default value:
|
740
880
|
|
741
881
|
```ruby
|
742
882
|
option :foo do
|
@@ -745,47 +885,55 @@ option :foo do
|
|
745
885
|
end
|
746
886
|
```
|
747
887
|
|
748
|
-
A parameter cannot be both required and have default value.
|
888
|
+
A parameter cannot be both required and have a default value. This will raise
|
889
|
+
`ConfigurationError`. The parser treats positional arguments as required.
|
890
|
+
To have a default for a required argument make it [optional](#258-optional):
|
749
891
|
|
750
892
|
```ruby
|
751
893
|
argument :foo do
|
752
894
|
optional
|
753
895
|
default "bar"
|
754
|
-
desc "
|
896
|
+
desc "Argument description"
|
755
897
|
end
|
756
898
|
```
|
757
899
|
|
758
|
-
The
|
900
|
+
The usage description for a given parameter will display the default value:
|
759
901
|
|
760
902
|
```
|
761
|
-
Usage: foobar [
|
903
|
+
Usage: foobar [FOO]
|
762
904
|
|
763
905
|
Arguments:
|
764
|
-
FOO
|
906
|
+
FOO Argument description (default "bar")
|
765
907
|
```
|
766
908
|
|
767
|
-
#### 2.5.4
|
909
|
+
#### 2.5.4 description
|
768
910
|
|
769
|
-
|
911
|
+
Use the `description` or `desc` setting to provide a summary for
|
912
|
+
a parameter. The [help](#29-help) method uses a parameter
|
913
|
+
description to generate a usage display.
|
914
|
+
|
915
|
+
For example, given an option with a description:
|
770
916
|
|
771
917
|
```ruby
|
772
918
|
option :foo do
|
773
|
-
desc "
|
919
|
+
desc "Option description"
|
774
920
|
end
|
775
921
|
```
|
776
922
|
|
777
|
-
|
923
|
+
This will result in the following help display:
|
778
924
|
|
779
925
|
```
|
780
926
|
Usage: foobar [OPTIONS]
|
781
927
|
|
782
928
|
Options:
|
783
|
-
--foo
|
929
|
+
--foo Option description
|
784
930
|
```
|
785
931
|
|
786
932
|
#### 2.5.5 hidden
|
787
933
|
|
788
|
-
|
934
|
+
Use the `hidden` setting to hide a parameter from the [help](#29-help) display.
|
935
|
+
|
936
|
+
For example, given a standard argument and a hidden one:
|
789
937
|
|
790
938
|
```ruby
|
791
939
|
argument :foo
|
@@ -795,55 +943,140 @@ argument :bar do
|
|
795
943
|
end
|
796
944
|
```
|
797
945
|
|
798
|
-
The above will hide the `:bar` parameter from the usage:
|
946
|
+
The above will hide the `:bar` parameter from the usage banner:
|
799
947
|
|
800
948
|
```
|
801
949
|
Usage: foobar FOO
|
802
950
|
```
|
803
951
|
|
804
|
-
#### 2.5.6
|
952
|
+
#### 2.5.6 long
|
953
|
+
|
954
|
+
Only [flag](#23-option) and [option](#23-option) parameters can use
|
955
|
+
the `long` setting.
|
956
|
+
|
957
|
+
Use the `long` setting to define a long name for an option. By convention,
|
958
|
+
a long name uses a double dash followed by many characters.
|
959
|
+
|
960
|
+
When you don't specify a short or long name, the parameter name
|
961
|
+
will serve as the option's long name by default.
|
962
|
+
|
963
|
+
For example, to define the `--foo` option:
|
964
|
+
|
965
|
+
```ruby
|
966
|
+
option :foo
|
967
|
+
```
|
968
|
+
|
969
|
+
To change the default name to the `--fuu` option:
|
970
|
+
|
971
|
+
```ruby
|
972
|
+
option :foo do
|
973
|
+
long "--fuu"
|
974
|
+
end
|
975
|
+
```
|
976
|
+
|
977
|
+
A long option can accept an argument. The argument can be either required
|
978
|
+
or optional. To define a required argument, separate it from the option
|
979
|
+
name with a space or an equal sign.
|
980
|
+
|
981
|
+
For the `:foo` option to accept a required integer argument:
|
982
|
+
|
983
|
+
```ruby
|
984
|
+
option :foo do
|
985
|
+
long "--foo int"
|
986
|
+
end
|
987
|
+
```
|
988
|
+
|
989
|
+
These are all equivalent ways to define a long option with a required
|
990
|
+
argument:
|
991
|
+
|
992
|
+
```ruby
|
993
|
+
long "--foo int"
|
994
|
+
long "--foo=int"
|
995
|
+
```
|
996
|
+
|
997
|
+
To define an optional argument, surround it with square brackets. Like
|
998
|
+
the required argument, separate it from the option name with a space
|
999
|
+
or an equal sign. It is possible to skip the space, but that would
|
1000
|
+
make the option description hard to read.
|
1001
|
+
|
1002
|
+
For the `:foo` option to accept an optional integer argument:
|
1003
|
+
|
1004
|
+
```ruby
|
1005
|
+
option :foo do
|
1006
|
+
long "--foo [int]"
|
1007
|
+
end
|
1008
|
+
```
|
1009
|
+
|
1010
|
+
These are all equivalent ways to define a long option with
|
1011
|
+
an optional argument:
|
805
1012
|
|
806
|
-
|
1013
|
+
```ruby
|
1014
|
+
long "--foo [int]"
|
1015
|
+
long "--foo=[int]"
|
1016
|
+
long "--foo[int]"
|
1017
|
+
```
|
1018
|
+
|
1019
|
+
When specifying short and long option names, only define the argument
|
1020
|
+
for the long name.
|
1021
|
+
|
1022
|
+
For example, to define an option with short and long names that accepts
|
1023
|
+
a required integer argument:
|
1024
|
+
|
1025
|
+
```ruby
|
1026
|
+
option :foo do
|
1027
|
+
short "-f"
|
1028
|
+
long "--foo int"
|
1029
|
+
end
|
1030
|
+
```
|
1031
|
+
|
1032
|
+
Note that the parser performs no conversion of the argument. Use the
|
1033
|
+
[convert](#252-convert) setting to transform the argument type.
|
807
1034
|
|
808
|
-
|
1035
|
+
#### 2.5.7 name
|
1036
|
+
|
1037
|
+
The parser will use a parameter name to match command line inputs by default.
|
1038
|
+
It will convert underscores in a name into dashes when matching input.
|
1039
|
+
|
1040
|
+
For example, given the `:foo_bar` keyword definition:
|
809
1041
|
|
810
1042
|
```ruby
|
811
1043
|
keyword :foo_bar
|
812
1044
|
```
|
813
1045
|
|
814
|
-
And
|
1046
|
+
And the following command line input:
|
815
1047
|
|
816
1048
|
```
|
817
1049
|
foo-bar=baz
|
818
1050
|
```
|
819
1051
|
|
820
|
-
|
1052
|
+
This would result in:
|
821
1053
|
|
822
1054
|
```ruby
|
823
1055
|
params[:foo_bar] # => "baz"
|
824
1056
|
```
|
825
1057
|
|
826
|
-
|
1058
|
+
Use the `name` setting to change the parameter default input name.
|
827
1059
|
|
828
1060
|
```ruby
|
829
|
-
|
1061
|
+
keyword :foo_bar do
|
830
1062
|
name "fum"
|
831
1063
|
end
|
832
1064
|
```
|
833
1065
|
|
834
|
-
|
1066
|
+
Given the following command line input:
|
835
1067
|
|
836
1068
|
```
|
837
1069
|
fum=baz
|
838
1070
|
```
|
839
1071
|
|
840
|
-
|
1072
|
+
This would result in:
|
841
1073
|
|
1074
|
+
```ruby
|
1075
|
+
params[:foo_bar] # => "baz"
|
842
1076
|
```
|
843
|
-
params[:foo] # => "baz"
|
844
|
-
````
|
845
1077
|
|
846
|
-
|
1078
|
+
Use uppercase characters when changing the input name for
|
1079
|
+
environment variables:
|
847
1080
|
|
848
1081
|
```ruby
|
849
1082
|
env :foo do
|
@@ -851,105 +1084,137 @@ env :foo do
|
|
851
1084
|
end
|
852
1085
|
```
|
853
1086
|
|
854
|
-
#### 2.5.
|
1087
|
+
#### 2.5.8 optional
|
1088
|
+
|
1089
|
+
All parameters are optional apart from positional arguments.
|
855
1090
|
|
856
|
-
|
1091
|
+
Use the `optional` setting to mark a parameter as optional.
|
1092
|
+
|
1093
|
+
For example, given a required argument and an optional one:
|
857
1094
|
|
858
1095
|
```ruby
|
859
1096
|
argument :foo do
|
860
|
-
desc "Foo
|
1097
|
+
desc "Foo argument description"
|
861
1098
|
end
|
862
1099
|
|
863
1100
|
argument :bar do
|
864
1101
|
optional
|
865
|
-
desc "Bar
|
1102
|
+
desc "Bar argument description"
|
866
1103
|
end
|
867
1104
|
```
|
868
1105
|
|
869
|
-
|
1106
|
+
And given the following command line input:
|
1107
|
+
|
1108
|
+
```
|
1109
|
+
baz
|
1110
|
+
```
|
1111
|
+
|
1112
|
+
This would result in:
|
1113
|
+
|
1114
|
+
```ruby
|
1115
|
+
params[:foo] # => "baz"
|
1116
|
+
params[:bar] # => nil
|
1117
|
+
```
|
1118
|
+
|
1119
|
+
The usage banner will display an optional argument surrounded by brackets:
|
870
1120
|
|
871
1121
|
```
|
872
|
-
Usage: foobar
|
1122
|
+
Usage: foobar FOO [BAR]
|
873
1123
|
|
874
1124
|
Arguments:
|
875
|
-
FOO Foo
|
876
|
-
BAR Bar
|
1125
|
+
FOO Foo argument description
|
1126
|
+
BAR Bar argument description
|
877
1127
|
```
|
878
1128
|
|
879
|
-
#### 2.5.
|
1129
|
+
#### 2.5.9 permit
|
880
1130
|
|
881
|
-
|
1131
|
+
Use the `permit` setting to restrict input to a set of possible values.
|
882
1132
|
|
883
|
-
For example,
|
1133
|
+
For example, to restrict the `:foo` option to only `"bar"` and `"baz"` strings:
|
884
1134
|
|
885
1135
|
```ruby
|
886
1136
|
option :foo do
|
887
1137
|
long "--foo string"
|
888
|
-
permit [
|
1138
|
+
permit %w[bar baz]
|
889
1139
|
end
|
890
1140
|
```
|
891
1141
|
|
892
|
-
|
1142
|
+
Given the following command line input:
|
893
1143
|
|
894
1144
|
```
|
895
1145
|
--foo bar
|
896
1146
|
```
|
897
1147
|
|
898
|
-
|
1148
|
+
This would result in:
|
899
1149
|
|
900
1150
|
```ruby
|
901
1151
|
params[:foo] # => "bar"
|
902
1152
|
```
|
903
1153
|
|
904
|
-
|
1154
|
+
Given not permitted value `qux` on the command line:
|
905
1155
|
|
906
1156
|
```
|
907
1157
|
--foo qux
|
908
1158
|
```
|
909
1159
|
|
910
|
-
|
1160
|
+
This would raise a `TTY::Option::UnpermittedArgument` error and
|
1161
|
+
make the [params](#27-params) invalid.
|
911
1162
|
|
912
|
-
|
1163
|
+
The parser checks permitted values after applying conversion first. Because
|
1164
|
+
of this, permit setting needs its values to be already of the correct type.
|
1165
|
+
|
1166
|
+
For example, given integer conversion, permitted values need to
|
1167
|
+
be integers as well:
|
913
1168
|
|
914
1169
|
```ruby
|
915
1170
|
option :foo do
|
916
1171
|
long "--foo int"
|
917
|
-
|
1172
|
+
convert :int
|
918
1173
|
permit [11, 12, 13]
|
919
1174
|
end
|
920
1175
|
```
|
921
1176
|
|
922
|
-
Then
|
1177
|
+
Then given not permitted integer:
|
923
1178
|
|
924
1179
|
```
|
925
1180
|
--foo 14
|
926
1181
|
```
|
927
1182
|
|
928
|
-
|
1183
|
+
This would invalidate `params` and collect the
|
1184
|
+
`TTY::Option::UnpermittedArgument` error.
|
929
1185
|
|
930
|
-
The
|
1186
|
+
The [help](#29-help) method displays permitted values in the
|
1187
|
+
parameter description.
|
1188
|
+
|
1189
|
+
For example, given the following option:
|
931
1190
|
|
932
1191
|
```ruby
|
933
1192
|
option :foo do
|
934
1193
|
short "-f"
|
935
1194
|
long "--foo string"
|
936
1195
|
permit %w[a b c d]
|
937
|
-
desc "
|
1196
|
+
desc "Option description"
|
938
1197
|
end
|
939
1198
|
```
|
940
1199
|
|
941
|
-
Then the
|
1200
|
+
Then the description for the option would be:
|
942
1201
|
|
943
1202
|
```
|
944
1203
|
Usage: foobar [OPTIONS]
|
945
1204
|
|
946
1205
|
Options:
|
947
|
-
-f, --foo string
|
1206
|
+
-f, --foo string Option description (permitted: a, b, c, d)
|
948
1207
|
```
|
949
1208
|
|
950
|
-
#### 2.5.
|
1209
|
+
#### 2.5.10 required
|
1210
|
+
|
1211
|
+
Parser only requires arguments to be present on the command line by default.
|
1212
|
+
Any other parameters like options, keywords and environment variables
|
1213
|
+
are optional.
|
951
1214
|
|
952
|
-
|
1215
|
+
Use the `required` setting to force parameter presence in command line input.
|
1216
|
+
|
1217
|
+
For example, given a required keyword and an optional one:
|
953
1218
|
|
954
1219
|
```ruby
|
955
1220
|
keyword :foo do
|
@@ -962,7 +1227,38 @@ keyword :bar do
|
|
962
1227
|
end
|
963
1228
|
```
|
964
1229
|
|
965
|
-
|
1230
|
+
And given the following command line input:
|
1231
|
+
|
1232
|
+
```
|
1233
|
+
foo=baz
|
1234
|
+
```
|
1235
|
+
|
1236
|
+
This would result in:
|
1237
|
+
|
1238
|
+
```ruby
|
1239
|
+
params[:foo] # => "baz"
|
1240
|
+
params[:bar] # => nil
|
1241
|
+
```
|
1242
|
+
|
1243
|
+
Given the following command line input without the `foo` keyword:
|
1244
|
+
|
1245
|
+
```
|
1246
|
+
bar=baz
|
1247
|
+
```
|
1248
|
+
|
1249
|
+
This would raise a `TTY::Option::MissingParameter` error.
|
1250
|
+
|
1251
|
+
Then printing [errors](#271-errors) summary would display the following
|
1252
|
+
error description:
|
1253
|
+
|
1254
|
+
```
|
1255
|
+
Error: keyword 'foo' must be provided
|
1256
|
+
```
|
1257
|
+
|
1258
|
+
The usage banner displays the required parameters first. Then surrounds any
|
1259
|
+
optional parameters in brackets.
|
1260
|
+
|
1261
|
+
The [help](#29-help) display for the above keywords would be:
|
966
1262
|
|
967
1263
|
```
|
968
1264
|
Usage: foobar FOO=FOO [BAR=BAR]
|
@@ -972,56 +1268,148 @@ Keywords:
|
|
972
1268
|
BAR=BAR Bar keyword description
|
973
1269
|
```
|
974
1270
|
|
975
|
-
|
1271
|
+
#### 2.5.11 short
|
1272
|
+
|
1273
|
+
Only [flag](#23-option) and [option](#23-option) parameters can use
|
1274
|
+
the `short` setting.
|
1275
|
+
|
1276
|
+
Use the `short` setting to define a short name for an option. By convention,
|
1277
|
+
a short name uses a single dash followed by a single alphanumeric character.
|
1278
|
+
|
1279
|
+
For example, to define the `-f` option:
|
1280
|
+
|
1281
|
+
```ruby
|
1282
|
+
option :foo do
|
1283
|
+
short "-f"
|
1284
|
+
end
|
1285
|
+
```
|
1286
|
+
|
1287
|
+
A short option can accept an argument. The argument can be either required
|
1288
|
+
or optional. To define a required argument, separate it from the option
|
1289
|
+
name with a space or an equal sign. It is possible to skip the space,
|
1290
|
+
but that would make the option description hard to read.
|
1291
|
+
|
1292
|
+
For the `:foo` option to accept a required integer argument:
|
1293
|
+
|
1294
|
+
```ruby
|
1295
|
+
option :foo do
|
1296
|
+
short "-f int"
|
1297
|
+
end
|
1298
|
+
```
|
1299
|
+
|
1300
|
+
These are all equivalent ways to define a short option with a required
|
1301
|
+
argument:
|
1302
|
+
|
1303
|
+
```ruby
|
1304
|
+
short "-f int"
|
1305
|
+
short "-f=int"
|
1306
|
+
short "-fint"
|
1307
|
+
```
|
1308
|
+
|
1309
|
+
To define an optional argument, surround it with square brackets. Like
|
1310
|
+
the required argument, separate it from the option name with a space
|
1311
|
+
or an equal sign. It is possible to skip the space, but that would
|
1312
|
+
make the option description hard to read.
|
1313
|
+
|
1314
|
+
For the `:foo` option to accept an optional integer argument:
|
1315
|
+
|
1316
|
+
```ruby
|
1317
|
+
option :foo do
|
1318
|
+
short "-f [int]"
|
1319
|
+
end
|
1320
|
+
```
|
1321
|
+
|
1322
|
+
These are all equivalent ways to define a short option with
|
1323
|
+
an optional argument:
|
1324
|
+
|
1325
|
+
```ruby
|
1326
|
+
short "-f [int]"
|
1327
|
+
short "-f=[int]"
|
1328
|
+
short "-f[int]"
|
1329
|
+
```
|
1330
|
+
|
1331
|
+
When specifying short and long option names, only define the argument
|
1332
|
+
for the long name.
|
1333
|
+
|
1334
|
+
For example, to define an option with short and long names that accepts
|
1335
|
+
a required integer argument:
|
1336
|
+
|
1337
|
+
```ruby
|
1338
|
+
option :foo do
|
1339
|
+
short "-f"
|
1340
|
+
long "--foo int"
|
1341
|
+
end
|
1342
|
+
```
|
1343
|
+
|
1344
|
+
Note that the parser performs no conversion of the argument. Use
|
1345
|
+
the [convert](#252-convert) setting to transform the argument type.
|
976
1346
|
|
977
|
-
#### 2.5.
|
1347
|
+
#### 2.5.12 validate
|
978
1348
|
|
979
|
-
Use the `validate` setting
|
1349
|
+
Use the `validate` setting to ensure that inputs match a validation rule.
|
1350
|
+
The rule can be a string, a regular expression or a `Proc` object.
|
980
1351
|
|
981
|
-
|
1352
|
+
For example, to ensure the `--foo` option only accepts digits:
|
982
1353
|
|
983
1354
|
```ruby
|
984
1355
|
option :foo do
|
985
|
-
long "--foo
|
1356
|
+
long "--foo int"
|
986
1357
|
validate "\d+"
|
987
1358
|
end
|
988
1359
|
```
|
989
1360
|
|
990
|
-
|
1361
|
+
Given the following command line input:
|
991
1362
|
|
992
1363
|
```
|
993
1364
|
--foo bar
|
994
1365
|
```
|
995
1366
|
|
996
|
-
|
1367
|
+
This would raise a `TTY::Option::InvalidArgument` error that would
|
1368
|
+
make `params` invalid.
|
1369
|
+
|
1370
|
+
Then printing [errors](#271-errors) summary would output:
|
997
1371
|
|
998
|
-
|
1372
|
+
```
|
1373
|
+
Error: value of `bar` fails validation for '--foo' option
|
1374
|
+
```
|
1375
|
+
|
1376
|
+
To define a validation rule as a `Proc` object that accepts
|
1377
|
+
an argument to check:
|
999
1378
|
|
1000
1379
|
```ruby
|
1001
1380
|
keyword :foo do
|
1002
|
-
arity one_or_more
|
1003
1381
|
convert :int
|
1004
1382
|
validate ->(val) { val < 12 }
|
1005
1383
|
end
|
1006
1384
|
```
|
1007
1385
|
|
1008
|
-
|
1386
|
+
The parser validates a value after applying conversion first. Because of
|
1387
|
+
this, the value inside a validation rule is already of the correct type.
|
1388
|
+
|
1389
|
+
Given the following command line input:
|
1009
1390
|
|
1010
1391
|
```
|
1011
|
-
foo=
|
1392
|
+
foo=13
|
1012
1393
|
```
|
1013
1394
|
|
1014
|
-
|
1395
|
+
This would raise a `TTY::Option::InvalidArgument` error and make
|
1396
|
+
`params` invalid.
|
1015
1397
|
|
1016
|
-
|
1398
|
+
Then using the [errors](#271-errors) summary would print the following error:
|
1399
|
+
|
1400
|
+
```
|
1401
|
+
Error: value of `13` fails validation for 'foo' keyword
|
1402
|
+
```
|
1017
1403
|
|
1018
|
-
|
1404
|
+
### 2.6 parse
|
1019
1405
|
|
1020
|
-
|
1406
|
+
Use the `parse` method to match command line inputs against defined parameters.
|
1021
1407
|
|
1022
|
-
|
1408
|
+
The `parse` method reads the input from the command line (aka `ARGV`) and
|
1409
|
+
the environment variables (aka `ENV`) by default. It also accepts inputs
|
1410
|
+
as an argument. This is useful when testing commands.
|
1023
1411
|
|
1024
|
-
|
1412
|
+
For example, given the following parameter definitions:
|
1025
1413
|
|
1026
1414
|
```ruby
|
1027
1415
|
argument :foo
|
@@ -1033,13 +1421,13 @@ keyword :baz
|
|
1033
1421
|
env :qux
|
1034
1422
|
```
|
1035
1423
|
|
1036
|
-
Then parsing the
|
1424
|
+
Then parsing the command line inputs:
|
1037
1425
|
|
1038
1426
|
```ruby
|
1039
1427
|
parse(%w[12 --bar baz=a QUX=b])
|
1040
1428
|
```
|
1041
1429
|
|
1042
|
-
|
1430
|
+
This would result in:
|
1043
1431
|
|
1044
1432
|
```ruby
|
1045
1433
|
params[:foo] # => "12"
|
@@ -1048,59 +1436,111 @@ params[:baz] # => "a"
|
|
1048
1436
|
params[:qux] # => "b"
|
1049
1437
|
```
|
1050
1438
|
|
1051
|
-
The
|
1439
|
+
The parser doesn't force any order for the parameters except for arguments.
|
1052
1440
|
|
1053
|
-
|
1441
|
+
For example, reordering inputs for the previous parameter definitions:
|
1054
1442
|
|
1443
|
+
```ruby
|
1444
|
+
parse(%w[12 QUX=b --bar baz=a])
|
1055
1445
|
```
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1446
|
+
|
1447
|
+
This would result in the same values:
|
1448
|
+
|
1449
|
+
```ruby
|
1450
|
+
params[:foo] # => "12"
|
1451
|
+
params[:bar] # => true
|
1452
|
+
params[:baz] # => "a"
|
1453
|
+
params[:qux] # => "b"
|
1454
|
+
```
|
1455
|
+
|
1456
|
+
The parser handles compact shorthand options that start with
|
1457
|
+
a single dash. These must be boolean options except for
|
1458
|
+
the last one that can accept an argument.
|
1459
|
+
|
1460
|
+
For example, passing three flags and an option with an argument to parse:
|
1461
|
+
|
1462
|
+
```
|
1463
|
+
parse(%w[-f -b -q -s 12])
|
1464
|
+
```
|
1465
|
+
|
1466
|
+
This is equivalent to parsing:
|
1467
|
+
|
1468
|
+
```
|
1469
|
+
parse(%w[-fbqs 12])
|
1470
|
+
```
|
1471
|
+
|
1472
|
+
Parameter parsing stops at the `--` terminator. The parser collects leftover
|
1473
|
+
inputs and makes them accessible with the [remaining](#272-remaining) method.
|
1474
|
+
|
1475
|
+
For example, given extra input after the terminator:
|
1476
|
+
|
1477
|
+
```ruby
|
1478
|
+
parse(%w[12 baz=a QUX=b -- --fum])
|
1059
1479
|
```
|
1060
1480
|
|
1061
|
-
|
1481
|
+
This would result in:
|
1482
|
+
|
1483
|
+
```ruby
|
1484
|
+
params[:foo] # => 12
|
1485
|
+
params[:bar] # => false
|
1486
|
+
params[:baz] # => "a"
|
1487
|
+
params[:qux] # => "b"
|
1488
|
+
params.remaining # => ["--fum"]
|
1489
|
+
```
|
1062
1490
|
|
1063
1491
|
#### 2.6.1 :raise_on_parse_error
|
1064
1492
|
|
1065
|
-
|
1493
|
+
The `parse` method doesn't raise any errors by default. Why? Displaying
|
1494
|
+
error backtraces in the terminal output may not be helpful for users.
|
1495
|
+
Instead, the parser collects any errors and exposes them through the
|
1496
|
+
[errors](#271-errors) method.
|
1066
1497
|
|
1067
|
-
|
1498
|
+
Use the `:raise_on_parse_error` keyword set to `true` to raise parsing errors:
|
1068
1499
|
|
1069
1500
|
```ruby
|
1070
1501
|
parse(raise_on_parse_error: true)
|
1071
1502
|
```
|
1072
1503
|
|
1073
|
-
|
1504
|
+
Parsing errors inherit from `TTY::Option::ParseError`.
|
1505
|
+
|
1506
|
+
For example, to catch parsing errors:
|
1074
1507
|
|
1075
1508
|
```ruby
|
1076
1509
|
begin
|
1077
1510
|
parse(raise_on_parse_error: true)
|
1078
1511
|
rescue TTY::Option::ParseError => err
|
1079
|
-
|
1512
|
+
...
|
1080
1513
|
end
|
1081
1514
|
```
|
1082
1515
|
|
1083
1516
|
#### 2.6.2 :check_invalid_params
|
1084
1517
|
|
1085
|
-
Users can provide any input, including parameters
|
1518
|
+
Users can provide any input, including parameters the parser
|
1519
|
+
didn't expect and define.
|
1086
1520
|
|
1087
|
-
|
1521
|
+
When the parser finds an unknown input on the command line, it raises
|
1522
|
+
a `TTY::Option::InvalidParameter` error and adds it to the
|
1523
|
+
[errors](#271-errors) array.
|
1088
1524
|
|
1089
|
-
|
1525
|
+
Use the `:check_invalid_params` keyword set to `false` to ignore unknown
|
1526
|
+
inputs during parsing:
|
1090
1527
|
|
1091
1528
|
```ruby
|
1092
1529
|
parse(check_invalid_params: false)
|
1093
1530
|
```
|
1094
1531
|
|
1095
|
-
This way
|
1532
|
+
This way, the parser will collect all the unrecognised inputs into the
|
1533
|
+
[remaining](#272-remaining) array.
|
1096
1534
|
|
1097
1535
|
### 2.7 params
|
1098
1536
|
|
1099
|
-
|
1537
|
+
All defined parameters are accessible from the `params` object.
|
1100
1538
|
|
1101
|
-
The `params` behaves like a hash with
|
1539
|
+
The `params` object behaves like a hash with indifferent access. It doesn't
|
1540
|
+
differentiate between arguments, keywords, options or environment variables.
|
1541
|
+
Because of that, each parameter needs to have a unique name.
|
1102
1542
|
|
1103
|
-
For example, given a command with all parameter
|
1543
|
+
For example, given a command with all parameter types:
|
1104
1544
|
|
1105
1545
|
```ruby
|
1106
1546
|
class Command
|
@@ -1110,7 +1550,9 @@ class Command
|
|
1110
1550
|
|
1111
1551
|
keyword :bar
|
1112
1552
|
|
1113
|
-
option :baz
|
1553
|
+
option :baz do
|
1554
|
+
long "--baz string"
|
1555
|
+
end
|
1114
1556
|
|
1115
1557
|
env :qux
|
1116
1558
|
|
@@ -1123,17 +1565,22 @@ class Command
|
|
1123
1565
|
end
|
1124
1566
|
```
|
1125
1567
|
|
1126
|
-
|
1568
|
+
And the following command line input:
|
1569
|
+
|
1570
|
+
```
|
1571
|
+
a bar=b --baz c QUX=d
|
1572
|
+
```
|
1573
|
+
|
1574
|
+
Then instantiating the command:
|
1127
1575
|
|
1128
1576
|
```ruby
|
1129
1577
|
cmd = Command.new
|
1130
|
-
cmd.parse
|
1131
1578
|
```
|
1132
1579
|
|
1133
|
-
|
1580
|
+
And parsing command line input:
|
1134
1581
|
|
1135
|
-
```
|
1136
|
-
|
1582
|
+
```ruby
|
1583
|
+
cmd.parse
|
1137
1584
|
```
|
1138
1585
|
|
1139
1586
|
And running the command:
|
@@ -1142,7 +1589,7 @@ And running the command:
|
|
1142
1589
|
cmd.run
|
1143
1590
|
```
|
1144
1591
|
|
1145
|
-
|
1592
|
+
This would result in the following output:
|
1146
1593
|
|
1147
1594
|
```
|
1148
1595
|
abcd
|
@@ -1150,21 +1597,34 @@ abcd
|
|
1150
1597
|
|
1151
1598
|
#### 2.7.1 errors
|
1152
1599
|
|
1153
|
-
|
1600
|
+
The `parse` method only raises configuration errors. The parsing errors are
|
1601
|
+
not raised by default. Instead, the `errors` method on the `params` object
|
1602
|
+
gives access to any parsing error.
|
1154
1603
|
|
1155
1604
|
```ruby
|
1156
|
-
params.errors
|
1157
|
-
|
1158
|
-
|
1605
|
+
params.errors # => TTY::Option::AggregateErrors
|
1606
|
+
```
|
1607
|
+
|
1608
|
+
The `errors` method returns an `TTY::Option::AggregateErrors` object that
|
1609
|
+
is an `Enumerable`.
|
1159
1610
|
|
1160
|
-
|
1611
|
+
For example, to iterate over all the errors:
|
1612
|
+
|
1613
|
+
```ruby
|
1614
|
+
params.errors.each do |error|
|
1615
|
+
...
|
1616
|
+
end
|
1617
|
+
```
|
1161
1618
|
|
1162
|
-
|
1619
|
+
The `TTY::Option::AggregateErrors` object has the following
|
1620
|
+
convenience methods:
|
1163
1621
|
|
1164
|
-
* `messages` -
|
1165
|
-
* `summary` - a string of
|
1622
|
+
* `messages` - an array of all error messages
|
1623
|
+
* `summary` - a string of formatted error messages ready to display
|
1624
|
+
in the terminal
|
1166
1625
|
|
1167
|
-
For example,
|
1626
|
+
For example, given an argument that needs to appear at least two times in
|
1627
|
+
the command line input:
|
1168
1628
|
|
1169
1629
|
```ruby
|
1170
1630
|
argument :foo do
|
@@ -1172,28 +1632,40 @@ argument :foo do
|
|
1172
1632
|
end
|
1173
1633
|
```
|
1174
1634
|
|
1175
|
-
And only one argument
|
1635
|
+
And parsing only one argument from the command line input:
|
1636
|
+
|
1637
|
+
```ruby
|
1638
|
+
parse(%w[12])
|
1639
|
+
```
|
1640
|
+
|
1641
|
+
Then printing errors summary:
|
1176
1642
|
|
1177
1643
|
```ruby
|
1178
1644
|
puts params.errors.summary
|
1179
|
-
|
1645
|
+
```
|
1180
1646
|
|
1181
|
-
|
1647
|
+
This would print the following error message:
|
1182
1648
|
|
1183
1649
|
```
|
1184
1650
|
Error: argument 'foo' should appear at least 2 times but appeared 1 time
|
1185
1651
|
```
|
1186
1652
|
|
1187
|
-
|
1653
|
+
Adding integer conversion to the previous example:
|
1188
1654
|
|
1189
1655
|
```ruby
|
1190
1656
|
argument :foo do
|
1191
1657
|
arity at_least(2)
|
1192
1658
|
convert :int
|
1193
1659
|
end
|
1194
|
-
|
1660
|
+
```
|
1661
|
+
|
1662
|
+
And given only one invalid argument to parse:
|
1663
|
+
|
1664
|
+
```ruby
|
1665
|
+
parse(%w[zzz])
|
1666
|
+
```
|
1195
1667
|
|
1196
|
-
|
1668
|
+
The summary would be:
|
1197
1669
|
|
1198
1670
|
```
|
1199
1671
|
Errors:
|
@@ -1201,133 +1673,163 @@ Errors:
|
|
1201
1673
|
2) Cannot convert value of `zzz` into 'int' type for 'foo' argument
|
1202
1674
|
```
|
1203
1675
|
|
1204
|
-
|
1676
|
+
Use the [:raise_on_parse_error](#261-raise_on_parse_error) keyword to raise
|
1677
|
+
parsing errors on invalid input.
|
1678
|
+
|
1679
|
+
Consider using the [tty-exit](https://github.com/piotrmurach/tty-exit) gem
|
1680
|
+
for more expressive exit code reporting.
|
1681
|
+
|
1682
|
+
For example, the `TTY::Exit` module provides the `exit_with` method:
|
1205
1683
|
|
1206
1684
|
```ruby
|
1207
|
-
|
1208
|
-
|
1685
|
+
class Command
|
1686
|
+
include TTY::Exit
|
1687
|
+
include TTY::Option
|
1209
1688
|
|
1210
|
-
|
1689
|
+
def run
|
1690
|
+
if params.errors.any?
|
1691
|
+
exit_with(:usage_error, params.errors.summary)
|
1692
|
+
end
|
1693
|
+
...
|
1694
|
+
end
|
1695
|
+
end
|
1696
|
+
```
|
1211
1697
|
|
1212
1698
|
#### 2.7.2 remaining
|
1213
1699
|
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1700
|
+
When the parser finds an unknown input on the command line, it raises
|
1701
|
+
a `TTY::Option::InvalidParameter` error and adds it to the
|
1702
|
+
[errors](#271-errors) array.
|
1217
1703
|
|
1218
|
-
|
1704
|
+
Use the [:check_invalid_params](#262-check_invalid_params) keyword
|
1705
|
+
set to `false` to ignore unknown inputs during parsing:
|
1219
1706
|
|
1220
1707
|
```ruby
|
1221
|
-
parse(check_invalid_params:
|
1708
|
+
parse(check_invalid_params: false)
|
1222
1709
|
```
|
1223
1710
|
|
1224
|
-
This way
|
1711
|
+
This way, the parser will collect all the unrecognised inputs
|
1712
|
+
into an array. The `remaining` method on the `params` gives access
|
1713
|
+
to all invalid inputs.
|
1225
1714
|
|
1226
|
-
For example,
|
1715
|
+
For example, given an unknown option to parse:
|
1227
1716
|
|
1228
1717
|
```ruby
|
1229
|
-
|
1718
|
+
parse(%w[--unknown])
|
1230
1719
|
```
|
1231
1720
|
|
1232
|
-
|
1233
|
-
|
1234
|
-
#### 2.7.3 valid?
|
1235
|
-
|
1236
|
-
Once parsing of the command-line input is done, you can check if all the conditions defined by the parameters are met with the `valid?` method.
|
1721
|
+
Then inspecting the `remaining` inputs:
|
1237
1722
|
|
1238
1723
|
```ruby
|
1239
|
-
params.
|
1724
|
+
params.remaining # => ["--unknown"]
|
1240
1725
|
```
|
1241
1726
|
|
1242
|
-
|
1727
|
+
The parser leaves any inputs after the `--` terminator alone. Instead,
|
1728
|
+
it collects them into the remaining array. This is useful when passing
|
1729
|
+
inputs over to other command line applications.
|
1730
|
+
|
1731
|
+
#### 2.7.3 valid?
|
1732
|
+
|
1733
|
+
Use the `valid?` method to check that command line inputs meet all
|
1734
|
+
validation rules.
|
1243
1735
|
|
1244
|
-
|
1736
|
+
The `valid?` method is available on the `params` object:
|
1245
1737
|
|
1246
1738
|
```ruby
|
1247
|
-
|
1248
|
-
# ... process params
|
1249
|
-
else
|
1250
|
-
puts params.errors.summary
|
1251
|
-
exit
|
1252
|
-
end
|
1739
|
+
params.valid? # => true
|
1253
1740
|
```
|
1254
1741
|
|
1255
|
-
|
1256
|
-
|
1257
|
-
The `TTY::Exit` module exposes the `exit_with` method and can be used like this:
|
1742
|
+
Use the [errors](#271-errors) method to check for any errors and not only
|
1743
|
+
validation rules:
|
1258
1744
|
|
1259
1745
|
```ruby
|
1260
|
-
|
1261
|
-
include TTY::Exit
|
1262
|
-
include TTY::Option
|
1263
|
-
|
1264
|
-
def run
|
1265
|
-
if params.valid?
|
1266
|
-
# ... process params
|
1267
|
-
else
|
1268
|
-
exit_with(:usage_error, params.errors.summary)
|
1269
|
-
end
|
1270
|
-
end
|
1271
|
-
end
|
1746
|
+
params.errors.any?
|
1272
1747
|
```
|
1273
1748
|
|
1274
1749
|
### 2.8 usage
|
1275
1750
|
|
1276
|
-
The `usage`
|
1751
|
+
The `usage` method accepts a block that configures the
|
1752
|
+
[help](#29-help) display.
|
1277
1753
|
|
1278
1754
|
#### 2.8.1 header
|
1279
1755
|
|
1280
|
-
|
1756
|
+
Use the `header` setting to display information above the banner.
|
1757
|
+
|
1758
|
+
For example, to explain a program's purpose:
|
1281
1759
|
|
1282
1760
|
```ruby
|
1283
1761
|
usage do
|
1284
|
-
header "A command
|
1762
|
+
header "A command line interface for foo service"
|
1285
1763
|
end
|
1286
1764
|
```
|
1287
1765
|
|
1288
|
-
|
1766
|
+
This would print:
|
1767
|
+
|
1768
|
+
```
|
1769
|
+
A command line interface for foo service
|
1770
|
+
|
1771
|
+
Usage: foo [OPTIONS]
|
1772
|
+
```
|
1773
|
+
|
1774
|
+
The `header` setting accepts many arguments, each representing a single
|
1775
|
+
paragraph. An empty string displays as a new line.
|
1776
|
+
|
1777
|
+
For example, to create an introduction with two paragraphs separated by
|
1778
|
+
an empty line:
|
1289
1779
|
|
1290
1780
|
```ruby
|
1291
1781
|
usage do
|
1292
|
-
header "A command
|
1782
|
+
header "A command line interface for foo service",
|
1293
1783
|
"",
|
1294
1784
|
"Access and retrieve data from foo service"
|
1295
1785
|
end
|
1296
1786
|
```
|
1297
1787
|
|
1298
|
-
|
1788
|
+
Or, add two paragraphs using the `header` setting twice:
|
1299
1789
|
|
1300
1790
|
```ruby
|
1301
1791
|
usage do
|
1302
|
-
header "A command
|
1792
|
+
header "A command line interface for foo service"
|
1303
1793
|
|
1304
1794
|
header "Access and retrieve data from foo service"
|
1305
1795
|
end
|
1306
1796
|
```
|
1307
1797
|
|
1798
|
+
Both would result in the same output:
|
1799
|
+
|
1800
|
+
```
|
1801
|
+
A command line interface for foo service
|
1802
|
+
|
1803
|
+
Access and retrieve data from foo service
|
1804
|
+
|
1805
|
+
Usage: foo [OPTIONS]
|
1806
|
+
```
|
1807
|
+
|
1308
1808
|
#### 2.8.2 program
|
1309
1809
|
|
1310
|
-
|
1810
|
+
The `program` setting uses an executable file name to generate a program
|
1811
|
+
name by default.
|
1311
1812
|
|
1312
|
-
|
1813
|
+
For example, to override the default name:
|
1313
1814
|
|
1314
1815
|
```ruby
|
1315
1816
|
usage do
|
1316
1817
|
program "custom-name"
|
1317
1818
|
end
|
1318
|
-
|
1819
|
+
```
|
1319
1820
|
|
1320
|
-
Then
|
1821
|
+
Then usage banner will display a custom program name:
|
1321
1822
|
|
1322
|
-
```
|
1823
|
+
```
|
1323
1824
|
Usage: custom-name
|
1324
1825
|
```
|
1325
1826
|
|
1326
1827
|
#### 2.8.3 command
|
1327
1828
|
|
1328
|
-
|
1829
|
+
The `command` setting uses a class name to generate a command name by default.
|
1830
|
+
It converts a class name into a dash case.
|
1329
1831
|
|
1330
|
-
For example,
|
1832
|
+
For example, given the following command class name:
|
1331
1833
|
|
1332
1834
|
```ruby
|
1333
1835
|
class NetworkCreate
|
@@ -1335,111 +1837,187 @@ class NetworkCreate
|
|
1335
1837
|
end
|
1336
1838
|
```
|
1337
1839
|
|
1338
|
-
The command name
|
1840
|
+
The command name would become `network-create`.
|
1841
|
+
|
1842
|
+
Use the `command` or `commands` setting to change the default command name.
|
1843
|
+
|
1844
|
+
For example, to change the previous class's default command name:
|
1339
1845
|
|
1340
1846
|
```ruby
|
1341
1847
|
class NetworkCreate
|
1342
1848
|
include TTY::Option
|
1343
1849
|
|
1344
1850
|
usage do
|
1345
|
-
|
1851
|
+
command "net-create"
|
1852
|
+
end
|
1853
|
+
end
|
1854
|
+
```
|
1855
|
+
|
1856
|
+
The usage banner would be:
|
1857
|
+
|
1858
|
+
```
|
1859
|
+
Usage: program net-create
|
1860
|
+
```
|
1861
|
+
|
1862
|
+
Use the `commands` setting for naming a subcommand.
|
1863
|
+
|
1864
|
+
For example, to add `create` command as a subcommand:
|
1865
|
+
|
1866
|
+
```ruby
|
1867
|
+
module Network
|
1868
|
+
class Create
|
1869
|
+
include TTY::Option
|
1870
|
+
|
1871
|
+
usage do
|
1872
|
+
commands "network", "create"
|
1873
|
+
end
|
1346
1874
|
end
|
1347
1875
|
end
|
1348
|
-
|
1876
|
+
```
|
1349
1877
|
|
1350
|
-
This will result in the following usage
|
1878
|
+
This will result in the following usage banner:
|
1351
1879
|
|
1352
1880
|
```
|
1353
1881
|
Usage: program network create
|
1354
1882
|
```
|
1355
1883
|
|
1356
|
-
|
1884
|
+
Use the `no_command` setting to skip having a command name:
|
1357
1885
|
|
1358
1886
|
```ruby
|
1359
1887
|
usage do
|
1360
1888
|
no_command
|
1361
1889
|
end
|
1362
|
-
|
1890
|
+
```
|
1363
1891
|
|
1364
|
-
|
1892
|
+
This will display only the program name:
|
1893
|
+
|
1894
|
+
```
|
1895
|
+
Usage: program
|
1896
|
+
```
|
1365
1897
|
|
1366
|
-
|
1898
|
+
#### 2.8.4 banner
|
1367
1899
|
|
1368
|
-
|
1900
|
+
The `banner` setting combines program, command and parameter names
|
1901
|
+
to generate usage banner.
|
1369
1902
|
|
1370
|
-
For example, given the following
|
1903
|
+
For example, given the following usage and parameter definitions:
|
1371
1904
|
|
1372
1905
|
```ruby
|
1373
1906
|
usage do
|
1374
|
-
program
|
1907
|
+
program "prog"
|
1375
1908
|
|
1376
|
-
command
|
1909
|
+
command "cmd"
|
1377
1910
|
end
|
1378
1911
|
|
1379
|
-
argument :
|
1912
|
+
argument :foo
|
1380
1913
|
|
1381
|
-
keyword :
|
1382
|
-
convert :uri
|
1383
|
-
end
|
1914
|
+
keyword :bar
|
1384
1915
|
|
1385
|
-
option :
|
1916
|
+
option :baz
|
1917
|
+
|
1918
|
+
env :qux
|
1386
1919
|
```
|
1387
1920
|
|
1388
|
-
|
1921
|
+
Then usage banner would print as follows:
|
1389
1922
|
|
1390
|
-
```
|
1391
|
-
Usage:
|
1923
|
+
```
|
1924
|
+
Usage: prog cmd [OPTIONS] [ENVIRONMENT] FOO [BAR=BAR]
|
1392
1925
|
```
|
1393
1926
|
|
1394
|
-
|
1927
|
+
The [help](#29-help) generator displays the usage banner first
|
1928
|
+
unless a [header](#281-header) is set.
|
1395
1929
|
|
1396
|
-
|
1930
|
+
Use the `banner` setting to create a custom usage display.
|
1931
|
+
|
1932
|
+
For example, to change the parameters format:
|
1397
1933
|
|
1398
1934
|
```ruby
|
1399
1935
|
usage do
|
1400
|
-
program "
|
1936
|
+
program "prog"
|
1937
|
+
|
1938
|
+
command "cmd"
|
1401
1939
|
|
1402
|
-
banner "Usage: #{program}
|
1940
|
+
banner "Usage: #{program} #{command.first} <opts> <envs> foo [bar=bar]"
|
1403
1941
|
end
|
1404
1942
|
```
|
1405
1943
|
|
1406
|
-
|
1944
|
+
This would display as:
|
1945
|
+
|
1946
|
+
```
|
1947
|
+
Usage: prog cmd <opts> <envs> foo [bar=bar]
|
1948
|
+
```
|
1949
|
+
|
1950
|
+
Use the [:param_display](#294-param_display) setting to change the banner
|
1951
|
+
parameters format.
|
1407
1952
|
|
1408
|
-
|
1953
|
+
#### 2.8.5 description
|
1409
1954
|
|
1410
|
-
|
1955
|
+
Use `description` or `desc` setting to display information right after
|
1956
|
+
the usage banner.
|
1957
|
+
|
1958
|
+
For example, to give extra information:
|
1411
1959
|
|
1412
1960
|
```ruby
|
1413
1961
|
usage do
|
1414
|
-
desc "
|
1962
|
+
desc "A description for foo service"
|
1415
1963
|
end
|
1416
1964
|
```
|
1417
1965
|
|
1418
|
-
This
|
1966
|
+
This would print:
|
1419
1967
|
|
1420
1968
|
```
|
1421
|
-
|
1422
|
-
|
1969
|
+
Usage: foo [OPTIONS]
|
1970
|
+
|
1971
|
+
A description for foo service
|
1972
|
+
```
|
1973
|
+
|
1974
|
+
The `desc` setting accepts many arguments, each representing a single
|
1975
|
+
paragraph. An empty string displays as a new line.
|
1976
|
+
|
1977
|
+
For example, to create a description with two paragraphs separated by
|
1978
|
+
an empty line:
|
1979
|
+
|
1980
|
+
```ruby
|
1981
|
+
usage do
|
1982
|
+
desc "A description for foo service",
|
1983
|
+
"",
|
1984
|
+
"Learn more about foo service\nby reading tutorials"
|
1985
|
+
end
|
1423
1986
|
```
|
1424
1987
|
|
1425
|
-
|
1988
|
+
Or, add two paragraphs using the `desc` setting twice:
|
1426
1989
|
|
1427
1990
|
```ruby
|
1428
1991
|
usage do
|
1429
|
-
desc "
|
1992
|
+
desc "A description for foo service",
|
1430
1993
|
|
1431
1994
|
desc <<~EOS
|
1432
|
-
|
1433
|
-
|
1995
|
+
Learn more about foo service
|
1996
|
+
by reading tutorials
|
1434
1997
|
EOS
|
1435
1998
|
end
|
1436
1999
|
```
|
1437
2000
|
|
1438
|
-
|
2001
|
+
Both would result in the same output:
|
1439
2002
|
|
1440
|
-
|
2003
|
+
```
|
2004
|
+
Usage: foo [OPTIONS]
|
2005
|
+
|
2006
|
+
A description for foo service
|
2007
|
+
|
2008
|
+
Learn more about foo service
|
2009
|
+
by reading tutorials
|
2010
|
+
```
|
1441
2011
|
|
1442
|
-
|
2012
|
+
#### 2.8.6 example
|
2013
|
+
|
2014
|
+
Use the `example` or `examples` setting to add a usage examples section
|
2015
|
+
to the help display.
|
2016
|
+
|
2017
|
+
The `example` setting accepts many arguments, each representing a single
|
2018
|
+
paragraph. An empty string displays as a new line.
|
2019
|
+
|
2020
|
+
For instance, to create an example usage displayed on two lines:
|
1443
2021
|
|
1444
2022
|
```ruby
|
1445
2023
|
usage do
|
@@ -1456,7 +2034,8 @@ Examples:
|
|
1456
2034
|
$ foo bar
|
1457
2035
|
```
|
1458
2036
|
|
1459
|
-
|
2037
|
+
Or, add two examples using the `example` setting twice:
|
2038
|
+
|
1460
2039
|
|
1461
2040
|
```ruby
|
1462
2041
|
usage do
|
@@ -1470,7 +2049,7 @@ usage do
|
|
1470
2049
|
end
|
1471
2050
|
```
|
1472
2051
|
|
1473
|
-
The
|
2052
|
+
The examples section would display the following:
|
1474
2053
|
|
1475
2054
|
```
|
1476
2055
|
Examples:
|
@@ -1483,39 +2062,68 @@ Examples:
|
|
1483
2062
|
|
1484
2063
|
#### 2.8.7 footer
|
1485
2064
|
|
1486
|
-
|
2065
|
+
Use the `footer` setting to display text after all information
|
2066
|
+
in the usage help.
|
2067
|
+
|
2068
|
+
For example, to reference further help:
|
1487
2069
|
|
1488
2070
|
```ruby
|
1489
2071
|
usage do
|
1490
|
-
footer "Run a command followed by --help to see more info"
|
2072
|
+
footer "Run a command followed by --help to see more info."
|
1491
2073
|
end
|
1492
2074
|
```
|
1493
2075
|
|
1494
|
-
|
2076
|
+
This would print as follows:
|
2077
|
+
|
2078
|
+
```
|
2079
|
+
Usage: foo [OPTIONS]
|
2080
|
+
|
2081
|
+
Run a command followed by --help to see more info.
|
2082
|
+
```
|
2083
|
+
|
2084
|
+
The `footer` setting accepts many arguments, each representing a single
|
2085
|
+
paragraph. An empty string displays as a new line.
|
2086
|
+
|
2087
|
+
For example, to display further help with two paragraphs separated by
|
2088
|
+
an empty line:
|
1495
2089
|
|
1496
2090
|
```ruby
|
1497
2091
|
usage do
|
1498
|
-
footer "Run a command followed by --help to see more info",
|
2092
|
+
footer "Run a command followed by --help to see more info.",
|
1499
2093
|
"",
|
1500
|
-
"
|
2094
|
+
"Report bugs to the mailing list."
|
1501
2095
|
end
|
1502
2096
|
```
|
1503
2097
|
|
1504
|
-
|
2098
|
+
Or, add two paragraphs using the `footer` setting twice:
|
1505
2099
|
|
1506
2100
|
```ruby
|
1507
2101
|
usage do
|
1508
|
-
footer "Run a command followed by --help to see more info"
|
2102
|
+
footer "Run a command followed by --help to see more info."
|
1509
2103
|
|
1510
|
-
footer "
|
2104
|
+
footer "Report bugs to the mailing list."
|
1511
2105
|
end
|
1512
2106
|
```
|
1513
2107
|
|
2108
|
+
Both would result in the same output:
|
2109
|
+
|
2110
|
+
```
|
2111
|
+
Usage: foo [OPTIONS]
|
2112
|
+
|
2113
|
+
Run a command followed by --help to see more info.
|
2114
|
+
|
2115
|
+
Report bugs to the mailing list.
|
2116
|
+
```
|
2117
|
+
|
1514
2118
|
### 2.9 help
|
1515
2119
|
|
1516
|
-
|
2120
|
+
Use the `help` method to generate usage information about defined parameters.
|
2121
|
+
|
2122
|
+
The [usage](#28-usage) describes how to add different sections to the
|
2123
|
+
help display.
|
1517
2124
|
|
1518
|
-
|
2125
|
+
For example, given the following command class definition with
|
2126
|
+
a `run` method that prints help:
|
1519
2127
|
|
1520
2128
|
|
1521
2129
|
```ruby
|
@@ -1523,16 +2131,18 @@ class Command
|
|
1523
2131
|
include TTY::Option
|
1524
2132
|
|
1525
2133
|
usage do
|
1526
|
-
program "foobar"
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
2134
|
+
program "foobar"
|
2135
|
+
no_command
|
2136
|
+
header "foobar CLI"
|
2137
|
+
desc "CLI description"
|
2138
|
+
example "Example usage"
|
2139
|
+
footer "Run --help to see more info"
|
1531
2140
|
end
|
1532
2141
|
|
1533
|
-
argument :
|
1534
|
-
keyword :
|
1535
|
-
|
2142
|
+
argument :foo, desc: "Argument description"
|
2143
|
+
keyword :bar, desc: "Keyword description"
|
2144
|
+
option :baz, desc: "Option description"
|
2145
|
+
env :qux, desc: "Environment description"
|
1536
2146
|
|
1537
2147
|
flag :help do
|
1538
2148
|
short "-h"
|
@@ -1557,38 +2167,42 @@ cmd.parse(%w[--help])
|
|
1557
2167
|
cmd.run
|
1558
2168
|
```
|
1559
2169
|
|
1560
|
-
|
2170
|
+
This would result in the following help display:
|
1561
2171
|
|
1562
2172
|
```
|
1563
2173
|
foobar CLI
|
1564
2174
|
|
1565
|
-
Usage: foobar [OPTIONS] [ENVIRONMENT]
|
2175
|
+
Usage: foobar [OPTIONS] [ENVIRONMENT] FOO [BAR=BAR]
|
1566
2176
|
|
1567
|
-
|
2177
|
+
CLI description
|
1568
2178
|
|
1569
2179
|
Arguments:
|
1570
|
-
|
2180
|
+
FOO Argument description
|
1571
2181
|
|
1572
2182
|
Keywords:
|
1573
|
-
|
2183
|
+
BAR=BAR Keyword description
|
1574
2184
|
|
1575
2185
|
Options:
|
1576
|
-
|
2186
|
+
--baz Option description
|
2187
|
+
-h, --help Print usage
|
1577
2188
|
|
1578
|
-
|
1579
|
-
|
2189
|
+
Environment:
|
2190
|
+
QUX Environment description
|
1580
2191
|
|
1581
2192
|
Examples:
|
1582
|
-
|
2193
|
+
Example usage
|
1583
2194
|
|
1584
2195
|
Run --help to see more info
|
1585
2196
|
```
|
1586
2197
|
|
1587
2198
|
#### 2.9.1 sections
|
1588
2199
|
|
1589
|
-
|
2200
|
+
Pass a block to the `help` method to change generated usage information.
|
2201
|
+
The block accepts a single argument, a `TTY::Option::Sections` object.
|
2202
|
+
This object provides hash-like access to each named part of the help display.
|
1590
2203
|
|
1591
|
-
The following are the names
|
2204
|
+
The following are the names of all supported sections ordered by help display
|
2205
|
+
from top to bottom:
|
1592
2206
|
|
1593
2207
|
* `:header`
|
1594
2208
|
* `:banner`
|
@@ -1597,41 +2211,94 @@ The following are the names for all supported sections:
|
|
1597
2211
|
* `:keywords`
|
1598
2212
|
* `:options`
|
1599
2213
|
* `:environments`
|
1600
|
-
* `:
|
2214
|
+
* `:examples`
|
1601
2215
|
* `:footer`
|
1602
2216
|
|
1603
|
-
|
2217
|
+
Accessing a named section returns a `TTY::Option::Section` object
|
2218
|
+
with `name` and `content` methods.
|
1604
2219
|
|
1605
|
-
For example, to
|
2220
|
+
For example, to access the arguments section content:
|
1606
2221
|
|
1607
2222
|
```ruby
|
1608
2223
|
help do |sections|
|
1609
|
-
sections.
|
2224
|
+
sections[:arguments].content # => "\nArguments:\n FOO Argument description"
|
1610
2225
|
end
|
1611
2226
|
```
|
1612
2227
|
|
1613
|
-
To
|
2228
|
+
To add a new section, use the `add_after` and `add_before` methods. These
|
2229
|
+
methods accept three arguments. The first argument is the section name
|
2230
|
+
to add after or before. The second argument is a new section name,
|
2231
|
+
and the last is content to add.
|
2232
|
+
|
2233
|
+
For example, to insert a new commands section after the description:
|
1614
2234
|
|
1615
2235
|
```ruby
|
1616
2236
|
help do |sections|
|
1617
|
-
sections.add_after :
|
1618
|
-
|
2237
|
+
sections.add_after :description, :commands, <<~EOS.chomp
|
2238
|
+
|
2239
|
+
Commands:
|
2240
|
+
create Create command description
|
2241
|
+
delete Delete command description
|
2242
|
+
EOS
|
2243
|
+
end
|
2244
|
+
```
|
2245
|
+
|
2246
|
+
Given the following usage and parameter definition:
|
2247
|
+
|
2248
|
+
```ruby
|
2249
|
+
usage do
|
2250
|
+
program "prog"
|
2251
|
+
|
2252
|
+
command "cmd"
|
2253
|
+
|
2254
|
+
desc "Program description"
|
2255
|
+
end
|
2256
|
+
|
2257
|
+
argument :foo do
|
2258
|
+
desc "Foo argument description"
|
1619
2259
|
end
|
1620
2260
|
```
|
1621
2261
|
|
1622
|
-
|
2262
|
+
The help display would be:
|
2263
|
+
|
2264
|
+
```
|
2265
|
+
Usage: prog cmd FOO
|
2266
|
+
|
2267
|
+
Program description
|
2268
|
+
|
2269
|
+
Commands:
|
2270
|
+
create Create command description
|
2271
|
+
delete Delete command description
|
2272
|
+
|
2273
|
+
Arguments:
|
2274
|
+
FOO Argument description
|
2275
|
+
```
|
2276
|
+
|
2277
|
+
Use `delete` and `replace` methods to change existing sections.
|
2278
|
+
|
2279
|
+
For example, to remove a header section:
|
1623
2280
|
|
1624
2281
|
```ruby
|
1625
2282
|
help do |sections|
|
1626
|
-
sections.
|
2283
|
+
sections.delete :header
|
2284
|
+
end
|
2285
|
+
```
|
2286
|
+
|
2287
|
+
Or, to replace the content of a footer section:
|
2288
|
+
|
2289
|
+
```ruby
|
2290
|
+
help do |sections|
|
2291
|
+
sections.replace :footer, "\nReport bugs to the mailing list."
|
1627
2292
|
end
|
1628
2293
|
```
|
1629
2294
|
|
1630
2295
|
#### 2.9.2 :indent
|
1631
2296
|
|
1632
|
-
|
2297
|
+
The help output has no indentation except for displaying parameters by default.
|
2298
|
+
|
2299
|
+
Use the `:indent` keyword to change the indentation of the help display.
|
1633
2300
|
|
1634
|
-
|
2301
|
+
For example, to indent help display by two spaces:
|
1635
2302
|
|
1636
2303
|
```ruby
|
1637
2304
|
help(indent: 2)
|
@@ -1639,58 +2306,91 @@ help(indent: 2)
|
|
1639
2306
|
|
1640
2307
|
#### 2.9.3 :order
|
1641
2308
|
|
1642
|
-
|
2309
|
+
The help generator orders parameters alphabetically within
|
2310
|
+
each section by default.
|
2311
|
+
|
2312
|
+
Use the `:order` keyword to change the default ordering.
|
2313
|
+
|
2314
|
+
The `:order` expects a `Proc` object as a value. The `Proc` accepts
|
2315
|
+
a single argument, an array of parameters within a section.
|
1643
2316
|
|
1644
|
-
|
2317
|
+
For example, to preserve the parameter definition order:
|
1645
2318
|
|
1646
2319
|
```ruby
|
1647
2320
|
help(order: ->(params) { params })
|
1648
|
-
|
2321
|
+
```
|
1649
2322
|
|
1650
2323
|
#### 2.9.4 :param_display
|
1651
2324
|
|
1652
|
-
|
2325
|
+
The usage banner displays positional and keyword arguments
|
2326
|
+
in uppercase letters by default.
|
1653
2327
|
|
1654
|
-
For example, given the following parameter
|
2328
|
+
For example, given the following parameter definitions:
|
1655
2329
|
|
1656
2330
|
```ruby
|
1657
|
-
|
2331
|
+
usage do
|
2332
|
+
program "prog"
|
2333
|
+
end
|
1658
2334
|
|
1659
|
-
argument :foo
|
2335
|
+
argument :foo, desc: "Argument description"
|
1660
2336
|
|
1661
|
-
keyword :bar
|
1662
|
-
required
|
1663
|
-
convert :uri
|
1664
|
-
end
|
2337
|
+
keyword :bar, desc: "Keyword description"
|
1665
2338
|
|
1666
|
-
option :baz
|
2339
|
+
option :baz, desc: "Option description"
|
2340
|
+
|
2341
|
+
env :qux, desc: "Environment description"
|
1667
2342
|
```
|
1668
2343
|
|
1669
|
-
The banner
|
2344
|
+
The usage banner would print as follows:
|
1670
2345
|
|
1671
|
-
```
|
1672
|
-
Usage:
|
2346
|
+
```
|
2347
|
+
Usage: prog [OPTIONS] [ENVIRONMENT] FOO [BAR=BAR]
|
1673
2348
|
```
|
1674
2349
|
|
1675
|
-
|
2350
|
+
Use the `:param_display` keyword to change the banner parameter formatting.
|
1676
2351
|
|
1677
|
-
|
2352
|
+
The `:param_display` expects a `Proc` object as a value. The `Proc` accepts
|
2353
|
+
a single argument, a parameter name within a section.
|
2354
|
+
|
2355
|
+
For example, to lowercase and surround parameters with `<` and `>` brackets:
|
1678
2356
|
|
1679
2357
|
```ruby
|
1680
|
-
help(param_display: ->(
|
2358
|
+
help(param_display: ->(param) { "<#{param.downcase}>" })
|
1681
2359
|
```
|
1682
2360
|
|
1683
|
-
This
|
2361
|
+
This would result in the following usage banner and parameter sections:
|
1684
2362
|
|
1685
2363
|
```
|
1686
|
-
Usage:
|
2364
|
+
Usage: prog [<options>] [<environment>] <foo> [<bar>=<bar>]
|
2365
|
+
|
2366
|
+
Arguments:
|
2367
|
+
<foo> Argument description
|
2368
|
+
|
2369
|
+
Keywords:
|
2370
|
+
<bar>=<bar> Keyword description
|
2371
|
+
|
2372
|
+
Options:
|
2373
|
+
--baz Option description
|
2374
|
+
|
2375
|
+
Environment:
|
2376
|
+
QUX Environment description
|
1687
2377
|
```
|
1688
2378
|
|
1689
2379
|
#### 2.9.5 :width
|
1690
2380
|
|
1691
|
-
|
2381
|
+
The help generator wraps content at the width of `80` columns by default.
|
2382
|
+
|
2383
|
+
Use the `:width` keyword to change it, for example, to `120` columns:
|
2384
|
+
|
2385
|
+
```ruby
|
2386
|
+
help(width: 120)
|
2387
|
+
```
|
2388
|
+
|
2389
|
+
Use the [tty-screen](https://github.com/piotrmurach/tty-screen) gem
|
2390
|
+
to change the help display based on terminal width.
|
1692
2391
|
|
1693
|
-
For example, to
|
2392
|
+
For example, to expand the help display to the full width of
|
2393
|
+
the terminal window:
|
1694
2394
|
|
1695
2395
|
```ruby
|
1696
2396
|
help(width: TTY::Screen.width)
|
@@ -1698,21 +2398,33 @@ help(width: TTY::Screen.width)
|
|
1698
2398
|
|
1699
2399
|
## Development
|
1700
2400
|
|
1701
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
2401
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
2402
|
+
Then, run `rake spec` to run the tests. You can also run `bin/console`
|
2403
|
+
for an interactive prompt that will allow you to experiment.
|
1702
2404
|
|
1703
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
2405
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
2406
|
+
To release a new version, update the version number in `version.rb`, and then
|
2407
|
+
run `bundle exec rake release`, which will create a git tag for the version,
|
2408
|
+
push git commits and tags, and push the `.gem` file to
|
2409
|
+
[rubygems.org](https://rubygems.org).
|
1704
2410
|
|
1705
2411
|
## Contributing
|
1706
2412
|
|
1707
|
-
Bug reports and pull requests are welcome on GitHub at
|
2413
|
+
Bug reports and pull requests are welcome on GitHub at
|
2414
|
+
https://github.com/piotrmurach/tty-option. This project is intended to be
|
2415
|
+
a safe, welcoming space for collaboration, and contributors are expected
|
2416
|
+
to adhere to the [code of conduct](https://github.com/piotrmurach/tty-option/blob/master/CODE_OF_CONDUCT.md).
|
1708
2417
|
|
1709
2418
|
## License
|
1710
2419
|
|
1711
|
-
The gem is available as open source under the terms of the
|
2420
|
+
The gem is available as open source under the terms of the
|
2421
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
1712
2422
|
|
1713
2423
|
## Code of Conduct
|
1714
2424
|
|
1715
|
-
Everyone interacting in the TTY::Option project's codebases, issue trackers,
|
2425
|
+
Everyone interacting in the TTY::Option project's codebases, issue trackers,
|
2426
|
+
chat rooms and mailing lists is expected to follow the
|
2427
|
+
[code of conduct](https://github.com/piotrmurach/tty-option/blob/master/CODE_OF_CONDUCT.md).
|
1716
2428
|
|
1717
2429
|
## Copyright
|
1718
2430
|
|