[Twisted-Python] twisted.python.usage.Options Questions
Justin Johnson
justinjohnson at fastmail.fm
Fri May 16 07:05:47 MDT 2003
Greetings,
I'm using twisted.python.usage to handle command-line option parsing.
The script I have uses subcommands, so I can type:
myscript.py subcommand -a 1 -b 2 ...
I sub-classed BaseOptions as follows...
[code]
# Setup command-line options for all subcommands
class BaseOptions(usage.Options):
optFlags = [
['help', 'h', "Display usage information"],
]
class MkvobOptions(BaseOptions):
optParameters = [
['vobs', 'v', None, "Comma-delimited list of VOBs to create"],
['original', 'o', None, "Original site to create VOBs at first"],
['sites', 's', None, "Comma-delimited list of sites to replicate
to"],
['group', 'g', r'UHCADAAA\ccusers', "NT group to be primary group for VOBs"]
]
def postOptions(self):
for opt in ["vobs","original"]:
if not self[opt]:
raise usage.UsageError, "--vobs and --original are required."
class RmvobOptions(BaseOptions):
optParameters = [
['vobs', 'v', None, "Comma-delimited list of VOBs to remove"],
['original', 'o', None, "Site to remove VOBs from"],
]
def postOptions(self):
for opt in ["vobs","original"]:
if not self[opt]:
raise usage.UsageError, "--vobs and --original are required."
class Options(usage.Options):
subCommands = [
['mkvob', None, MkvobOptions, "Create VOBs"],
['rmvob', None, RmvobOptions, "Remove VOBs"]
]
[/code]
I have some questions though.
1) Passing in the --help option displays usage info the way I'd want it,
grabbing __doc__ from the top of the file and also displaying info about
my subcommands.
[output]
C:\share\python\uhg\uht\tsd\ClearCase>vobtool.py --help
Usage: vobtool.py [options]
Options:
--version
--help Display this help and exit.
VOB Administrative Tool <<< This is my __doc__
Commands:
mkvob Create VOBs
rmvob Remove VOBs
C:\share\python\uhg\uht\tsd\ClearCase>
[/output]
But when I pass the --help option to a subcommand, I'd like to have some
sort of __doc__ equivalent for the subcommand, but I get the following
instead.
[output]
C:\share\python\uhg\uht\tsd\ClearCase>vobtool.py mkvob --help
Usage: vobtool.py [options]
Options:
-h, --help Display this help and exit.
-v, --vobs= Comma-delimited list of VOBs to create
-o, --original= Original site to create VOBs at first
-s, --sites= Comma-delimited list of sites to replicate to
-g, --group= NT group to be primary group for VOBs [default:
UHCADAAA\ccusers]
--version
VOB Administrative Tool <<< The __doc__ for the file
[/output]
Is there a way to do that?
2) When I just run the command without passing any options (not even a
subcommand) I get the following error:
[output]
C:\share\python\uhg\uht\tsd\ClearCase>vobtool.py
Traceback (most recent call last):
File "C:\share\python\uhg\uht\tsd\ClearCase\vobtool.py", line 121, in ?
main()
File "C:\share\python\uhg\uht\tsd\ClearCase\vobtool.py", line 55, in
main
if config.subCommand == 'mkvob':
File "C:\Python22\Lib\site-packages\twisted\python\usage.py", line 151,
in __g
etattr__
raise AttributeError("%s instance has no attribute '%s'" %
(self.__class__,
attr))
AttributeError: __main__.Options instance has no attribute 'subCommand'
C:\share\python\uhg\uht\tsd\ClearCase>
[/output]
My main function is as follows...
[snip]
# Main function
def main():
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 == 'mkvob':
doMkvob(config.subOptions)
elif config.subCommand == 'rmvob':
doRmvob(config.subOptions)
[snip]
Is there a particular test I should do on config before checking
config.subCommand?
3) I see there is a --version option available if you use
twisted.python.usage. But it always displays the Twisted version. Is
there a way to override that so the --version option either isn't
available or at least returns the version information for my script and
not for Twisted?
Sorry for the long email.
Thanks so much.
-Justin
More information about the Twisted-Python
mailing list