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