mò æû¥Dc@s dZdkZdkZdkZdkZdklZdkZdkZdkZde fd„ƒYZ e Z de fd„ƒYZ dd„Zed „ZdS( s twisted.python.usage is a module for parsing/handling the command line of your program. For information on how to use it, see U{http://twistedmatrix.com/projects/core/documentation/howto/options.html}, or doc/howto/options.html in your Twisted directory. N(spatht UsageErrorcBstZRS(N(t__name__t __module__(((tl/home/radix/Projects/Twisted/branches/releases/twisted-core-2.5.x-2329-2/Twisted.exp/twisted/python/usage.pyRstOptionscBs°tZdZeZeZeZd„Zd„Zd„Z d„Z ed„Z d„Z d„Z ed„Zd „Zd „Zd „Zd „Zd „Zd„Zed„ZRS(s‡ An option list parser class C{optFlags} and C{optParameters} are lists of available parameters which your program can handle. The difference between the two is the 'flags' have an on(1) or off(0) state (off by default) whereas 'parameters' have an assigned value, with an optional default. (Compare '--verbose' and '--verbosity=2') optFlags is assigned a list of lists. Each list represents a flag parameter, as so:: | optFlags = [['verbose', 'v', 'Makes it tell you what it doing.'], | ['quiet', 'q', 'Be vewy vewy quiet.']] As you can see, the first item is the long option name (prefixed with '--' on the command line), followed by the short option name (prefixed with '-'), and the description. The description is used for the built-in handling of the --help switch, which prints a usage summary. C{optParameters} is much the same, except the list also contains a default value:: | optParameters = [['outfile', 'O', 'outfile.log', 'Description...']] subCommands is a list of 4-tuples of (command name, command shortcut, parser class, documentation). If the first non-option argument found is one of the given command names, an instance of the given parser class is instantiated and given the remainder of the arguments to parse and self.opts[command] is set to the command name. For example:: | subCommands = [ | ['inquisition', 'inquest', InquisitionOptions, 'Perform an inquisition'], | ['holyquest', 'quest', HolyQuestOptions, 'Embark upon a holy quest'] | ] In this case, C{" holyquest --horseback --for-grail"} will cause C{HolyQuestOptions} to be instantiated and asked to parse C{['--horseback', '--for-grail']}. Currently, only the first sub-command is parsed, and all options following it are passed to its parser. If a subcommand is found, the subCommand attribute is set to its name and the subOptions attribute is set to the Option instance that parses the remaining options. If a subcommand is not given to parseOptions, the subCommand attribute will be None. You can also mark one of the subCommands to be the default. | defaultSubCommand = 'holyquest' In this case, the subCommand attribute will never be None, and the subOptions attribute will always be set. If you want to handle your own options, define a method named C{opt_paramname} that takes C{(self, option)} as arguments. C{option} will be whatever immediately follows the parameter on the command line. Options fully supports the mapping interface, so you can do things like C{'self["option"] = val'} in these methods. Advanced functionality is covered in the howto documentation, available at U{http://twistedmatrix.com/projects/core/documentation/howto/options.html}, or doc/howto/options.html in your Twisted directory. c Cs tt|ƒiƒ||_h|_g|_d|_h|_h|_ h|_ |i |i |i g}x™|D]‘}|ƒ\}}}}}}|ii|ƒ|i||_|ii|ƒ|ii|ƒ|ii|ƒ|i i|ƒ|i i|ƒqqWdS(Nt(tsuperRtselft__init__toptstdefaultstlongOpttshortOpttdocstsynonymst_Options__dispatcht _gather_flagst_gather_parameterst_gather_handlerst collectorstctsettingstdispatchtextendtupdate( RRRRR RRR R ((RRcs&       cCstt|ƒtiƒS(sÜ Define a custom hash function so that Options instances can be used as dictionary keys. This is an internal feature used to implement the parser. Do not rely on it in application code. N(tinttidRtsystmaxint(R((Rt__hash__ƒscCs|iƒGHtidƒdS(sDisplay this help and exit.iN(Rt__str__Rtexit(R((Rtopt_help‹s cCs*dkl}dG|iGHtidƒdS(N(s copyrightsTwisted version:i(ttwistedt copyrighttversionRR(RR"((Rt opt_versions  cCsE|d jotid}ny%ti||i|iƒ\} }Wn*ti j o} t t | ƒƒ‚nXx·| D]¯\}}|ddjo|d}n |d}|}|ii|ƒp@ti|ddƒ}|ii|ƒpt d|f‚qn|i|}|i|||ƒqwWt|dd ƒoÇ|p|id j o°|p|ig}n|d|d}}x´|iD]b\} } }} || jp || jo6| |_|ƒ|_ ||i _!|i i"|ƒPqŠqŠWt d|ƒ‚n4y|i#|ŒWnt$j ot d ƒ‚nX|i%ƒd S( s-The guts of the command-line parser. it-it_sNo such option '%s't subCommandsisUnknown command: %ssWrong number of arguments.N(&toptionstNoneRtargvtgetoptRR R R targsterrorteRtstrtopttargt optMangledRthas_keytstringtreplaceRtgetattrtdefaultSubCommandtsubtrestR'tcmdtshorttparsertdoct subCommandt subOptionstparentt parseOptionst parseArgst TypeErrort postOptions(RR(R2R<R9R1R8R0R,R;R.R=R:R ((RRA—sL     *     cCsdS(sÛI am called after the options are parsed. Override this method in your subclass to do something after the options have been parsed and assigned, like validate that all options are sane. N((R((RRDÉscCsdS(sI am called with any leftover arguments which were not options. Override me to do something with the remaining arguments on the command line, those which were not flags or options. e.g. interpret them as a list of files to operate on. Note that if there more arguments on the command line than this method accepts, parseArgs will blow up with a getopt.error. This means if you don't override me, parseArgs will blow up if I am passed any arguments at all! N((R((RRBÓs cCs5|djotd||f‚nd|i|MscCs|ƒS(N(Ra(RbRERa((RRcQsiRXRYcCstt|ƒt|ƒƒS(N(tcmptlentatb(RfRg((RRcfsiN(#R R R RRRtdctRLtaddMethodNamesToDictRRNtkeysRbR6tmethodt flagFunctionttakesArgR4R5t prettyNameR)R=tgettfnReRTt reverse_dctR3t cmpLengthtitemstnamestnames_tsorttpoptlongest(RRRRqRhRkR RuRxR RmRpRrRbRR R=RnRt((RR/sZ          cCs|iƒd|iddƒS(Ns twidth(Rt getSynopsistgetUsageR)(R((RRtscCsîdtitidƒ|iodpdf}|id jo5dtitidƒ|iodpdf}nd|iodpd}t |d|ƒ}|i ƒ}|id j o+d i |ii ƒ|ii|fƒ}n|S( s{ Returns a string containing a description of these options and how to pass them to the executed file. s%s%sis [options]Rs Usage: %s%ss%ss [options]tsynopsist N(tpathtbasenameRR*RR R^R@R)R6R|trstriptjoinRzR>(RR^R|((RRzws15 +cCsÈt|dƒo|iid|ƒSn|pttiiddƒƒ}nt|dƒo†g}xW|i D]L\}} }} |ihd|<d| <d| <d d <d d<ƒqmWt||ƒ}d d i|ƒ} nd } h}xo|iiƒD]^\} }|}| |jo!t| ƒdjo| ||   X   0(RRR`R)R>R7R@RRR R$RARDRBRGRIRRRRRzR{(((RR s& >    2     $ E  iPcCs-d} xg|D]_}t|iddƒƒ} | o:|iddƒdjo| d} nt| | ƒ} q q W| tdƒ} || }d| } g}h}x†|D]~}|i|id dƒƒp|i|iddƒƒoq§nxG|id dƒ|iddƒfD]!}|dj od||