-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathargparse_action.py
More file actions
67 lines (63 loc) · 1.64 KB
/
argparse_action.py
File metadata and controls
67 lines (63 loc) · 1.64 KB
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
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-s", action="store", dest="simple_value", help="store a simple value"
)
parser.add_argument(
"-c",
action="store_const",
const="value-to-store",
dest="constant_value",
help="store a const value",
)
parser.add_argument(
"-t",
action="store_true",
default=False,
dest="boolean_t",
help="set a switch to true",
)
parser.add_argument(
"-f",
action="store_false",
default=True,
dest="boolean_f",
help="set a switch to false",
)
parser.add_argument(
"-a",
action="append",
default=[],
dest="collection",
help="add repeated values to a list",
)
parser.add_argument(
"-A",
action="append_const",
dest="const_collection",
const="value-1-to-append",
default=[],
help="add different values to list",
)
parser.add_argument(
"-B",
action="append_const",
dest="const_collection",
const="value-2-to-append",
help="add different values to list",
)
parser.add_argument("--version", action="version", version="%(prog)s 1.0")
results = parser.parse_args()
print(f"simple value = {results.simple_value!r}")
print(f"const_value = {results.constant_value!r}")
print(f"boolean_t = {results.boolean_t!r}")
print(f"boolean_f = {results.boolean_f!r}")
print(f"collection = {results.collection!r}")
print(f"const_collection = {results.const_collection!r}")
# python argparse_action.py -h
# python argparse_action.py -s value
# python argparse_action.py -c
# python argparse_action.py -t
# python argparse_action.py -f
# python argparse_action.py -a one -a two -a three
# python argparse_action.py -B -A