main: Add help subcommand

Add a new "help" subcommand which prints the help text for any of the
existing commands, by invoking them with "-h".

Signed-off-by: Marti Bolivar <marti@foundries.io>
Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
This commit is contained in:
Marti Bolivar 2019-02-20 01:03:05 +01:00 committed by Carles Cufí
parent 047a23d319
commit 64ffb67f46
1 changed files with 38 additions and 4 deletions

View File

@ -52,10 +52,11 @@ HIDDEN_COMMANDS = {None: [PostInit()]}
BUILTIN_COMMANDS = dict(PROJECT_COMMANDS) BUILTIN_COMMANDS = dict(PROJECT_COMMANDS)
BUILTIN_COMMANDS.update(HIDDEN_COMMANDS) BUILTIN_COMMANDS.update(HIDDEN_COMMANDS)
# Initialize the set with the virtual 'help' command so that an external
BUILTIN_COMMAND_NAMES = set() # command cannot clash with it
BUILTIN_COMMAND_NAMES = set(['help'])
for group, commands in BUILTIN_COMMANDS.items(): for group, commands in BUILTIN_COMMANDS.items():
BUILTIN_COMMAND_NAMES.add(c.name for c in commands) BUILTIN_COMMAND_NAMES.update(c.name for c in commands)
class InvalidWestContext(RuntimeError): class InvalidWestContext(RuntimeError):
@ -264,7 +265,8 @@ def _make_parsers():
# showing up when West is run via the wrapper # showing up when West is run via the wrapper
parser = WestArgumentParser( parser = WestArgumentParser(
prog='west', description='The Zephyr RTOS meta-tool.', prog='west', description='The Zephyr RTOS meta-tool.',
epilog='Run "west <command> -h" for detailed help on each command.', epilog='''Run "west <command> -h" or "west help <command>" for detailed
help on each command.''',
add_help=False) add_help=False)
# Remember to update scripts/west-completion.bash if you add or remove # Remember to update scripts/west-completion.bash if you add or remove
@ -327,6 +329,32 @@ def ext_command_handler(spec, argv, *ignored):
command.run(*west_parser.parse_known_args(argv)) command.run(*west_parser.parse_known_args(argv))
def help_command_handler(west_parser, help_parser, externals, args, *ignored):
command_name = args.command
if not command_name:
west_parser.print_help(top_level=True)
return
if command_name in BUILTIN_COMMAND_NAMES:
if command_name == 'help':
help_parser.print_help()
return
for group, commands in BUILTIN_COMMANDS.items():
for command in commands:
if command.name == command_name:
command.parser.print_help()
return
else:
for path, specs in externals.items():
for spec in specs:
if spec.name != command_name:
continue
# ext_command_handler() does not return
ext_command_handler(spec, [command_name, '--help'])
else:
west_parser.print_help(top_level=True)
def set_zephyr_base(args): def set_zephyr_base(args):
'''Ensure ZEPHYR_BASE is set, emitting warnings if that's not '''Ensure ZEPHYR_BASE is set, emitting warnings if that's not
possible, or if the user is pointing it somewhere different than possible, or if the user is pointing it somewhere different than
@ -426,6 +454,12 @@ def parse_args(argv, externals):
spec, argv)) spec, argv))
west_parser.set_externals(externals) west_parser.set_externals(externals)
help_parser = subparser_gen.add_parser('help',
help='get help on a west command')
help_parser.add_argument('command', nargs='?')
help_parser.set_defaults(handler=partial(help_command_handler, west_parser,
help_parser, externals))
# Parse arguments. # Parse arguments.
args, unknown = west_parser.parse_known_args(args=argv) args, unknown = west_parser.parse_known_args(args=argv)