forked from sbu-python-class/python-science
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparse_example.py
More file actions
executable file
·37 lines (28 loc) · 1.08 KB
/
argparse_example.py
File metadata and controls
executable file
·37 lines (28 loc) · 1.08 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
#!/usr/bin/env python
# to get usage: use -h
import sys
import argparse
# simple example of argparse
#
# ./argparse_example.py -a -b # -c string --darg --earg fextra
parser = argparse.ArgumentParser()
parser.add_argument("-a", help="the -a option", action="store_true")
parser.add_argument("-b", help="-b takes a number", type=int, default=0)
parser.add_argument("-c", help="-c takes a string", type=str, default=None)
parser.add_argument("--darg", help="the --darg option", action="store_true")
parser.add_argument("--earg", help="--earg takes a string", type=str, metavar="test",
default="example string")
# extra arguments (positional)
parser.add_argument("extras", metavar="extra", type=str, nargs="*",
help="optional positional arguments")
args = parser.parse_args()
if args.a: print "-a set"
print "-b = {}".format(args.b)
print "-c = {}".format(args.c)
if args.darg: print "--dargs set"
print "--earg value = {}".format(args.earg)
print " "
print "extra positional arguments: "
if len(args.extras) > 0:
for e in args.extras:
print e