Python uses getopt parsing command-line input parameter instances

Source: Internet
Author: User
In this paper, we describe Python's method of using getopt to parse command line input parameters and share them for your reference.

The specific instance code is as follows:

Import getopt Import sys  config = {   "input": "",   "Output": ".",    }  #getopt三个选项, the first one is generally sys.argv[1:], The second parameter is a short parameter, if the argument must follow the value, add:, the third parameter is a long parameter #是一个列表, opts, args = Getopt.getopt (sys.argv[1:], ' hi:o:d ',     [     ' input= ',      ' output= ',      ' help '     ]    )  #参数的解析过程, the long parameter is--, the short parameter is-for option, the value in the OPTs:   if option in ["-H", "--help"]:     print "" "     usage:%s--input=[value]--output=[value]     usage:%s-input value-o value     " ""   elif option in ['--input ', '-i ']:     config["input"] = value   elif option in ['--output ', '-o ']:     config ["Output"] = value   elif option = "-D":     print "usage-d"  print config  

Input parameters:

--input=c:\temp\aa-o c:\temp\output-d

Printed results:

usage-d{' input ': ' c:\\temp\\aa ', ' Output ': ' C:\\temp\\output '}

Hopefully this article will help you with Python programming.

  • 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.