Just look at the example and hit the code. A script that writes a command-line argument called to perform the operation.
Reference article link: http://www.jianshu.com/p/a50aead61319
#-*-coding:utf-8-*-__author__='Deen'" "Import Argparseparser = Argparse. Argumentparser () # This action works for if verbosity is used, the assignment is true, otherwise falseparser.add_argument ("--verbosity", help= "increase Output verbosity ", action =" store_true ") args = Parser.parse_args () # for this program method, only two parameters are useful, true, and Falseif ARGS.VERBOSITY:PR int "verbosity turned on"" "" "Import Argparseparser = Argparse. Argumentparser () parser.add_argument ('-V ', '--verbose ', help= ' increase output verbosity ', action= ' store _true ') args = Parser.parse_args () if Args.verbose:print "verbosity turned on"" "#almost, write yourself a subtraction command-line parameter script.ImportArgparsedefPlus (x, y):Print("Plus is%d"% ((x +y )))defminus (x, y):Print("minus is%s"% (X-y))defmul (x, y):Print("Mul is%s"% (x *y))defdivi (x, y):Print("Divi is%s"% Float (x/y))if __name__=='__main__': #adding Python dictionary mappings, calling functionsChoices = {'Plus': Plus,'minus': Minus,'Mul': Mul,'divi': Divi}#Description Script DescriptionParser = Argparse. Argumentparser (description='This a test') #to start adding parameters to a script #Add a function call parameterParser.add_argument ('function', choices=Choices, Help='To Count X,y,input fucntion in [Plus][minus][mul][divi]', default='Plus') #add command line parameter locationParser.add_argument ('x', help='Number x', type=int) parser.add_argument ('y', help='Nubmer y', type=int) args=Parser.parse_args () func=Choices[args.function]#Calling ParametersFunc (args.x, Args.y)
It feels like it's going to work.
Python command-line argument Learning (ii)