1 ## our_shell: ysh
2 ## oils_failures_allowed: 2
3
4 # Some thoughts before writing code. Hm can we do this entirely in user code, not as a builtin?
5 #
6 # The following is as close as possible to the python argparse which seems to work well
7
8 #### Argparse bool option and positional
9
10 hay define ArgSpec
11 hay define ArgSpec/Arg
12
13 ArgSpec myspec {
14 Arg -v --verbose { type = Bool }
15 Arg src
16 Arg dst
17 }
18 var args = ['-v', 'src/path', 'dst/path']
19 argparse (myspec, args, :opts)
20
21 json write (opts)
22 json write (args)
23 ## STDOUT:
24 {
25 "verbose": true,
26 "src": "src/path",
27 "dst": "dst/path"
28 }
29 # TODO: Should this be empty afterwards? Is it even possible with above call?
30 [
31
32 ]
33 ## END
34
35 #### Argparse basic help message
36 hay define ArgSpec
37 hay define ArgSpec/Arg
38
39 ArgSpec myspec {
40 description = '''
41 Reference Implementation
42 '''
43 prog = "program-name"
44 Arg -v --verbose { type = Bool; help = "Verbose" }
45 Arg src
46 Arg dst
47 }
48 var args = ['-h', 'src', 'dst']
49
50 argparse (myspec, args, :opts)
51 ## STDOUT:
52 usage: program-name [-h] [-v] src dst
53
54 Reference Implementation
55
56 positional arguments:
57 src
58 dst
59
60 options:
61 -h, --help show this help message and exit
62 -v, --verbose Verbose
63 ## END