/
argparse_test2.jl
246 lines (216 loc) · 10.7 KB
/
argparse_test2.jl
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# test 2: version information, default values, flags,
# options with types, optional arguments, variable
# number of arguments;
# function version of add_arg_table
function ap_settings2()
s = ArgParseSettings(description = "Test 2 for ArgParse.jl",
epilog = "Have fun!",
version = "Version 1.0",
add_version = true,
exc_handler = ArgParse.debug_handler)
@add_arg_table s begin
"--opt1"
nargs = '?' # '?' means optional argument
arg_type = Int # only Int arguments allowed
default = 0 # this is used when the option is not passed
constant = 1 # this is used if --opt1 is paseed with no argument
help = "an option"
"--flag", "-f"
action = :store_true # this makes it a flag
help = "a flag"
"--karma", "-k"
action = :count_invocations # increase a counter each time the option is given
help = "increase karma"
"arg1"
nargs = 2 # eats up two arguments; puts the result in a Vector
help = "first argument, two " *
"entries at once"
required = true
"arg2"
nargs = '*' # eats up as many arguments as possible before an option
default = Any["no_arg_given"] # since the result will be a Vector{Any}, the default must
# also be (or it can be [] or nothing)
help = "second argument, eats up " *
"as many items as possible " *
"before an option"
end
return s
end
function ap_settings2b()
s = ArgParseSettings(description = "Test 2 for ArgParse.jl",
epilog = "Have fun!",
version = "Version 1.0",
add_version = true,
exc_handler = ArgParse.debug_handler)
add_arg_table(s,
"--opt1", Dict(
:nargs => '?', # '?' means optional argument
:arg_type => Int, # only Int arguments allowed
:default => 0, # this is used when the option is not passed
:constant => 1, # this is used if --opt1 is paseed with no argument
:help => "an option"),
["--flag", "-f"], Dict(
:action => :store_true, # this makes it a flag
:help => "a flag"),
["--karma", "-k"], Dict(
:action => :count_invocations, # increase a counter each time the option is given
:help => "increase karma"),
"arg1", Dict(
:nargs => 2, # eats up two arguments; puts the result in a Vector
:help => "first argument, two " *
"entries at once",
:required => true),
"arg2", Dict(
:nargs => '*', # eats up as many arguments as possible before an option
:default => Any["no_arg_given"], # since the result will be a Vector{Any}, the default must
# also be (or it can be [] or nothing)
:help => "second argument, eats up " *
"as many items as possible " *
"before an option")
)
return s
end
function ap_settings2c()
s = ArgParseSettings(description = "Test 2 for ArgParse.jl",
epilog = "Have fun!",
version = "Version 1.0",
add_version = true,
exc_handler = ArgParse.debug_handler)
@add_arg_table(s
, "--opt1"
, nargs = '?' # '?' means optional argument
, arg_type = Int # only Int arguments allowed
, default = 0 # this is used when the option is not passed
, constant = 1 # this is used if --opt1 is paseed with no argument
, help = "an option"
, ["--flag", "-f"]
, action = :store_true # this makes it a flag
, help = "a flag"
, ["--karma", "-k"]
, action = :count_invocations # increase a counter each time the option is given
, help = "increase karma"
, "arg1"
, nargs = 2 # eats up two arguments; puts the result in a Vector
, help = "first argument, two " *
"entries at once"
, required = true
, "arg2"
, nargs = '*' # eats up as many arguments as possible before an option
, default = Any["no_arg_given"] # since the result will be a Vector{Any}, the default must
# also be (or it can be [] or nothing)
, help = "second argument, eats up " *
"as many items as possible " *
"before an option"
)
return s
end
function ap_settings2d()
s = ArgParseSettings(description = "Test 2 for ArgParse.jl",
epilog = "Have fun!",
version = "Version 1.0",
add_version = true,
exc_handler = ArgParse.debug_handler)
@add_arg_table s begin
("--opt1";
nargs = '?'; # '?' means optional argument
arg_type = Int; # only Int arguments allowed
default = 0; # this is used when the option is not passed
constant = 1; # this is used if --opt1 is paseed with no argument
help = "an option"),
(["--flag", "-f"];
action = :store_true; # this makes it a flag
help = "a flag")
(["--karma", "-k"];
action = :count_invocations; # increase a counter each time the option is given
help = "increase karma")
("arg1";
nargs = 2; # eats up two arguments; puts the result in a Vector
help = "first argument, two " *
"entries at once";
required = true)
("arg2";
nargs = '*'; # eats up as many arguments as possible before an option
default = Any["no_arg_given"]; # since the result will be a Vector{Any}, the default must
# also be (or it can be [] or nothing)
help = "second argument, eats up " *
"as many items as possible " *
"before an option")
end
return s
end
function ap_settings2e()
s = ArgParseSettings(description = "Test 2 for ArgParse.jl",
epilog = "Have fun!",
version = "Version 1.0",
add_version = true,
exc_handler = ArgParse.debug_handler)
@add_arg_table(s,
"--opt1",
begin
nargs = '?' # '?' means optional argument
arg_type = Int # only Int arguments allowed
default = 0 # this is used when the option is not passed
constant = 1 # this is used if --opt1 is paseed with no argument
help = "an option"
end,
["--flag", "-f"],
begin
action = :store_true # this makes it a flag
help = "a flag"
end,
["--karma", "-k"],
begin
action = :count_invocations # increase a counter each time the option is given
help = "increase karma"
end,
"arg1",
begin
nargs = 2 # eats up two arguments; puts the result in a Vector
help = "first argument, two " *
"entries at once"
required = true
end,
"arg2",
begin
nargs = '*' # eats up as many arguments as possible before an option
default = Any["no_arg_given"] # since the result will be a Vector{Any}, the default must
# also be (or it can be [] or nothing)
help = "second argument, eats up " *
"as many items as possible " *
"before an option"
end)
return s
end
for s = [ap_settings2(), ap_settings2b(), ap_settings2c(), ap_settings2d(), ap_settings2e()]
ap_test2(args) = parse_args(args, s)
@test stringhelp(s) == """
usage: $(basename(Base.source_path())) [--opt1 [OPT1]] [-f] [-k] arg1 arg1 [arg2...]
Test 2 for ArgParse.jl
positional arguments:
arg1 first argument, two entries at once
arg2 second argument, eats up as many items as possible
before an option (default: Any["no_arg_given"])
optional arguments:
--opt1 [OPT1] an option (type: $Int, default: 0, without arg: 1)
-f, --flag a flag
-k, --karma increase karma
Have fun!
"""
@test stringversion(s) == "Version 1.0\n"
@ap_test_throws ap_test2([])
@test ap_test2(["X", "Y"]) == Dict{String,Any}("opt1"=>0, "flag"=>false, "karma"=>0, "arg1"=>Any["X", "Y"], "arg2"=>Any["no_arg_given"])
@test ap_test2(["X", "Y", "-k", "-f", "Z", "--karma", "--opt"]) == Dict{String,Any}("opt1"=>1, "flag"=>true, "karma"=>2, "arg1"=>Any["X", "Y"], "arg2"=>Any["Z"])
@test ap_test2(["X", "Y", "--opt", "-k", "-f", "Z", "--karma"]) == Dict{String,Any}("opt1"=>1, "flag"=>true, "karma"=>2, "arg1"=>Any["X", "Y"], "arg2"=>Any["Z"])
@test ap_test2(["X", "Y", "--opt", "--karma", "-f", "Z", "-k"]) == Dict{String,Any}("opt1"=>1, "flag"=>true, "karma"=>2, "arg1"=>Any["X", "Y"], "arg2"=>Any["Z"])
@test ap_test2(["--opt", "-3", "X", "Y", "-k", "-f", "Z", "--karma"]) == Dict{String,Any}("opt1"=>-3, "flag"=>true, "karma"=>2, "arg1"=>Any["X", "Y"], "arg2"=>Any["Z"])
@ap_test_throws ap_test2(["--opt"])
@ap_test_throws ap_test2(["--opt="])
@ap_test_throws ap_test2(["--opt", "", "X", "Y"])
@ap_test_throws ap_test2(["--opt", "1e-2", "X", "Y"])
@ee_test_throws @add_arg_table(s, "required_arg_after_optional_args", required = true)
# wrong default
@ee_test_throws @add_arg_table(s, "--opt", arg_type = Int, default = 1.5)
# wrong range tester
@ee_test_throws @add_arg_table(s, "--opt", arg_type = Int, range_tester = x->string(x), default = 1)
@ee_test_throws @add_arg_table(s, "--opt", arg_type = Int, range_tester = x->sqrt(x)<1, default = -1)
end