-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·110 lines (82 loc) · 3.98 KB
/
Copy pathexample.py
File metadata and controls
executable file
·110 lines (82 loc) · 3.98 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
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
#!/usr/bin/python3
# An example usage of easy_args
# Want to see it in use in a much LARGER project with multiple subsections including positional and HIDDEN args?
# Check out the parse_args() function in:
# https://github.com/SurpriseDog/KeyLocker/blob/master/args.py
import argparse
import easy_args
def my_parser():
"An example using my custom parser"
# The class itself
# newline is the number of newlines between groups
# verbose prints what each argument looks like with parser.add_argument
am = easy_args.ArgMaster(usage="./example.py <pos arg> --options...",
newline='\n'*2,
exit=False,
verbose=True)
# A list of required Positional arguments:
pos_args = [\
['pos1'],
"Positional argument 1",
]
am.update(pos_args, 'Positional Arguments:', positionals=True)
# A list of Optional arguments
basic_args = [\
['name'],
"enter your name",
['age', 'min_age', int, 365],
"File age in days",
['size', '', float, 100],
'''
Min file size in MB
As this is a multline help in the source code,
the second line has been automatically bumped down.
Starting at the word 'As'
''',
['verbose', '', bool],
'''Make everything very verbose. As verbosity is very important to understanding, this is a very verbose help line. As you can see it wraps at the edge of the screen (or the given `wrap` value, whichever is lower), but fear not! It will automatically fit neatly into it's place thanks to the magic in sd/columns.py''',
['three', '', 3],
"Three items must follow this argument.",
['list', '', list],
"A list of unknown length",
]
am.update(basic_args, 'Optional Arguments:')
return am.parse(wrap=95)
def old_parser():
"An example of the above using the default argparse syntax"
def msg():
return "./example.py <pos arg> --options..."
parser = argparse.ArgumentParser(allow_abbrev=True, usage=msg())
pos = parser.add_argument_group("Positional Arguments")
pos.add_argument('pos1', nargs='?', default='', help="Positional argument 1")
basic = parser.add_argument_group("Optional Arguments")
basic.add_argument('--name', dest='name', default='', type=str,
help="Enter your name")
basic.add_argument('--age', dest='min_age', nargs='?', type=int, default=365,
help="File age in days")
basic.add_argument('--size', nargs='?', type=float, default=100,
help='''
Min file size in MB
As this is a multline help in the source code,
the second line has been automatically bumped down.
Starting at the word 'As'
''')
basic.add_argument('--verbose', '-v', action='store_true',
help='''Make everything very verbose. As verbosity is very important to understanding, this is a very verbose help line. As you can see it wraps at the edge of the screen (or the given `wrap` value, whichever is lower), but fear not! It will automatically fit neatly into it's place thanks to the magic in sd/columns.py''')
basic.add_argument('--three', dest='three', default=[], type=str, nargs=3,
help="Three items must follow this argument.")
basic.add_argument('--list', dest='list', default=[], type=str, nargs='*',
help="A list of unknown length")
return parser.parse_args()
def main():
print("\nArguments as interpreted by EasyArgs:\n")
args = my_parser()
if args:
print('\n' + '#'*80)
print("EasyArgs passed by user:\n")
easy_args.show_args(args)
print('\n' + '#'*80)
print("Default argparse syntax for reference:\n")
easy_args.show_args(old_parser())
if __name__ == "__main__":
main()