-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathargparse_example4.jl
80 lines (72 loc) · 3.17 KB
/
argparse_example4.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
# example 4: dest_name, metavar, range_tester, alternative
# actions, epilog with examples
using ArgParse
function main(args)
s = ArgParseSettings("Example 4 for argparse.jl: " *
"more tweaking of the arg fields: " *
"dest_name, metvar, range_tested, " *
"alternative actions.")
@add_arg_table! s begin
"--opt1"
action = :append_const # appends 'constant' to 'dest_name'
arg_type = String # the only utility of this is restricting the dest array type
constant = "O1"
dest_name = "O_stack" # this changes the destination
help = "append O1"
"--opt2"
action = :append_const
arg_type = String
constant = "O2"
dest_name = "O_stack" # same dest_name as opt1, different constant
help = "append O2"
"-k"
action = :store_const # stores constant if given, default otherwise
default = 0
constant = 42
help = "provide the answer"
"--awkward-option"
nargs = '+' # eats up as many argument as found (at least 1)
action = :append_arg # argument chunks are appended when the option is
# called repeatedly
arg_type = String
dest_name = "awk"
range_tester = (x->x=="X"||x=="Y") # each argument must be either "X" or "Y"
metavar = "XY"
help = "either X or Y; all XY's are " *
"stored in chunks"
end
# we add an epilog and provide usage examples, also demonstrating
# how to have some control on the formatting: we use additional '\n' at
# the end of lines to force newlines, and '\ua0' to put non-breakable spaces.
# Non-breakable spaces are not removed and are not used to split lines; they
# will be substituted with spaces in the final output.
s.epilog = """
examples:\n
\n
\ua0\ua0$(basename(Base.source_path())) --opt1 --opt2 --opt2 -k\n
\n
\ua0\ua0$(basename(Base.source_path())) --awkward X X --opt1 --awkward X Y X --opt2\n
\n
The first form takes option 1 once, than option 2, then activates the answer flag,
while the second form takes only option 1 and then 2, and intersperses them with "X\ua0X"
and "X\ua0Y\ua0X" groups, for no particular reason.
"""
# the epilog section will be displayed like this in the help screen:
#
# examples:
#
# argparse_example4.jl --opt1 --opt2 --opt2 -kkkkk
#
# argparse_example4.jl --awkward X X --opt1 --awkward X Y X --opt2
#
# The first form takes option 1 once, than option 2, then activates the
# answer flag, while the second form takes only option 1 and then 2, and
# intersperses them with "X X" and "X Y X" groups, for no particular
# reason.
parsed_args = parse_args(args, s)
println("Parsed args:")
for (key,val) in parsed_args
println(" $key => $(repr(val))")
end
end
main(ARGS)