replace default --help processor with one that includes the list of subcommands available

This commit is contained in:
Doug Hellmann 2012-04-20 22:20:17 -07:00
parent e5c6c4c918
commit 417c4f7bac
3 changed files with 24 additions and 0 deletions

View File

@ -23,6 +23,7 @@ class App(object):
self.parser = optparse.OptionParser(
description=description,
version='%prog {}'.format(version),
add_help_option=False,
)
self.parser.disable_interspersed_args()
self.parser.add_option(
@ -31,8 +32,27 @@ class App(object):
dest='verbose',
help='Increase verbosity of output. Can be repeated.',
)
self.parser.add_option(
'-h', action='help',
help="show this help message and exit",
)
self.parser.add_option(
'--help', action='callback',
callback=self.show_verbose_help,
help="show verbose help message and exit",
)
return
def show_verbose_help(self, *args):
self.parser.print_help()
print('')
print('Commands:')
for name, ep in sorted(self.command_manager):
factory = ep.load()
cmd = factory(self, None)
print(' %-13s %s' % (name, cmd.get_description()))
raise SystemExit()
def run(self, argv):
parsed_args, remainder = self.parser.parse_args(argv)
# FIXME(dhellmann): set up logging based on verbosity flag

View File

@ -24,6 +24,9 @@ class CommandManager(object):
self.commands[ep.name.replace('_', ' ')] = ep
return
def __iter__(self):
return iter(self.commands.items())
def find_command(self, argv):
"""Given an argument list, find a command and
return the processor and any remaining arguments.

View File

@ -3,6 +3,7 @@ from cliff.command import Command
class Simple(Command):
"A simple command that prints a message."
def run(self, parsed_args):
print 'hi!'