Parsing command-line options for Argparse modules simple to use
util.py
#!/usr/bin/env python#Coding=utf-8ImportArgparseparser= Argparse. Argumentparser (prog="MyProgram", description="a test of Argparse") parser.add_argument (" -A", type=int,dest="Start", choices={1,2,3},help="Start number") parser.add_argument ("- b", type=int,dest="End", help="End Point") parser.add_argument ("- S","--sum", dest="Data", required=true,type=int,nargs="*", help="numbers to add") parser.add_argument ("- o", required=true,dest="FW", help="Output File")defCal_sum (A, B): s=0 forIinchRange (A, b): s+=Ireturnsif __name__=="__main__": Args=Parser.parse_args ()PrintargsPrintVARs (args) data=Args.dataPrintsum (data) Res=str (SUM (data)) with open (ARGS.FW,'WB') as Fw:fw.write (res)
Look at the effect:
>:~/$ python util.py-s 1 2 3-o output.txtnamespace (data=[1, 2, 3], End=none, fw='output.txt', start=None) {'Start': None,'FW':'output.txt','End': None,'Data': [1, 2, 3]}6[email protected]-ipin:~/workplace/mygithub/leetcodex$ more Output.txt6
Python argparse Module parsing command-line options simple to use