Reputation: 43
I have a similar problem to the one described in this question, however I have required options before the command and not an argument. I have tried adapting the accepted answer to my situation but can not get it to work, for instance
#! python3
import click
class PerCommandOptWantSubCmdHelp(click.Option):
def handle_parse_result(self, ctx, opts, args):
# check to see if there is a --help on the command line
if any(arg in ctx.help_option_names for arg in args):
# if asking for help see if we are a subcommand name
for arg in opts.values():
if arg in ctx.command.commands:
# this matches a sub command name, and --help is
# present, let's assume the user wants help for the
# subcommand
args = [arg] + args
return super(PerCommandOptWantSubCmdHelp, self).handle_parse_result(ctx, opts, args)
@click.group()
def foo():
pass
@click.group('map')
@click.option('-f', '--force', is_flag=True)
@click.option('-i', '--id')
@click.option('-b', '--base', required=True, cls=PerCommandOptWantSubCmdHelp)
def archive_map(force, id, base):
click.echo('Map called')
volla.add_command(archive_map)
@click.command('bar')
@click.option('-t', '--template', required=True)
@click.option('-p', '--project', required=True)
def bar_command():
pass
archive_map.add_command(bar_command);
if __name__ == '__main__':
foo()
But I still get this behavior
$ ./foo map bar --help
Usage: foo map [OPTIONS] COMMAND [ARGS]...
Try 'foo map --help' for help.
Error: Missing option '-b' / '--base'.
$
Any ideas for what I have misunderstood?
Upvotes: 0
Views: 806
Reputation: 2781
I have done something like this:
class PerCommandOptWantSubCmdHelp(click.Option):
def handle_parse_result(self, ctx, opts, args):
# check to see if there is a --help on the command line
if any(arg in ctx.help_option_names for arg in args):
# if asking for help see if we are a subcommand name
remaining_args = [arg for arg in args if arg not in ctx.help_option_names]
for arg in remaining_args:
if arg in ctx.command.commands:
click.echo(ctx.command.get_help(ctx))
click.echo()
click.echo(f'Command {arg} usage')
click.echo(ctx.command.commands[arg].get_help(ctx))
return super(PerCommandOptWantSubCmdHelp, self).handle_parse_result(ctx, opts, args)
Essentially, if you've invoked the subcommand with help, get the parent's help message, output it, and then output your current subcommand's help message.
Your output will look like this:
Usage: main.py map [OPTIONS] COMMAND [ARGS]...
Options:
-f, --force
-i, --id TEXT
-b, --base TEXT [required]
--help Show this message and exit.
Commands:
bar
Command bar usage:
Usage: main.py map [OPTIONS]
Options:
-t, --template TEXT [required]
-p, --project TEXT [required]
--help Show this message and exit.
Usage: main.py map [OPTIONS] COMMAND [ARGS]...
Try 'main.py map --help' for help.
Error: Missing option '-b' / '--base'.
Does this help?
Upvotes: 1