forked from gnyers/python-tuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparse-demo.py
More file actions
executable file
·57 lines (44 loc) · 1.99 KB
/
argparse-demo.py
File metadata and controls
executable file
·57 lines (44 loc) · 1.99 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
#!/usr/bin/env python3
'''Simple argparse demo for the `profiler` program
'''
import argparse
import sys
from os.path import isdir
def valid_dir(dirname):
''' a validator function for directory names
returns: `dirname` if directory exists and is indeed a directory
raises: argparse.ArgumentTypeError dirname
'''
if isdir(dirname): # is dirname a valid directory?
return dirname # return dirname as is
else: # do not accept an invalid dir
raise argparse.ArgumentTypeError(f'{dirname} is not a directory')
def parseargs(cmdline=sys.argv[1:], # parse either CLI args or a string
known_args_only=False, # fail if unknown args?
description=__doc__, # --help begins with the docstring
epilog="That's all folks!" # --help ends with this string
):
''' CLI argument parser
'''
p = argparse.ArgumentParser( # get an ArgumentParser instance
formatter_class=argparse.RawTextHelpFormatter,
description=description,
epilog=epilog
)
p.add_argument('dirname', # name of this argument
metavar='DIR', # --help will show this as arg name
type=valid_dir, # validator function
help='path to directory to analyze') # purpose of this arg
if known_args_only:
return p.parse_known_args(cmdline)[0] # want only known args
else:
return p.parse_args(cmdline) # parse all args!
args = parseargs() # begin of the prg, lets parse args!
# **NOTE**: the received CLI arguments will be available through the whole
# program. To access any values refer to the appropriate attribute of the
# `args` object, e.g.: `args.dirname`
# Show the received info:
print(f'''
Received directory name from CLI arguments: {args.dirname}
''')
# The rest of the program's code goes here;