Skip to content

Commit 0e2adab

Browse files
authored
Make Option by default a string option. (#26)
Fixes #13.
1 parent 7240ecd commit 0e2adab

File tree

8 files changed

+138
-116
lines changed

8 files changed

+138
-116
lines changed

examples/main.toit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ create_device_command -> cli.Command:
122122
last used device is used.
123123
"""
124124
--options=[
125-
cli.OptionString "device" --short_name="d"
125+
cli.Option "device" --short_name="d"
126126
--short_help="The device to operate on."
127127
]
128128
device_cmd.add create_reset_command

main.toit

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ main args:
1313
--short_name="f"
1414
--short_help="Output format"
1515
--default="long",
16-
arguments.OptionString "global" --short_help="global required flag" --required,
16+
arguments.Option "global" --short_help="global required flag" --required,
1717
]
1818
--examples=[
1919
arguments.Example "Example of subcommand"
@@ -29,13 +29,13 @@ main args:
2929
Multiple lines.
3030
"""
3131
--options=[
32-
arguments.OptionString "other" --short_help="other option",
32+
arguments.Option "other" --short_help="other option",
3333
arguments.OptionInt "other_int" --short_help="other int option\ntwo lines" --default=42,
3434
arguments.OptionInt "other_int2" --required --multi,
3535
]
3636
--rest=[
37-
arguments.OptionString "input" --short_help="Input file" --required --type="file",
38-
arguments.OptionString "output" --short_help="Output file" --multi --required --type="file",
37+
arguments.Option "input" --short_help="Input file" --required --type="file",
38+
arguments.Option "output" --short_help="Output file" --multi --required --type="file",
3939
]
4040
--examples=[
4141
arguments.Example "Use a long output format"
@@ -47,8 +47,8 @@ main args:
4747
--run=:: | parsed | run_other parsed
4848
--short_help="""Some short help for the other2 command.\nTwo lines."""
4949
--rest=[
50-
arguments.OptionString "input" --short_help="Input file" --required --type="file",
51-
arguments.OptionString "output" --short_help="Output file" --multi --required --type="file",
50+
arguments.Option "input" --short_help="Input file" --required --type="file",
51+
arguments.Option "output" --short_help="Output file" --multi --required --type="file",
5252
arguments.Flag "flag_rest"
5353
]
5454
--examples=[

src/cli.toit

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,32 @@ abstract class Option:
251251
is_multi/bool
252252
should_split_commas/bool
253253

254+
/** An alias for $OptionString. */
255+
constructor name/string
256+
--default/string?=null
257+
--type/string="string"
258+
--short_name/string?=null
259+
--short_help/string?=null
260+
--required/bool=false
261+
--hidden/bool=false
262+
--multi/bool=false
263+
--split_commas/bool=false:
264+
return OptionString name
265+
--default=default
266+
--type=type
267+
--short_name=short_name
268+
--short_help=short_help
269+
--required=required
270+
--hidden=hidden
271+
--multi=multi
272+
--split_commas=split_commas
273+
254274
/**
255275
Creates an option with the given $name.
256276
277+
This constructor is intended to be called from subclasses. Also see $Option.constructor
278+
which is an alias for the $OptionString constructor.
279+
257280
The $name sets the name of the option. It must be unique among all options of a command.
258281
It is also used to extract the parsed value from the $Parsed object. For multi-word
259282
options kebab case ('foo-bar') is recommended. The constructor automatically converts
@@ -275,9 +298,8 @@ abstract class Option:
275298
276299
If $split_commas is true, then $multi must be true too. Values given to this option are then
277300
split on commas. For example, `--option a,b,c` will result in the list `["a", "b", "c"]`.
278-
279301
*/
280-
constructor .name --.short_name --.short_help --required --hidden --multi --split_commas:
302+
constructor.from_subclass .name --.short_name --.short_help --required --hidden --multi --split_commas:
281303
name = to_kebab name
282304
is_required = required
283305
is_hidden = hidden
@@ -355,7 +377,7 @@ class OptionString extends Option:
355377
--split_commas/bool=false:
356378
if multi and default: throw "Multi option can't have default value."
357379
if required and default: throw "Option can't have default value and be required."
358-
super name --short_name=short_name --short_help=short_help \
380+
super.from_subclass name --short_name=short_name --short_help=short_help \
359381
--required=required --hidden=hidden --multi=multi \
360382
--split_commas=split_commas
361383

@@ -398,7 +420,7 @@ class OptionEnum extends Option:
398420
--split_commas/bool=false:
399421
if multi and default: throw "Multi option can't have default value."
400422
if required and default: throw "Option can't have default value and be required."
401-
super name --short_name=short_name --short_help=short_help \
423+
super.from_subclass name --short_name=short_name --short_help=short_help \
402424
--required=required --hidden=hidden --multi=multi \
403425
--split_commas=split_commas
404426
if default and not values.contains default:
@@ -439,7 +461,7 @@ class OptionInt extends Option:
439461
--split_commas/bool=false:
440462
if multi and default: throw "Multi option can't have default value."
441463
if required and default: throw "Option can't have default value and be required."
442-
super name --short_name=short_name --short_help=short_help \
464+
super.from_subclass name --short_name=short_name --short_help=short_help \
443465
--required=required --hidden=hidden --multi=multi \
444466
--split_commas=split_commas
445467

@@ -478,7 +500,7 @@ class Flag extends Option:
478500
--multi/bool=false:
479501
if multi and default != null: throw "Multi option can't have default value."
480502
if required and default != null: throw "Option can't have default value and be required."
481-
super name --short_name=short_name --short_help=short_help \
503+
super.from_subclass name --short_name=short_name --short_help=short_help \
482504
--required=required --hidden=hidden --multi=multi --no-split_commas
483505

484506
type -> string:

tests/check_test.toit

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,37 @@ missing_run:
3939
ambiguous_option:
4040
root := cli.Command "root"
4141
--options=[
42-
cli.OptionString "foo" --short_name="a",
43-
cli.OptionString "foo" --short_name="b",
42+
cli.Option "foo" --short_name="a",
43+
cli.Option "foo" --short_name="b",
4444
]
4545
--run=(:: null)
4646

4747
expect_throw "Ambiguous option of 'root': --foo.": root.check --invoked_command="root"
4848

4949
root = cli.Command "root"
5050
--options=[
51-
cli.OptionString "foo" --short_name="a",
52-
cli.OptionString "bar" --short_name="a",
51+
cli.Option "foo" --short_name="a",
52+
cli.Option "bar" --short_name="a",
5353
]
5454
--run=(:: null)
5555
expect_throw "Ambiguous option of 'root': -a.": root.check --invoked_command="root"
5656

5757
root = cli.Command "root"
5858
--options=[
59-
cli.OptionString "foo" --short_name="a",
60-
cli.OptionString "bar" --short_name="ab",
59+
cli.Option "foo" --short_name="a",
60+
cli.Option "bar" --short_name="ab",
6161
]
6262
--run=(:: null)
6363
expect_throw "Ambiguous option of 'root': -ab.": root.check --invoked_command="root"
6464

6565
root = cli.Command "root"
6666
--options=[
67-
cli.OptionString "foo" --short_name="a",
67+
cli.Option "foo" --short_name="a",
6868
]
6969

7070
sub := cli.Command "sub"
7171
--options=[
72-
cli.OptionString "foo" --short_name="b",
72+
cli.Option "foo" --short_name="b",
7373
]
7474
--run=(:: null)
7575
root.add sub
@@ -78,11 +78,11 @@ ambiguous_option:
7878

7979
root = cli.Command "root"
8080
--options=[
81-
cli.OptionString "foo" --short_name="a",
81+
cli.Option "foo" --short_name="a",
8282
]
8383
sub = cli.Command "sub"
8484
--options=[
85-
cli.OptionString "bar" --short_name="a",
85+
cli.Option "bar" --short_name="a",
8686
]
8787
--run=(:: null)
8888
root.add sub
@@ -91,11 +91,11 @@ ambiguous_option:
9191

9292
root = cli.Command "root"
9393
--options=[
94-
cli.OptionString "foo" --short_name="a",
94+
cli.Option "foo" --short_name="a",
9595
]
9696
sub = cli.Command "sub"
9797
--options=[
98-
cli.OptionString "bar" --short_name="ab",
98+
cli.Option "bar" --short_name="ab",
9999
]
100100
--run=(:: null)
101101
root.add sub
@@ -104,11 +104,11 @@ ambiguous_option:
104104

105105
root = cli.Command "root"
106106
--options=[
107-
cli.OptionString "machine_32" --short_name="m32",
107+
cli.Option "machine_32" --short_name="m32",
108108
]
109109
sub = cli.Command "sub"
110110
--options=[
111-
cli.OptionString "machine_64" --short_name="m64",
111+
cli.Option "machine_64" --short_name="m64",
112112
]
113113
--run=(:: null)
114114
root.add sub
@@ -117,13 +117,13 @@ ambiguous_option:
117117
root = cli.Command "root"
118118
sub1 := cli.Command "sub1"
119119
--options=[
120-
cli.OptionString "foo" --short_name="a",
120+
cli.Option "foo" --short_name="a",
121121
]
122122
--run=(:: null)
123123
root.add sub1
124124
sub2 := cli.Command "sub2"
125125
--options=[
126-
cli.OptionString "foo" --short_name="a",
126+
cli.Option "foo" --short_name="a",
127127
]
128128
--run=(:: null)
129129
root.add sub2
@@ -144,7 +144,7 @@ ambiguous_command:
144144
rest_and_command:
145145
root := cli.Command "root"
146146
--rest=[
147-
cli.OptionString "rest" --multi,
147+
cli.Option "rest" --multi,
148148
]
149149
sub := cli.Command "sub"
150150
--run=(:: null)
@@ -154,7 +154,7 @@ rest_and_command:
154154
expect_throw "Cannot have both subcommands and rest arguments.":
155155
root = cli.Command "root"
156156
--rest=[
157-
cli.OptionString "rest" --multi,
157+
cli.Option "rest" --multi,
158158
]
159159
--subcommands=[
160160
cli.Command "sub" --run=(:: null),
@@ -179,47 +179,47 @@ run_and_command:
179179
rest:
180180
root := cli.Command "root"
181181
--rest=[
182-
cli.OptionString "rest" --multi,
183-
cli.OptionString "other",
182+
cli.Option "rest" --multi,
183+
cli.Option "other",
184184
]
185185
--run=(:: null)
186186
expect_throw "Multi-option 'rest' of 'root' must be the last rest argument.":
187187
root.check --invoked_command="root"
188188

189189
root = cli.Command "root"
190190
--rest=[
191-
cli.OptionString "foo",
192-
cli.OptionString "bar" --required,
191+
cli.Option "foo",
192+
cli.Option "bar" --required,
193193
]
194194
--run=(:: null)
195195
expect_throw "Required rest argument 'bar' of 'root' cannot follow optional rest argument.":
196196
root.check --invoked_command="root"
197197

198198
root = cli.Command "root"
199199
--options=[
200-
cli.OptionString "foo",
200+
cli.Option "foo",
201201
]
202202
--rest=[
203-
cli.OptionString "foo",
203+
cli.Option "foo",
204204
]
205205
--run=(:: null)
206206
expect_throw "Rest name 'foo' of 'root' already used.": root.check --invoked_command="root"
207207

208208
root = cli.Command "root"
209209
--rest=[
210-
cli.OptionString "foo",
211-
cli.OptionString "foo",
210+
cli.Option "foo",
211+
cli.Option "foo",
212212
]
213213
--run=(:: null)
214214
expect_throw "Rest name 'foo' of 'root' already used.": root.check --invoked_command="root"
215215

216216
root = cli.Command "root"
217217
--options=[
218-
cli.OptionString "foo",
218+
cli.Option "foo",
219219
]
220220
sub := cli.Command "sub"
221221
--rest=[
222-
cli.OptionString "foo",
222+
cli.Option "foo",
223223
]
224224
--run=(:: null)
225225
root.add sub
@@ -229,7 +229,7 @@ rest:
229229
hidden_rest:
230230
root := cli.Command "root"
231231
--rest=[
232-
cli.OptionString "foo" --hidden,
232+
cli.Option "foo" --hidden,
233233
]
234234
--run=(:: null)
235235
expect_throw "Rest argument 'foo' of 'root' cannot be hidden.":
@@ -239,8 +239,8 @@ snake_kebab:
239239
// Test that kebab and snake case lead to ambiguous options.
240240
root := cli.Command "root"
241241
--options=[
242-
cli.OptionString "foo-bar",
243-
cli.OptionString "foo_bar"
242+
cli.Option "foo-bar",
243+
cli.Option "foo_bar"
244244
]
245245
--run=:: | parsed/cli.Parsed |
246246
unreachable

0 commit comments

Comments
 (0)