diff options
author | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-04-26 16:19:17 -0400 |
---|---|---|
committer | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-04-26 16:19:17 -0400 |
commit | c7002526d0b5009049c8f9a70ce9020035026b50 (patch) | |
tree | 59652cffc7656b9e8f3dab7dbcca583049549d64 /cliff | |
parent | b5c97668034e455e6e56f037473c7b9da8d8f08a (diff) | |
download | cliff-tablib-c7002526d0b5009049c8f9a70ce9020035026b50.tar.gz |
register a custom help action that knows how to print the list of commands available and a help command to generate help for the other commands
Diffstat (limited to 'cliff')
-rw-r--r-- | cliff/app.py | 19 | ||||
-rw-r--r-- | cliff/help.py | 43 |
2 files changed, 49 insertions, 13 deletions
diff --git a/cliff/app.py b/cliff/app.py index 0610f9e..344d3da 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -7,6 +7,8 @@ import logging.handlers import os import sys +from .help import HelpAction, HelpCommand + LOG = logging.getLogger(__name__) @@ -32,6 +34,7 @@ class App(object): :param stderr: Standard error output stream """ self.command_manager = command_manager + self.command_manager.add_command('help', HelpCommand) self.stdin = stdin or sys.stdin self.stdout = stdout or sys.stdout self.stderr = stderr or sys.stderr @@ -68,7 +71,9 @@ class App(object): ) parser.add_argument( '-h', '--help', - action='help', + action=HelpAction, + nargs=0, + default=self.command_manager, # tricky help="show this help message and exit", ) parser.add_argument( @@ -79,18 +84,6 @@ class App(object): ) return parser - def show_verbose_help(self, *args): - """Displays the normal syntax info and a list of available subcommands. - """ - 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())) - sys.exit(0) - def configure_logging(self): """Create logging handlers for any log output. """ diff --git a/cliff/help.py b/cliff/help.py new file mode 100644 index 0000000..f53e61b --- /dev/null +++ b/cliff/help.py @@ -0,0 +1,43 @@ +import argparse +import sys + +from .command import Command + + +class HelpAction(argparse.Action): + """Provide a custom action so the -h and --help options + to the main app will print a list of the commands. + + The commands are determined by checking the CommandManager + instance, passed in as the "default" value for the action. + """ + def __call__(self, parser, namespace, values, option_string=None): + parser.print_help() + print('') + print('Commands:') + command_manager = self.default + for name, ep in sorted(command_manager): + factory = ep.load() + cmd = factory(self, None) + print(' %-13s %s' % (name, cmd.get_description())) + sys.exit(0) + + +class HelpCommand(Command): + def get_parser(self, prog_name): + parser = super(HelpCommand, self).get_parser(prog_name) + parser.add_argument('cmd', + nargs='*', + help='name of the command', + ) + return parser + + def run(self, parsed_args): + if parsed_args.cmd: + cmd_factory, name, search_args = self.app.command_manager.find_command(parsed_args.cmd) + cmd = cmd_factory(self.app, search_args) + cmd_parser = cmd.get_parser(' '.join([self.app.NAME, name])) + else: + cmd_parser = self.get_parser(' '.join([self.app.NAME, 'help'])) + cmd_parser.parse_args(['--help']) + return 0 |