1# parsing arguments with argparse is too easy
2import argparse
3
4parser = argparse.ArgumentParser(description='Process some integers.')
5parser.add_argument('integers', metavar='N', type=int, nargs='+',
6 help='an integer for the accumulator')
7parser.add_argument('--sum', dest='accumulate', action='store_const',
8 const=sum, default=max,
9 help='sum the integers (default: find the max)')
10
11args = parser.parse_args()
12print(args.accumulate(args.integers))
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument('file', type=argparse.FileType('r'))
5args = parser.parse_args()
6
7print(args.file.readlines())
1parser.add_argument("-v", "--verbose", action="store_true",
2 default="your default value", help="verbose output")
1# Generic parser function intialization in PYTHON
2def create_parser(arguments):
3 """Returns an instance of argparse.ArgumentParser"""
4 # your code here
5
6 parser = argparse.ArgumentParser(
7 description="Description of your code")
8 parser.add_argument("argument", help="mandatory or positional argument")
9 parser.add_argument("-o", "--optional",
10 help="Will take an optional argument after the flag")
11 namespace = parser.parse_args(arguments)
12
13 # Returns a namespace object with your arguments
14 return namespace
15
1parser.add_argument("-v", "--verbose", action="store_true",
2 help="verbose output")