Python uses getopt to parse command line input parameter instances _python

Source: Internet
Author: User

The example of this article is about Python using getopt to parse command line input parameters and share them for everyone's reference.

The specific example code is as follows:

Import getopt 
import sys 
 
config = {" 
  input": "", 
  "Output": ".", 
   
} 
 
#getopt三个选项, the first is generally SYS.ARGV [1:], the second parameter is a short parameter, if the parameter must be followed by a value, add:, the third parameter is a long parameter 
#是一个列表, 
opts, args = Getopt.getopt (sys.argv[1:], ' hi:o:d ',  
   [ 
    ' input= ',  
    ' output= ', '  
    help ' 
    ] 
 
#参数的解析过程, long parameter is--, short parameter is-for 
option, value in 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  

Parameters entered:

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

Results of Printing:

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

I hope this article will help you with your Python programming.

Related Article

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.