diff options
| -rw-r--r-- | docs/command-line-parsing.md | 49 | ||||
| -rw-r--r-- | src/stdlib/cli.c | 2 |
2 files changed, 36 insertions, 15 deletions
diff --git a/docs/command-line-parsing.md b/docs/command-line-parsing.md index 7acfe95c..283c41d0 100644 --- a/docs/command-line-parsing.md +++ b/docs/command-line-parsing.md @@ -21,10 +21,10 @@ Compiled executable: greet $ ./greet greet: Required argument 'name' was not provided! -Signature: greet [--help] <name> [--be-excited|-E|--no-be-exited] +Usage: greet [--help] <name> [--be-excited|-E|--no-be-exited] $ ./greet --help -Signature: greet [--help] <name> [--be-excited|-E|--no-be-excited] +Usage: greet [--help] <name> [--be-excited|-E|--no-be-excited] $ ./greet "Zaphod" Hi Zaphod. @@ -40,7 +40,7 @@ Hi Zaphod. $ ./greet --not-a-real-argument "Bob" greet: Unrecognized argument: --not-a-real-argument -Signature: greet [--help] <name> [--be-excited|-E|--no-be-excited] +Usage: greet [--help] <name> [--be-excited|-E|--no-be-excited] ``` Underscores in argument names are converted to dashes when parsing command line @@ -91,27 +91,50 @@ or `1.23`) or scientific notation (`1e99`). For fixed-size integers (`Int64`, `Int32`, `Int16`, `Int8`), arguments that exceed the representable range for those values are considered usage errors. +### Structs + +For structs, values can be passed using positional arguments for each struct +field. + +``` +# foo.tm +struct Pair(x,y:Int) + +func main(pair:Pair) + >> pair + + +$ tomo foo.tm -- --pair 1 2 +Pair(x=1, y=2) +``` + +Tomo does not currently support omitting fields with default values or passing +individual struct fields by named flag. + ### Enums -For enums that do not have member values (e.g. `enum Foo(Baz, Qux)`, not `enum -Foo(Baz(x:Int), Qux)`, Tomo supports automatic command line argument parsing. -Parsing is case-insensitive: +For enums, values can be passed using the enum's tag name and each of its +fields positionally (the same as for structs). Parsing is case-sensitive: ``` # foo.tm -enum Foo(One, Two, Three) +enum Foo(Nothing, AnInteger(i:Int), TwoThings(i:Int, text:Text)) func main(foo:Foo) >> foo -# Signature: -$ tomo foo.tm one ->> Foo.One +$ tomo foo.tm -- Nothing +Nothing + +$ tomo foo.tm -- AnInteger 123 +AnInteger(123) -$ tomo foo.tm xxx -foo: Invalid value provided for --foo; valid values are: One Two -Signature: foo [--help] <foo> +$ tomo foo.tm -- TwoThings 123 hello +TwoThings(i=123, text="hello") ``` +Like structs, enums do not currently support passing fields as flags or +omitting fields with default values. + ### Lists of Text Currently, Tomo supports accepting arguments that take a list of text. diff --git a/src/stdlib/cli.c b/src/stdlib/cli.c index 397ed0b2..8512d106 100644 --- a/src/stdlib/cli.c +++ b/src/stdlib/cli.c @@ -150,8 +150,6 @@ void tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const ch } static int64_t parse_arg_list(List_t args, const char *flag, void *dest, const TypeInfo_t *type, bool allow_dashes) { - // print("Parsing type ", generic_as_text(NULL, true, type), ": ", - // generic_as_text(&args, true, List$info(&CString$info))); if (type->tag == ListInfo) { void *item = GC_MALLOC((size_t)type->ListInfo.item->size); int64_t n = 0; |
