-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharg_parser.py
More file actions
87 lines (64 loc) · 3.04 KB
/
Copy patharg_parser.py
File metadata and controls
87 lines (64 loc) · 3.04 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
import argparse
"""
For each project, fill in this template for specific cmd arguments.
"""
# Put the argument parser in a function and return the args.
# Later on, import and call it in the main function.
def create_main_parser():
parser = argparse.ArgumentParser()
# TODO Add arguemnts below.
# Templates for positional arguments:
parser.add_argument("posarg1", type=int, help="<description>")
# Note: argmuent dest name will convert `-` to `_`. This gives `pos_arg2`
parser.add_argument("pos-arg2", type=int, help="<description>")
# Templates for optional arguments:
parser.add_argument("--optarg1", type=int,
default=10,
help="<description>")
# if the argument represents a binary value
parser.add_argument("--optarg2", action="store_true", help="<description>")
# if the argument has fixed choices.
parser.add_argument("--verbosity", type=int, choices=[0, 1, 2],
help="increase output verbosity")
# if we want to gather arguments into a list.
parser.add_argument("--optarg3", nargs="*", help="<description>")
# when the argument type is file
parser.add_argument("--input", type=argparse.FileType('r'))
parser.add_argument("--output", type=argparse.FileType('w'))
# `type` can take any callable that takes a single string argument and returns the converted value
parser.add_argument("foo", type=perfect_square)
# When the program has multiple functionnalities, we might want subparsers.
# Note main parser's arguments must come before subparsers.
# Use `dest` to help identify which subcommand was used.
subparsers = parser.add_subparsers(title="<subparser description>", dest="subcommand")
# We will create arguments of subparsers in functions for readibility.
add_functionA_subparser(subparsers)
add_functionB_subparser(subparsers)
# TODO add more sub commands below.
# Parsed arguments will only contain attributes for the main parser and the subparser.
return parser
# This is a fuction used for `type`.
def perfect_square(string):
import math
value = int(string)
sqrt = math.sqrt(value)
if sqrt != int(sqrt):
msg = "%r is not a perfect square" % string
raise argparse.ArgumentTypeError(msg)
return value
def add_functionA_subparser(subparsers):
a_parser = subparsers.add_parser("func-a", help="subcommand a")
a_parser.add_argument("yay", type=int, help="<yay>")
def add_functionB_subparser(subparsers):
b_parser = subparsers.add_parser('func-b', help='b help')
b_parser.add_argument('--baz', choices='XYZ', help='baz help')
# TODO add more sub command functions below.
# This is the funciton to be imported.
# Putting this function here so the above functions could be defined in custom order.
def parse_args():
parser = create_main_parser()
return parser.parse_args()
# USe the following line in the main script.
# from arg_parser import parse_args
if __name__ == '__main__':
print(vars(parse_args()))