Use python_getopt to parse command line input parameters

Source: Internet
Author: User

[Cpp]
Import getopt
Import sys
 
Config = {
"Input ":"",
"Output ":".",

}
 
# Getopt: the first option is sys. argv [1:]. The second parameter is short. If the parameter must be followed by a value, add:. The third parameter is long.
# Is a list,
Opts, args = getopt. getopt (sys. argv [1:], 'Hi: o: d ',
[
'Input = ',
'Output = ',
'Help'
]
)
 
# Parameter Parsing process. The long parameter is -- and the 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

Input parameter: -- input = c: \ temp \ aa-o c: \ temp \ output-d
Printed result:
Usage-d
{'Input': 'C: \ temp \ aa ', 'output': 'C: \ temp \ output '}

 


From Xiao Ju's column

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.