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 [item*] # The argument parser. Using an accumulator so we can send nulls after each # arg. ArgParser: parser = new parser() # Parse the args and extract the result into Args. A: argv_el = argv->head while ( A ) { send ArgParser [A->value '\0'] A = A->next } Args: args = ArgParser->finish() # 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