-
Notifications
You must be signed in to change notification settings - Fork 0
/
args06app_call.py
32 lines (26 loc) · 1.18 KB
/
args06app_call.py
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
#!/usr/bin/env python3
import argparse, pathlib
def info(name, x):
return '{0:15s}:{1} is a {2}'.format(name, x, type(x).__name__)
def input_file(fn):
fp=pathlib.Path(fn)
if fp.is_file():
return fp
else:
raise ValueError()
def myapp(upper, set_false, files):
print(info('upper', upper))
print(info('set_false', set_false))
print(info('files', files))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='a very simple app with command line arguments.')
parser.add_argument('-u', '--upper', action='store_true', help="convert strings to upper case if present - default off")
parser.add_argument('-s', '--set_false', action='store_false', help='this argument defaults to True')
# there are other actions - see https://docs.python.org/3/library/argparse.html#action
parser.add_argument('files', nargs='+', type=input_file, help='the last argument, zero or more')
# use nargs='+' to force at least 1
# use nargs='*' for zero or more
# use nargs=5 for exactly 5
args = parser.parse_args()
# print(args.__dict__)
myapp(**args.__dict__)