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[] # Parse the args and extract the result into Args. ArgV: list = 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" ) } }