Skip to content

Commit

Permalink
Fixes in add_arg_table macro parsing, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobaldassi committed Apr 26, 2018
1 parent ad6efdc commit 224ce77
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/ArgParse.jl
Expand Up @@ -755,13 +755,15 @@ macro add_arg_table(s, x...)
# Was parsed as doc syntax. Split into components
splice!(x, i, y.args[2:end])
continue
elseif isa(y, AbstractString) || Meta.isexpr(y, (:vcat, :tuple))
# found a string, or a vector expression, or a tuple:
# this must be the option name
if Meta.isexpr(y, :tuple)
# transform tuples into vectors
y.head = :vcat
elseif isa(y, AbstractString) || Meta.isexpr(y, (:vect, :tuple))
Meta.isexpr(y, :tuple) && (y.head = :vect) # transform tuples into vectors
if Meta.isexpr(y, :vect) && (isempty(y.args) || !all(x->x isa AbstractString, y.args))
# heterogeneous elements: splice it in place, just like blocks
splice!(x, i, y.args)
continue
end
# found a string, or a vector/tuple of strings:
# this must be the option name
if name nothing
# there was a previous arg field on hold
# first, concretely build the options
Expand Down
125 changes: 124 additions & 1 deletion test/argparse_test2.jl
Expand Up @@ -79,7 +79,130 @@ function ap_settings2b()
return s
end

for s = [ap_settings2(), ap_settings2b()]
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) == """
Expand Down

0 comments on commit 224ce77

Please sign in to comment.