click-contrib / click-default-group

Extends click.Group to invoke a command without explicit subcommand name.
BSD 3-Clause "New" or "Revised" License
73 stars 17 forks source link

Customizing --help command for default groups #14

Open viscrisn opened 4 years ago

viscrisn commented 4 years ago

Is there a way I can redirect one of the subcommand --help output to the group command's --help? For example:

@click.group(cls=DefaultGroup, default='foo', default_if_no_args=True)
def cli():
    pass

@cli.command()
def foo():
    click.echo('foo')

cli --help should output foo --help description.

Is this possible to customize --help in this way?

jpsnyder commented 4 years ago

You can hack this in by inheriting from DefaultGroup and then overwrite the parse_arg() function like so:

    def parse_args(self, ctx, args):
        if (not args or args in (["-h"], ["--help"])) and self.default_if_no_args:
            args.insert(0, self.default_cmd_name)
        return super(DefaultGroup, self).parse_args(ctx, args)

However, this will make it impossible to get the original help messaging containing all the subcommands. So your users will be unaware of the other commands available.

The ideal solution would be to somehow merge the help messages from both the default command and main group. But I'm not sure how to do that...