Use getopt. getopt ()
To analyze command line parameters
Number.
Getopt is a module used to analyze command line parameters.
Functions in the module:
Getopt (ARGs, options [, long_options]) ARGs is generally SYS. argv [1:]. Options is a string consisting of option letters (starting with "-"). If an option letter carries a parameter, the letter must contain. Long_options is an optional parameter. It is a string list used to indicate a long parameter (starting with "--"). If a long parameter has a parameter
Enter "=" after the letter ". The function has two returned results:
The first one is (option,
Value. Option is an option letter with "-" or a long parameter with "--" (for example, -- HL can match -- Help ).
For example If option has a parameter, it is given in value; otherwise, value is null.
The second is the parameter not matched by the function. Example in the python document:
Import getopt, sys
Def
Main ():
Try:
Opts, argS = getopt. getopt (SYS. argv [1:],
"Ho: V", ["help", "output ="])
Counter t getopt. getopterror:
# Print help information and exit:
Usage ()
SYS. Exit (2)
Output = none
Verbose = false
For O,
In opts:
If O = "-V ":
Verbose = true
If O in ("-h", "-- Help "):
Usage ()
SYS. Exit ()
If O in ("-o", "-- output "):
Output =
A
#...
If _ name _ = "_ main __":
Main ()
If the program is called in this way
: Python program name. py-h-o ABC -- out def Ghi
The result returned by the getopt. getopt () function is roughly as follows:
.
(Option, value) in list is:
[('-H', ''), ('-O', 'abc'), ('-- output ',
'Def ')]
In ARGs:
['Ghi']
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