Python parameter Module Optionparser description

Source: Internet
Author: User

can replace a module of getoptFrom optparse import Optionparser# generate an instanceparser = Optionparser (usage= "%prog-f server.list-u root ... versrion 1", version= "%prog 1")parser.add_option ("-F", "--file", dest= "file", action= "store", help= "Host list")# Saved, can be options.filename by printparser.add_option ("-U", "--user", action= "store", dest= "user", help= "Username")# without specifying the dest parameter, the value of the options object will be accessed using the command-line parameter name, which is user.parser.add_option ("-S", "--screen", action= "Store_true", dest= "screen", help= "Output to the screen")# Store_true This action means that only when the parameter is used, that is, you add the-s parameter, it will be saved. (options, args) = Parser.parse_args ()Parse_args (): #解析命令行的参数 and pass the results to optionsreturns a value of two:1. Options, which is an object (Optpars. Values), save the command line parameter value. As long as you know the command line parameter name, such as file, you can access its corresponding value: Options.file. 2. Args, a list made up of positional argumentsArgdir = VARs (options)return Argdir============================================parser.add_option ('-V ', ' –version ', action= ' store_true ', dest= ' version ', Default=false, help= ' show the version of This command ')#store_true这个action的意思是只有在使用该参数的时候存储, default can give a value. That is, you added-V to display version information, otherwise it is not displayed.

Python parameter Module Optionparser description

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.