[Twisted-Python] twisted python usage

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Fri Oct 2 12:00:25 MDT 2009


On 05:47 pm, vitaly at synapticvision.com wrote:
>Quoting "Glyph Lefkowitz" <glyph at twistedmatrix.com>:
>>On Fri, Oct 2, 2009 at 12:47 PM, <vitaly at synapticvision.com> wrote:
>>>hi,
>>Hi, Vitaly!
>>>I'm trying to accomplish to work couple of CLI commands without luck.
>>
>>>http://twistedmatrix.com/projects/core/documentation/howto/options.htmland
>>>test/test_usage.py were not really helpful.
>>
>>Why weren't these helpful?  To me, they seem to clearly explain 
>>everything
>>you're trying to do.
>
>First of all - I'm sorry, I've succeed to accomplish first two
>commands with previous doc link partially: I can't read value per key
>...
>
>Trying to follow the following snip, how can I read for example value
>of key -m?
>CLI: python test.py import -m 'abc321'
>
>from twisted.python import usage
>import sys
>
>class ImportOptions(usage.Options):
>     optParameters = [
>         ['module',  'm', 1, None],
>         ['vendor',  'v', None, None],
>         ['release', 'r', None]
>     ]

I think the idiomatic approach would be to add another method to this 
class to replace the "doImport" function below:

    def execute(self):
        print "My -m option was", repr(self['module'])
>
>class CheckoutOptions(usage.Options):
>     optParameters = [['module', 'm', None, None], ['tag', 'r', None, 
>None]]

And again here, replacing "doCheckout":

    def execute(self):
        print "*My* -m option was", repr(self['module'])
>
>class Options(usage.Options):
>     subCommands = [['import', None, ImportOptions, "Do an Import"],
>                    ['checkout', None, CheckoutOptions, "Do a 
>Checkout"]]
>
>     optParameters = [
>         ['compression', 'z', 0, 'Use compression'],
>         ['repository', 'r', None, 'Specify an alternate repository']
>     ]
>
>def doImport():
>     print "TODO: how can I now read the value of key, let's say of -m 
>key?"
>
>def doCheckout():
>     print "TODO: same as doImport()"
>
>config = Options()
>try:
>     config.parseOptions()
>except usage.UsageError, errortext:
>     print '%s: %s' % (sys.argv[0], errortext)
>     print '%s: Try --help for usage details.' % (sys.argv[0])
>     sys.exit(1)
>
>if config.subCommand == 'import':
>     doImport(config.subOptions)
>elif config.subCommand == 'checkout':
>     doCheckout(config.subOptions)

Then replace this thing with this:

    config.subOptions.execute()

But the answer to your original question seems to just be 
"options[optionName]".

Jean-Paul




More information about the Twisted-Python mailing list