1import argparse
2
3if __name__ == "__main__":
4 #add a description
5 parser = argparse.ArgumentParser(description="what the program does")
6
7 #add the arguments
8 parser.add_argument("arg1", help="advice on arg")
9 parser.add_argument("arg2", help="advice on arg")
10# .
11# .
12# .
13 parser.add_argument("argn", help="advice on arg")
14
15 #this allows you to access the arguments via the object args
16 args = parser.parse_args()
17
18 #how to use the arguments
19 args.arg1, args.arg2 ... args.argn
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...
2parser.add_argument('--val',
3 choices=['a', 'b', 'c'],
4 help='Special testing value')
5
6args = parser.parse_args(sys.argv[1:])