-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
argparse.erl
1900 lines (1663 loc) · 78.1 KB
/
argparse.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%%
%% Copyright Maxim Fedorov
%%
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(argparse).
-moduledoc """
Command line arguments parser.
This module implements command line parser. Parser operates with _commands_ and
_arguments_ represented as a tree. Commands are branches, and arguments are
leaves of the tree. Parser always starts with the root command, named after
`progname` (the name of the program which started Erlang).
A [`command specification`](`t:command/0`) may contain handler definition for
each command, and a number argument specifications. When parser is successful,
`argparse` calls the matching handler, passing arguments extracted from the
command line. Arguments can be positional (occupying specific position in the
command line), and optional, residing anywhere but prefixed with a specified
character.
`argparse` automatically generates help and usage messages. It will also issue
errors when users give the program invalid arguments.
## Quick start
`argparse` is designed to work with [`escript`](`e:erts:escript_cmd.md`). The
example below is a fully functioning Erlang program accepting two command line
arguments and printing their product.
```erlang
#!/usr/bin/env escript
main(Args) ->
argparse:run(Args, cli(), #{progname => mul}).
cli() ->
#{
arguments => [
#{name => left, type => integer},
#{name => right, type => integer}
],
handler =>
fun (#{left := Left, right := Right}) ->
io:format("~b~n", [Left * Right])
end
}.
```
Running this script with no arguments results in an error, accompanied by the
usage information.
The `cli` function defines a single command with embedded handler accepting a
map. Keys of the map are argument names as defined by the `argument` field of
the command, `left` and `right` in the example. Values are taken from the
command line, and converted into integers, as requested by the type
specification. Both arguments in the example above are required (and therefore
defined as positional).
## Command hierarchy
A command may contain nested commands, forming a hierarchy. Arguments defined at
the upper level command are automatically added to all nested commands. Nested
commands example (assuming `progname` is `nested`):
```erlang
cli() ->
#{
%% top level argument applicable to all commands
arguments => [#{name => top}],
commands => #{
"first" => #{
%% argument applicable to "first" command and
%% all commands nested into "first"
arguments => [#{name => mid}],
commands => #{
"second" => #{
%% argument only applicable for "second" command
arguments => [#{name => bottom}],
handler => fun (A) -> io:format("~p~n", [A]) end
}
}
}
}
}.
```
In the example above, a 3-level hierarchy is defined. First is the script itself
(`nested`), accepting the only argument `top`. Since it has no associated
handler, `run/3` will not accept user input omitting nested command selection.
For this example, user has to supply 5 arguments in the command line, two being
command names, and another 3 - required positional arguments:
```text
./nested.erl one first second two three
#{top => "one",mid => "two",bottom => "three"}
```
Commands have preference over positional argument values. In the example above,
commands and positional arguments are interleaving, and `argparse` matches
command name first.
## Arguments
`argparse` supports positional and optional arguments. Optional arguments, or
options for short, must be prefixed with a special character (`-` is the default
on all operating systems). Both options and positional arguments have 1 or more
associated values. See [`argument specification`](`t:argument/0`) to find more
details about supported combinations.
In the user input, short options may be concatenated with their values. Long
options support values separated by `=`. Consider this definition:
```erlang
cli() ->
#{
arguments => [
#{name => long, long => "-long"},
#{name => short, short => $s}
],
handler => fun (Args) -> io:format("~p~n", [Args]) end
}.
```
Running `./args --long=VALUE` prints `#{long => "VALUE"}`, running
`./args -sVALUE` prints `#{short => "VALUE"}`
`argparse` supports boolean flags concatenation: it is possible to shorten
`-r -f -v` to `-rfv`.
Shortened option names are not supported: it is not possible to use `--my-argum`
instead of `--my-argument-name` even when such option can be unambiguously
found.
""".
-moduledoc(#{since => "OTP 26.0"}).
-author("maximfca@gmail.com").
%% API Exports
-export([
run/3,
parse/2, parse/3,
help/1, help/2,
format_error/1
]).
%% Internal exports for validation and error reporting.
-export([validate/1, validate/2, format_error/2]).
%%--------------------------------------------------------------------
%% API
-doc """
Defines type conversion applied to the string retrieved from the user input. If
the conversion is successful, resulting value is validated using optional
`Choices`, or minimums and maximums (for integer and floating point values
only). Strings and binary values may be validated using regular expressions.
It's possible to define custom type conversion function, accepting a string and
returning Erlang term. If this function raises error with `badarg` reason,
argument is treated as invalid.
""".
-type arg_type() ::
boolean |
float |
{float, Choice :: [float()]} |
{float, [{min, float()} | {max, float()}]} |
integer |
{integer, Choices :: [integer()]} |
{integer, [{min, integer()} | {max, integer()}]} |
string |
{string, Choices :: [string()]} |
{string, Re :: string()} |
{string, Re :: string(), ReOptions :: [term()]} |
binary |
{binary, Choices :: [binary()]} |
{binary, Re :: binary()} |
{binary, Re :: binary(), ReOptions :: [term()]} |
atom |
{atom, Choices :: [atom()]} |
{atom, unsafe} |
{custom, fun((string()) -> term())}.
%% Built-in types include basic validation abilities
%% String and binary validation may use regex match (ignoring captured value).
%% For float, integer, string, binary and atom type, it is possible to specify
%% available choices instead of regex/min/max.
-doc """
User-defined help template to print in the command usage. First element of a
tuple must be a string. It is printed as a part of the usage header. Second
element of the tuple can be either a list containing strings, `type` and
`default` atoms, or a user-defined function that must return a string. A plain
string should be wrapped as a list such as `["string is nested"]`.
""".
-type argument_help() :: {
unicode:chardata(), %% short form, printed in command usage, e.g. "[--dir <dirname>]", developer is
%% responsible for proper formatting (e.g. adding <>, dots... and so on)
[unicode:chardata() | type | default] | fun(() -> unicode:chardata())
}.
%% Help template definition for argument. Short and long forms exist for every argument.
%% Short form is printed together with command definition, e.g. "usage: rm [--force]",
%% while long description is printed in detailed section below: "--force forcefully remove".
-doc "Argument name is used to populate argument map.".
-type argument_name() :: atom() | string() | binary().
-doc """
Argument specification. Defines a single named argument that is returned in the
[`argument map`](`t:arg_map/0`). The only required field is `name`, all other
fields have defaults.
If either of the `short` or `long` fields is specified, the argument is treated
as optional. Optional arguments do not have specific order and may appear
anywhere in the command line. Positional arguments are ordered the same way as
they appear in the arguments list of the command specification.
By default, all positional arguments must be present in the command line. The
parser will return an error otherwise. Options, however, may be omitted, in
which case resulting argument map will either contain the default value, or not
have the key at all.
- **`name`** - Sets the argument name in the parsed argument map. If `help` is
not defined, name is also used to generate the default usage message.
- **`short`** - Defines a short (single character) form of an optional argument.
```erlang
%% Define a command accepting argument named myarg, with short form $a:
1> Cmd = #{arguments => [#{name => myarg, short => $a}]}.
%% Parse command line "-a str":
2> {ok, ArgMap, _, _} = argparse:parse(["-a", "str"], Cmd), ArgMap.
#{myarg => "str"}
%% Option value can be concatenated with the switch: "-astr"
3> {ok, ArgMap, _, _} = argparse:parse(["-astr"], Cmd), ArgMap.
#{myarg => "str"}
```
By default all options expect a single value following the option switch. The
only exception is an option of a boolean type.
- **`long`** - Defines a long form of an optional argument.
```erlang
1> Cmd = #{arguments => [#{name => myarg, long => "name"}]}.
%% Parse command line "-name Erlang":
2> {ok, ArgMap, _, _} = argparse:parse(["-name", "Erlang"], Cmd), ArgMap.
#{myarg => "Erlang"}
%% Or use "=" to separate the switch and the value:
3> {ok, ArgMap, _, _} = argparse:parse(["-name=Erlang"], Cmd), ArgMap.
#{myarg => "Erlang"}
```
If neither `short` not `long` is defined, the argument is treated as
positional.
- **`required`** - Forces the parser to expect the argument to be present in the
command line. By default, all positional argument are required, and all
options are not.
- **`default`** - Specifies the default value to put in the parsed argument map
if the value is not supplied in the command line.
```erlang
1> argparse:parse([], #{arguments => [#{name => myarg, short => $m}]}).
{ok,#{}, ...
2> argparse:parse([], #{arguments => [#{name => myarg, short => $m, default => "def"}]}).
{ok,#{myarg => "def"}, ...
```
- **`type`** - Defines type conversion and validation routine. The default is
`string`, assuming no conversion.
- **`nargs`** - Defines the number of following arguments to consume from the
command line. By default, the parser consumes the next argument and converts
it into an Erlang term according to the specified type.
- **`t:pos_integer/0`** - Consume exactly this number of positional arguments,
fail if there is not enough. Value in the argument map contains a list of
exactly this length. Example, defining a positional argument expecting 3
integer values:
```erlang
1> Cmd = #{arguments => [#{name => ints, type => integer, nargs => 3}]},
argparse:parse(["1", "2", "3"], Cmd).
{ok, #{ints => [1, 2, 3]}, ...
```
Another example defining an option accepted as `-env` and expecting two
string arguments:
```erlang
1> Cmd = #{arguments => [#{name => env, long => "env", nargs => 2}]},
argparse:parse(["-env", "key", "value"], Cmd).
{ok, #{env => ["key", "value"]}, ...
```
- **`list`** - Consume all following arguments until hitting the next option
(starting with an option prefix). May result in an empty list added to the
arguments map.
```erlang
1> Cmd = #{arguments => [
#{name => nodes, long => "nodes", nargs => list},
#{name => verbose, short => $v, type => boolean}
]},
argparse:parse(["-nodes", "one", "two", "-v"], Cmd).
{ok, #{nodes => ["one", "two"], verbose => true}, ...
```
- **`nonempty_list`** - Same as `list`, but expects at least one argument.
Returns an error if the following command line argument is an option switch
(starting with the prefix).
- **`'maybe'`** - Consumes the next argument from the command line, if it does
not start with an option prefix. Otherwise, adds a default value to the
arguments map.
```erlang
1> Cmd = #{arguments => [
#{name => level, short => $l, nargs => 'maybe', default => "error"},
#{name => verbose, short => $v, type => boolean}
]},
argparse:parse(["-l", "info", "-v"], Cmd).
{ok,#{level => "info",verbose => true}, ...
%% When "info" is omitted, argument maps receives the default "error"
2> argparse:parse(["-l", "-v"], Cmd).
{ok,#{level => "error",verbose => true}, ...
```
- **`{'maybe', term()}`** - Consumes the next argument from the command line,
if it does not start with an option prefix. Otherwise, adds a specified
Erlang term to the arguments map.
- **`all`** - Fold all remaining command line arguments into a list, ignoring
any option prefixes or switches. Useful for proxying arguments into another
command line utility.
```erlang
1> Cmd = #{arguments => [
#{name => verbose, short => $v, type => boolean},
#{name => raw, long => "-", nargs => all}
]},
argparse:parse(["-v", "--", "-kernel", "arg", "opt"], Cmd).
{ok,#{raw => ["-kernel","arg","opt"],verbose => true}, ...
```
- **`action`** - Defines an action to take when the argument is found in the
command line. The default action is `store`.
- **`store`** - Store the value in the arguments map. Overwrites the value
previously written.
```erlang
1> Cmd = #{arguments => [#{name => str, short => $s}]},
argparse:parse(["-s", "one", "-s", "two"], Cmd).
{ok, #{str => "two"}, ...
```
- **`{store, term()}`** - Stores the specified term instead of reading the
value from the command line.
```erlang
1> Cmd = #{arguments => [#{name => str, short => $s, action => {store, "two"}}]},
argparse:parse(["-s"], Cmd).
{ok, #{str => "two"}, ...
```
- **`append`** - Appends the repeating occurrences of the argument instead of
overwriting.
```erlang
1> Cmd = #{arguments => [#{name => node, short => $n, action => append}]},
argparse:parse(["-n", "one", "-n", "two", "-n", "three"], Cmd).
{ok, #{node => ["one", "two", "three"]}, ...
%% Always produces a list - even if there is one occurrence
2> argparse:parse(["-n", "one"], Cmd).
{ok, #{node => ["one"]}, ...
```
- **`{append, term()}`** - Same as `append`, but instead of consuming the
argument from the command line, appends a provided `t:term/0`.
- **`count`** - Puts a counter as a value in the arguments map. Useful for
implementing verbosity option:
```erlang
1> Cmd = #{arguments => [#{name => verbose, short => $v, action => count}]},
argparse:parse(["-v"], Cmd).
{ok, #{verbose => 1}, ...
2> argparse:parse(["-vvvv"], Cmd).
{ok, #{verbose => 4}, ...
```
- **`extend`** - Works as `append`, but flattens the resulting list. Valid
only for `nargs` set to `list`, `nonempty_list`, `all` or `t:pos_integer/0`.
```erlang
1> Cmd = #{arguments => [#{name => duet, short => $d, nargs => 2, action => extend}]},
argparse:parse(["-d", "a", "b", "-d", "c", "d"], Cmd).
{ok, #{duet => ["a", "b", "c", "d"]}, ...
%% 'append' would result in {ok, #{duet => [["a", "b"],["c", "d"]]},
```
- **`help`** - Specifies help/usage text for the argument. `argparse` provides
automatic generation based on the argument name, type and default value, but
for better usability it is recommended to have a proper description. Setting
this field to `hidden` suppresses usage output for this argument.
""".
-type argument() :: #{
%% Argument name, and a destination to store value too
%% It is allowed to have several arguments named the same, setting or appending to the same variable.
name := argument_name(),
%% short, single-character variant of command line option, omitting dash (example: $b, meaning -b),
%% when present, the argument is considered optional
short => char(),
%% long command line option, omitting first dash (example: "kernel" means "-kernel" in the command line)
%% long command always wins over short abbreviation (e.g. -kernel is considered before -k -e -r -n -e -l)
%% when present, the argument is considered optional
long => string(),
%% makes parser to return an error if the argument is not present in the command line
required => boolean(),
%% default value, produced if the argument is not present in the command line
%% parser also accepts a global default
default => term(),
%% parameter type (string by default)
type => arg_type(),
%% action to take when argument is matched
action => store | %% default: store argument consumed (last stored wins)
{store, term()} | %% does not consume argument, stores term() instead
append | %% appends consumed argument to a list
{append, term()} | %% does not consume an argument, appends term() to a list
count | %% does not consume argument, bumps counter
extend, %% uses when nargs is list/nonempty_list/all - appends every element to the list
%% how many positional arguments to consume
nargs =>
pos_integer() | %% consume exactly this amount, e.g. '-kernel key value' #{long => "-kernel", args => 2}
%% returns #{kernel => ["key", "value"]}
'maybe' | %% if the next argument is positional, consume it, otherwise produce default
{'maybe', term()} | %% if the next argument is positional, consume it, otherwise produce term()
list | %% consume zero or more positional arguments, until next optional
nonempty_list | %% consume at least one positional argument, until next optional
all, %% fold remaining command line into this argument
%% help string printed in usage, hidden help is not printed at all
help => hidden | unicode:chardata() | argument_help()
}.
%% Command line argument specification.
%% Argument can be optional - starting with - (dash), and positional.
-doc """
Arguments map is the map of argument names to the values extracted from the
command line. It is passed to the matching command handler. If an argument is
omitted, but has the default value is specified, it is added to the map. When no
default value specified, and argument is not present in the command line,
corresponding key is not present in the resulting map.
""".
-type arg_map() :: #{argument_name() => term()}.
%% Arguments map: argument name to a term, produced by parser. Supplied to the command handler
-doc """
Command handler specification. Called by [`run/3` ](`run/3`)upon successful
parser return.
- **`fun((arg_map()) -> term())`** - Function accepting
[`argument map`](`t:arg_map/0`). See the basic example in the
[Quick Start](`m:argparse#module-quick-start`) section.
- **`{Module :: module(), Function :: atom()}`** - Function named `Function`,
exported from `Module`, accepting [`argument map`](`t:arg_map/0`).
- **`{fun(() -> term()), Default :: term()}`** - Function accepting as many
arguments as there are in the `arguments` list for this command. Arguments
missing from the parsed map are replaced with the `Default`. Convenient way to
expose existing functions.
```erlang
1> Cmd = #{arguments => [
#{name => x, type => float},
#{name => y, type => float, short => $p}],
handler => {fun math:pow/2, 1}},
argparse:run(["2", "-p", "3"], Cmd, #{}).
8.0
%% default term 1 is passed to math:pow/2
2> argparse:run(["2"], Cmd, #{}).
2.0
```
- **`{Module :: module(), Function :: atom(), Default :: term()}`** - Function
named `Function`, exported from `Module`, accepting as many arguments as
defined for this command. Arguments missing from the parsed map are replaced
with the `Default`. Effectively, just a different syntax to the same
functionality as demonstrated in the code above.
""".
-type handler() ::
optional | %% valid for commands with sub-commands, suppresses parser error when no
%% sub-command is selected
fun((arg_map()) -> term()) | %% handler accepting arg_map
{module(), Fn :: atom()} | %% handler, accepting arg_map, Fn exported from module()
{fun(() -> term()), term()} | %% handler, positional form (term() is supplied for omitted args)
{module(), atom(), term()}. %% handler, positional form, exported from module()
%% Command handler. May produce some output. Can accept a map, or be
%% arbitrary mfa() for handlers accepting positional list.
%% Special value 'optional' may be used to suppress an error that
%% otherwise raised when command contains sub-commands, but arguments
%% supplied via command line do not select any.
-doc """
User-defined help template. Use this option to mix custom and predefined usage
text. Help template may contain unicode strings, and following atoms:
- **usage** - Formatted command line usage text, e.g. `rm [-rf] <directory>`.
- **commands** - Expanded list of sub-commands.
- **arguments** - Detailed description of positional arguments.
- **options** - Detailed description of optional arguments.
""".
-type command_help() :: [unicode:chardata() | usage | commands | arguments | options].
%% Template for the command help/usage message.
%% Command descriptor
-doc """
Command specification. May contain nested commands, forming a hierarchy.
- **`commands`** - Maps of nested commands. Keys must be strings, matching
command line input. Basic utilities do not need to specify any nested
commands.
- **`arguments`** - List of arguments accepted by this command, and all nested
commands in the hierarchy.
- **`help`** - Specifies help/usage text for this command. Pass `hidden` to
remove this command from the usage output.
- **`handler`** - Specifies a callback function to call by `run/3` when the
parser is successful.
""".
-type command() :: #{
%% Sub-commands are arranged into maps. Command name must not start with <em>prefix</em>.
commands => #{string() => command()},
%% accepted arguments list. Order is important!
arguments => [argument()],
%% help line
help => hidden | unicode:chardata() | command_help(),
%% recommended handler function
handler => handler()
}.
-doc """
Path to the nested command. First element is always the `progname`, subsequent
elements are nested command names.
""".
-type cmd_path() :: [string()].
%% Command path, for nested commands
-export_type([arg_type/0, argument_help/0, argument/0,
command/0, handler/0, cmd_path/0, arg_map/0]).
-doc """
Returned from [`parse/2,3`](`parse/3`) when the user input cannot be parsed
according to the command specification.
First element is the path to the command that was considered when the parser
detected an error. Second element, `Expected`, is the argument specification
that caused an error. It could be `undefined`, meaning that `Actual` argument
had no corresponding specification in the arguments list for the current
command.
When `Actual` is set to `undefined`, it means that a required argument is
missing from the command line. If both `Expected` and `Actual` have values, it
means validation error.
Use `format_error/1` to generate a human-readable error description, unless
there is a need to provide localised error messages.
""".
-type parser_error() :: {Path :: cmd_path(),
Expected :: argument() | undefined,
Actual :: string() | undefined,
Details :: unicode:chardata()}.
%% Returned from `parse/2,3' when command spec is valid, but the command line
%% cannot be parsed using the spec.
%% When `Expected' is undefined, but `Actual' is not, it means that the input contains
%% an unexpected argument which cannot be parsed according to command spec.
%% When `Expected' is an argument, and `Actual' is undefined, it means that a mandatory
%% argument is not provided in the command line.
%% When both `Expected' and `Actual' are defined, it means that the supplied argument
%% is failing validation.
%% When both are `undefined', there is some logical issue (e.g. a sub-command is required,
%% but was not selected).
-doc """
Options changing parser behaviour.
- **`prefixes`** - Changes the option prefix (the default is `-`).
- **`default`** - Specifies the default value for all optional arguments. When
this field is set, resulting argument map will contain all argument names.
Useful for easy pattern matching on the argument map in the handler function.
- **`progname`** - Specifies the program (root command) name. Returned as the
first element of the command path, and printed in help/usage text. It is
recommended to have this value set, otherwise the default one is determined
with `init:get_argument(progname)` and is often set to `erl` instead of the
actual script name.
- **`command`** - Specifies the path to the nested command for `help/2`. Useful
to limit output for complex utilities with multiple commands, and used by the
default error handling logic.
- **`columns`** - Specifies the help/usage text width (characters) for `help/2`.
Default value is 80.
""".
-type parser_options() :: #{
%% allowed prefixes (default is [$-]).
prefixes => [char()],
%% default value for all missing optional arguments
default => term(),
%% root command name (program name)
progname => string() | atom(),
%% considered by `help/2' only
command => cmd_path(), %% command to print the help for
columns => pos_integer() %% viewport width, in characters
}.
%% Parser options
-doc """
Returned from [`parse/2,3`](`parse/3`). Contains arguments extracted from the
command line, path to the nested command (if any), and a (potentially nested)
command specification that was considered when the parser finished successfully.
It is expected that the command contains a handler definition, that will be
called passing the argument map.
""".
-type parse_result() ::
{ok, arg_map(), Path :: cmd_path(), command()} |
{error, parser_error()}.
%% Parser result: argument map, path leading to successfully
%% matching command (contains only ["progname"] if there were
%% no subcommands matched), and a matching command.
%% @equiv validate(Command, #{})
-doc false.
-spec validate(command()) -> Progname :: string().
validate(Command) ->
validate(Command, #{}).
%% @doc Validate command specification, taking Options into account.
%% Raises an error if the command specification is invalid.
-doc false.
-spec validate(command(), parser_options()) -> Progname :: string().
validate(Command, Options) ->
Prog = executable(Options),
is_list(Prog) orelse erlang:error(badarg, [Command, Options],
[{error_info, #{cause => #{2 => <<"progname is not valid">>}}}]),
Prefixes = maps:from_list([{P, true} || P <- maps:get(prefixes, Options, [$-])]),
_ = validate_command([{Prog, Command}], Prefixes),
Prog.
%% @equiv parse(Args, Command, #{})
-doc(#{equiv => parse/3}).
-doc(#{since => <<"OTP 26.0">>}).
-spec parse(Args :: [string()], command()) -> parse_result().
parse(Args, Command) ->
parse(Args, Command, #{}).
%% @doc Parses supplied arguments according to expected command specification.
%% @param Args command line arguments (e.g. `init:get_plain_arguments()')
%% @returns argument map, or argument map with deepest matched command
%% definition.
-doc """
Parses command line arguments according to the command specification. Raises an
exception if the command specification is not valid. Use
[`erl_error:format_exception/3,4` ](`erl_error:format_exception/3`)to see a
friendlier message. Invalid command line input does not raise an exception, but
makes `parse/2,3` to return a tuple
[`{error, parser_error()}`](`t:parser_error/0`).
This function does not call command handler.
""".
-doc(#{since => <<"OTP 26.0">>}).
-spec parse(Args :: [string()], command(), Options :: parser_options()) -> parse_result().
parse(Args, Command, Options) ->
Prog = validate(Command, Options),
%% use maps and not sets v2, because sets:is_element/2 cannot be used in guards (unlike is_map_key)
Prefixes = maps:from_list([{P, true} || P <- maps:get(prefixes, Options, [$-])]),
try
parse_impl(Args, merge_arguments(Prog, Command, init_parser(Prefixes, Command, Options)))
catch
%% Parser error may happen at any depth, and bubbling the error is really
%% cumbersome. Use exceptions and catch it before returning from `parse/2,3' instead.
throw:Reason ->
{error, Reason}
end.
%% @equiv help(Command, #{})
-doc(#{equiv => help/2}).
-doc(#{since => <<"OTP 26.0">>}).
-spec help(command()) -> string().
help(Command) ->
help(Command, #{}).
%% @doc Returns help for Command formatted according to Options specified
-doc """
Generates help/usage information text for the command supplied, or any nested
command when `command` option is specified. Arguments are displayed in the same
order as specified in `Command`. Does not provide localisation. Expects
`progname` to be set, otherwise defaults to return value of
`init:get_argument(progname)`.
""".
-doc(#{since => <<"OTP 26.0">>}).
-spec help(command(), parser_options()) -> unicode:chardata().
help(Command, Options) ->
Prog = validate(Command, Options),
format_help({Prog, Command}, Options).
%% @doc
-doc """
Parses command line arguments and calls the matching command handler. Prints
human-readable error, help/usage information for the discovered command, and
halts the emulator with code 1 if there is any error in the command
specification or user-provided command line input.
> #### Warning {: .warning }
>
> This function is designed to work as an entry point to a standalone
> [`escript`](`e:erts:escript_cmd.md`). Therefore, it halts the emulator for any
> error detected. Do not use this function through remote procedure call, or it
> may result in an unexpected shutdown of a remote node.
""".
-doc(#{since => <<"OTP 26.0">>}).
-spec run(Args :: [string()], command(), parser_options()) -> term().
run(Args, Command, Options) ->
try parse(Args, Command, Options) of
{ok, ArgMap, Path, SubCmd} ->
handle(Command, ArgMap, tl(Path), SubCmd);
{error, Reason} ->
io:format("error: ~ts~n", [argparse:format_error(Reason)]),
io:format("~ts", [argparse:help(Command, Options#{command => tl(element(1, Reason))})]),
erlang:halt(1)
catch
error:Reason:Stack ->
io:format(erl_error:format_exception(error, Reason, Stack)),
erlang:halt(1)
end.
%% @doc Basic formatter for the parser error reason.
-doc """
Generates human-readable text for [`parser error`](`t:parser_error/0`). Does not
include help/usage information, and does not provide localisation.
""".
-doc(#{since => <<"OTP 26.0">>}).
-spec format_error(Reason :: parser_error()) -> unicode:chardata().
format_error({Path, undefined, undefined, Details}) ->
io_lib:format("~ts: ~ts", [format_path(Path), Details]);
format_error({Path, undefined, Actual, Details}) ->
io_lib:format("~ts: unknown argument: ~ts~ts", [format_path(Path), Actual, Details]);
format_error({Path, #{name := Name}, undefined, Details}) ->
io_lib:format("~ts: required argument missing: ~ts~ts", [format_path(Path), Name, Details]);
format_error({Path, #{name := Name}, Value, Details}) ->
io_lib:format("~ts: invalid argument for ~ts: ~ts ~ts", [format_path(Path), Name, Value, Details]).
-type validator_error() ::
{?MODULE, command | argument, cmd_path(), Field :: atom(), Detail :: unicode:chardata()}.
%% @doc Transforms exception thrown by `validate/1,2' according to EEP54.
%% Use `erl_error:format_exception/3,4' to get the shell-like output.
-doc false.
-spec format_error(Reason :: validator_error(), erlang:stacktrace()) -> map().
format_error({?MODULE, command, Path, Field, Reason}, [{_M, _F, [Cmd], Info} | _]) ->
#{cause := Cause} = proplists:get_value(error_info, Info, #{}),
Cause#{general => <<"command specification is invalid">>, 1 => io_lib:format("~tp", [Cmd]),
reason => io_lib:format("command \"~ts\": invalid field '~ts', reason: ~ts", [format_path(Path), Field, Reason])};
format_error({?MODULE, argument, Path, Field, Reason}, [{_M, _F, [Arg], Info} | _]) ->
#{cause := Cause} = proplists:get_value(error_info, Info, #{}),
ArgName = maps:get(name, Arg, ""),
Cause#{general => "argument specification is invalid", 1 => io_lib:format("~tp", [Arg]),
reason => io_lib:format("command \"~ts\", argument '~ts', invalid field '~ts': ~ts",
[format_path(Path), ArgName, Field, Reason])}.
%%--------------------------------------------------------------------
%% Parser implementation
%% Parser state (not available via API)
-record(eos, {
%% prefix character map, by default, only -
prefixes :: #{char() => true},
%% argument map to be returned
argmap = #{} :: arg_map(),
%% sub-commands, in reversed orders, allowing to recover the path taken
commands = [] :: cmd_path(),
%% command being matched
current :: command(),
%% unmatched positional arguments, in the expected match order
pos = [] :: [argument()],
%% expected optional arguments, mapping between short/long form and an argument
short = #{} :: #{integer() => argument()},
long = #{} :: #{string() => argument()},
%% flag, whether there are no options that can be confused with negative numbers
no_digits = true :: boolean(),
%% global default for not required arguments
default :: error | {ok, term()}
}).
init_parser(Prefixes, Cmd, Options) ->
#eos{prefixes = Prefixes, current = Cmd, default = maps:find(default, Options)}.
%% Optional or positional argument?
-define(IS_OPTION(Arg), is_map_key(short, Arg) orelse is_map_key(long, Arg)).
%% helper function to match either a long form of "--arg=value", or just "--arg"
match_long(Arg, LongOpts) ->
case maps:find(Arg, LongOpts) of
{ok, Option} ->
{ok, Option};
error ->
%% see if there is '=' equals sign in the Arg
case string:split(Arg, "=") of
[MaybeLong, Value] ->
case maps:find(MaybeLong, LongOpts) of
{ok, Option} ->
{ok, Option, Value};
error ->
nomatch
end;
_ ->
nomatch
end
end.
%% parse_impl implements entire internal parse logic.
%% Clause: option starting with any prefix
%% No separate clause for single-character short form, because there could be a single-character
%% long form taking precedence.
parse_impl([[Prefix | Name] | Tail], #eos{prefixes = Pref} = Eos) when is_map_key(Prefix, Pref) ->
%% match "long" option from the list of currently known
case match_long(Name, Eos#eos.long) of
{ok, Option} ->
consume(Tail, Option, Eos);
{ok, Option, Value} ->
consume([Value | Tail], Option, Eos);
nomatch ->
%% try to match single-character flag
case Name of
[Flag] when is_map_key(Flag, Eos#eos.short) ->
%% found a flag
consume(Tail, maps:get(Flag, Eos#eos.short), Eos);
[Flag | Rest] when is_map_key(Flag, Eos#eos.short) ->
%% can be a combination of flags, or flag with value,
%% but can never be a negative integer, because otherwise
%% it will be reflected in no_digits
case abbreviated(Name, [], Eos#eos.short) of
false ->
%% short option with Rest being an argument
consume([Rest | Tail], maps:get(Flag, Eos#eos.short), Eos);
Expanded ->
%% expand multiple flags into actual list, adding prefix
parse_impl([[Prefix,E] || E <- Expanded] ++ Tail, Eos)
end;
MaybeNegative when Prefix =:= $-, Eos#eos.no_digits ->
case is_digits(MaybeNegative) of
true ->
%% found a negative number
parse_positional([Prefix|Name], Tail, Eos);
false ->
catch_all_positional([[Prefix|Name] | Tail], Eos)
end;
_Unknown ->
catch_all_positional([[Prefix|Name] | Tail], Eos)
end
end;
%% Arguments not starting with Prefix: attempt to match sub-command, if available
parse_impl([Positional | Tail], #eos{current = #{commands := SubCommands}} = Eos) ->
case maps:find(Positional, SubCommands) of
error ->
%% sub-command not found, try positional argument
parse_positional(Positional, Tail, Eos);
{ok, SubCmd} ->
%% found matching sub-command with arguments, descend into it
parse_impl(Tail, merge_arguments(Positional, SubCmd, Eos))
end;
%% Clause for arguments that don't have sub-commands (therefore check for
%% positional argument).
parse_impl([Positional | Tail], Eos) ->
parse_positional(Positional, Tail, Eos);
%% Entire command line has been matched, go over missing arguments,
%% add defaults etc
parse_impl([], #eos{argmap = ArgMap0, commands = Commands, current = Current, pos = Pos, default = Def} = Eos) ->
%% error if stopped at sub-command with no handler
map_size(maps:get(commands, Current, #{})) >0 andalso
(not is_map_key(handler, Current)) andalso
throw({Commands, undefined, undefined, <<"subcommand expected">>}),
%% go over remaining positional, verify they are all not required
ArgMap1 = fold_args_map(Commands, true, ArgMap0, Pos, Def),
%% go over optionals, and either raise an error, or set default
ArgMap2 = fold_args_map(Commands, false, ArgMap1, maps:values(Eos#eos.short), Def),
ArgMap3 = fold_args_map(Commands, false, ArgMap2, maps:values(Eos#eos.long), Def),
%% return argument map, command path taken, and the deepest
%% last command matched (usually it contains a handler to run)
{ok, ArgMap3, Eos#eos.commands, Eos#eos.current}.
%% Generate error for missing required argument, and supply defaults for
%% missing optional arguments that have defaults.
fold_args_map(Commands, Req, ArgMap, Args, GlobalDefault) ->
lists:foldl(
fun (#{name := Name}, Acc) when is_map_key(Name, Acc) ->
%% argument present
Acc;
(#{required := true} = Opt, _Acc) ->
%% missing, and required explicitly
throw({Commands, Opt, undefined, <<>>});
(#{name := Name, required := false, default := Default}, Acc) ->
%% explicitly not required argument with default
Acc#{Name => Default};
(#{name := Name, required := false}, Acc) ->
%% explicitly not required with no local default, try global one
try_global_default(Name, Acc, GlobalDefault);
(#{name := Name, default := Default}, Acc) when Req =:= true ->
%% positional argument with default
Acc#{Name => Default};
(Opt, _Acc) when Req =:= true ->
%% missing, for positional argument, implicitly required
throw({Commands, Opt, undefined, <<>>});
(#{name := Name, default := Default}, Acc) ->
%% missing, optional, and there is a default
Acc#{Name => Default};
(#{name := Name}, Acc) ->
%% missing, optional, no local default, try global default
try_global_default(Name, Acc, GlobalDefault)
end, ArgMap, Args).
try_global_default(_Name, Acc, error) ->
Acc;
try_global_default(Name, Acc, {ok, Term}) ->
Acc#{Name => Term}.
%%--------------------------------------------------------------------
%% argument consumption (nargs) handling
catch_all_positional(Tail, #eos{pos = [#{nargs := all} = Opt]} = Eos) ->
action([], Tail, Opt#{type => {list, maps:get(type, Opt, string)}}, Eos);
%% it is possible that some positional arguments are not required,
%% and therefore it is possible to catch all skipping those
catch_all_positional(Tail, #eos{argmap = Args, pos = [#{name := Name, default := Default, required := false} | Pos]} = Eos) ->
catch_all_positional(Tail, Eos#eos{argmap = Args#{Name => Default}, pos = Pos});
%% same as above, but no default specified
catch_all_positional(Tail, #eos{pos = [#{required := false} | Pos]} = Eos) ->
catch_all_positional(Tail, Eos#eos{pos = Pos});
catch_all_positional([Arg | _Tail], #eos{commands = Commands}) ->
throw({Commands, undefined, Arg, <<>>}).