2011-8-23
Python中的parser用法
15.5.optparse— Parser for command
线选项
3358 docs.python.org/library/optparse.html
here’sanexampleofusingoptparseinasimplescript :
来自optparse import年轻的夏天/p help='write report to FILE ',metavar='FILE ' )
action='store_false ',dest='verbose ',default=True,
help=' don ' tprintstatusmessagestostdout ' )
(options,args )=parser.parse_args ()
With these few lines of code,usersofyourscriptcannowdothe“usual thing”on the command-line,for example:
--file=outfile -q
Thus,thefollowingcommandlinesareallequivalenttotheaboveexample 3360
-f outfile --quiet
--quiet--文件输出文件
-q -foutfile
-qfoutfile
Additionally,users can run one of
-h
- -帮助
andoptparsewillprintoutabriefsummaryofyourscript’s options 3360
usage : [选项]
Options:
-h,--helpshowthishelpmessageandexit
- f文件,--file=FILE write report to FILE
-q,--quiet don ' tprintstatusmessagestostdout
wherethevalueofyourscriptisdeterminedatruntime (正常lyfromsys.argv [0] )。
15.5.1.1 . Terminology
授权
astringenteredonthecommand-line,andpassedbytheshelltoexecl (orexecv ).In Python,argumentsareeelementsofsys.argv [ 133333
itisoccasionallydesirabletosubstituteanargumentlistotherthansys.argv [ 1: ],soyoushouldread“argument”as“anelemenement”
选项
anargumentusedtosupplyextrainformationtoguideorcustomizetheexecutionofaprogram.therearemanydifferentsyntaxesforoptions; thetraditionalunixsyntaxisahyphen (“-”followed by a single letter,e.g.-xor-F. Also, traditionalunixsyntaxallowsmultipleoptionstobemergedintoasingleargument,e.g.- x-fisequivalentto-xf.thegnuprojectintioal
hyphen-separated words, e.g.--file or --dry-run. These are the only two option syntaxes provided byoptparse.Some other option syntaxes that the world has seen include:
These option syntaxes are not supported by optparse, and they never will be. This is deliberate: the first three are non-standard on any environment, and the last only makes sense if you’re exclusively targeting VMS, MS-DOS, and/or Windows.
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 lkddx
--file lkddx
or included in the same argument:
-flkddx
--file=lkddx
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 -atakes an optional argument and -b is another option entirely, how do we interpret -ab? Because of this ambiguity, optparse does not support this feature.
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. optparse doesn’t prevent you from implementing required options, but doesn’t give you much help at it either.
For example, consider this hypothetical command-line:
prog -v --report /tmp/report.txt lkddx bar
-v and --report are both options. Assuming that --report takes one argument,/tmp/report.txt is an option argument. lkddx and bar are positional arguments.
15.5.2. Tutorial
While optparse is quite flexible and powerful, it’s also straightforward to use in most cases. This section covers the code patterns that are common to any optparse-based program.
First, you need to import the OptionParser class; then, early in the main program, create an OptionParser instance:
from optparse import 年轻的夏天/p>
Then you can start defining options. The basic syntax is: attr=value, ...) Each option has one or more option strings, such as -f or --file, and several option attributes that tell optparse what to expect and what to do when it encounters that option on the command line. Typically, each option will have one short option string and one long option string, e.g.: 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 add_option() are effectively labels for the option defined by that call. For brevity, we will frequently refer to encountering an option on the command line; in reality, optparse encounters option strings and looks up options from them. Once all of your options are defined, instruct optparse to parse your program’s command line: (options, args) = parser.parse_args() (If you like, you can pass a custom argument list to parse_args(), but that’s rarely necessary: by default it uses sys.argv[1:].) parse_args() returns two values: 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, type, dest (destination), and help. Of these, action is the most fundamental. OK,that is enough for me to realize the GNU RADIO’s code. If I have extra time, I will continue to learn it.