blob: 67b38d91b2a5f693b9b8f4027a4355630fafafc9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
lex args
{
literal '-'
token file /^('-'|0)(^0)*/
}
lex opts
{
token single /[qvh]/
token with_opt /[oi]/
token dash /'-'/
literal 'help', 'verbose', 'input', '='
}
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: argv_list = argv
for A: str in ArgV
ArgParser << [A '\0']
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" )
}
}
|