Python command-line argument Learning (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.