summaryrefslogtreecommitdiff
path: root/test/argv2.lm
blob: eb1ba790dec65703b35a6dd1b2658e1ef7320aaf (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
79
80
81
82
83
84
85
86
87
88
89
90
##### 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