2011-8-23
Python 里边的parser用法
15.5. optparse — Parser for command
line options
http://docs.python.org/library/optparse.html
Here’s an example of using
from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
(options, args) = parser.parse_args()
With these few lines of code, users of your script can now do the “usual thing” on the command-line, for example:
<yourscript> --file=outfile -q
Thus, the following command lines are all equivalent to the above example:
<yourscript> -f outfile --quiet
<yourscript> --quiet --file outfile
<yourscript> -q -foutfile
<yourscript> -qfoutfile
Additionally, users can run one of
<yourscript> -h
<yourscript> --help
and
Usage: <yourscript> [options]
Options:
-h, --help show this help message and exit
-f FILE, --file=FILE write report to FILE
-q, --quiet don't print status messages to stdout
where the value of
argument
a string entered on the command-line, and passed by the shell to
It is occasionally desirable to substitute an argument list other thansys.argv[1:], so you should read “argument” as “an element of
option
an argument used to supply extra information to guide or customize the execution of a program. There are many different syntaxes for options; the traditional Unix syntax is a hyphen (“-“) followed by a single letter, e.g.
Some other option syntaxes that the world has seen include:
·
·
·
·
These option syntaxes are not supported by
option argument
an argument that follows an option, is closely associated with that option, and is consumed from the argument list when that option is. Withoptparse, option arguments may either be in a separate argument from their option:
-f foo
--file foo
or included in the same argument:
-ffoo
--file=foo
Typically, a given option either takes an argument or it doesn’t. Lots of people want an “optional option arguments” feature, meaning that some options will take an argument if they see it, and won’t if they don’t. This is somewhat controversial, because it makes parsing ambiguous: if
positional argument
something leftover in the argument list after options have been parsed, i.e. after options and their arguments have been parsed and removed from the argument list.
required option
an option that must be supplied on the command-line; note that the phrase “required option” is self-contradictory in English.
For example, consider this hypothetical command-line:
prog -v --report /tmp/report.txt foo bar
-v
While
First, you need to import the OptionParser class; then, early in the main program, create an OptionParser instance:
from optparse import OptionParser
[...]
parser = OptionParser()
Then you can start defining options. The basic syntax is:
parser.add_option(opt_str, ...,
attr=value, ...)
Each option has one or more option strings, such as
Typically, each option will have one short option string and one long option string, e.g.:
parser.add_option("-f", "--file", ...)
You’re free to define as many short option strings and as many long option strings as you like (including zero), as long as there is at least one option string overall.
The option strings passed to
Once all of your options are defined, instruct
(options, args) = parser.parse_args()
(If you like, you can pass a custom argument list to
parse_args()
- options, an object containing values for all of your options—e.g. if
--filetakes a single string argument, then options.file will be the filename supplied by the user, or None if the user did not supply that option - args, the list of positional arguments leftover after parsing options
This tutorial section only covers the four most important option attributes:action,
OK,that is enough for me to realize the GNU RADIO’s code. If I have extra time, I will continue to learn it.