summaryrefslogtreecommitdiff
path: root/test/argv2.lm
diff options
context:
space:
mode:
Diffstat (limited to 'test/argv2.lm')
-rw-r--r--test/argv2.lm91
1 files changed, 91 insertions, 0 deletions
diff --git a/test/argv2.lm b/test/argv2.lm
new file mode 100644
index 0000000..5c84564
--- /dev/null
+++ b/test/argv2.lm
@@ -0,0 +1,91 @@
+##### LM #####
+lex
+ literal `-
+ token file /^('-'|0)(^0)*/
+end
+
+lex
+ token single /[qvh]/
+ token with_opt /[oi]/
+ token dash /'-'/
+
+ literal `help `verbose `input `=
+end
+
+def long
+ [`help]
+| [`verbose]
+
+def long_with_opt
+ [`input]
+
+def long_eqals
+ [`=]
+| [zero]
+
+token word /(^0)+/
+token zero /0/
+
+def item
+ [`- single* zero]
+| [`- with_opt zero? word zero]
+| [`- dash long zero]
+| [`- dash long_with_opt long_eqals word zero]
+| [file zero]
+
+def args
+ [word zero item*]
+
+# The argument parser. Using an accumulator so we can send nulls after each
+# arg.
+cons ArgParser: parser<args>[]
+
+# Parse the args and extract the result into Args.
+ArgV: list<str> = argv
+for A: str in ArgV
+ send ArgParser [A '\0']
+Args: args = ArgParser()
+
+# Process the args.
+for Item: item in Args {
+ if match Item
+ [`- SL: single* zero]
+ {
+ for S: single in SL
+ print( "single: [$S]\n" )
+ }
+ elsif match Item
+ [`- W: with_opt zero? Opt: word zero]
+ {
+ print( "with opt: [$W] -> [$Opt]\n" )
+ }
+ elsif match Item
+ [`- dash L: long zero]
+ {
+ print("long: [$L]\n" )
+ }
+ elsif match Item
+ [`- dash LO: long_with_opt long_eqals LongOpt: word zero]
+ {
+ print("long: [$LO] -> [$LongOpt]\n" )
+ }
+ elsif match Item
+ [F: file zero]
+ {
+ print("file: [$F]\n" )
+ }
+}
+
+##### ARGS #####
+-qv -h -o output -iinput file --input=foo --input bar --help --verbose
+##### EXP #####
+single: q
+single: v
+single: h
+with opt: o -> output
+with opt: i -> input
+file: file
+long: input -> foo
+long: input -> bar
+long: help
+long: verbose