Add the ability to hide commands and options from help
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matt Robenolt · committed by Matt Robenolt · parent 0a7e6ef · Browse files · 2 files·+55 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+55 added−2 removed
- tests:+44 added−0 removed
- other:+11 added−2 removed
Provenance
- Commit
8f4c34f- Tree
40da026- Author
Matt Robenolt <matt@ydekproductions.com>- Committer
Matt Robenolt <matt@ydekproductions.com>- Parent
0a7e6ef
2 files changed+55 added−2 removed
modified:click/core.py+11 added-2 removed
735 unmodified lines736 :param short_help: the short help to use for this command. This is737 shown on the command listing of the parent command.738 :param add_help_option: by default each command registers a ``--help``739 option. This can be disabled by this parameter.740+ :param hidden: hide this command from help outputs.741 """742 743 def __init__(self, name, context_settings=None, callback=None,744 params=None, help=None, epilog=None, short_help=None,744- options_metavar='[OPTIONS]', add_help_option=True):745+ options_metavar='[OPTIONS]', add_help_option=True,746+ hidden=False):747 BaseCommand.__init__(self, name, context_settings)748 #: the callback to execute when the command fires. This might be749 #: `None` in which case nothing happens.750 self.callback = callback7 unmodified lines758 if short_help is None and help:759 short_help = make_default_short_help(help)760 self.short_help = short_help761 self.add_help_option = add_help_option762+ self.hidden = hidden763 764 def get_usage(self, ctx):765 formatter = ctx.make_formatter()766 self.format_usage(ctx, formatter)231 unmodified lines998 cmd = self.get_command(ctx, subcommand)999 # What is this, the tool lied about a command. Ignore it1000 if cmd is None:1001 continue1002+ if cmd.hidden:1003+ continue1004 1005 help = cmd.short_help or ''1006 rows.append((subcommand, help))1007 438 unmodified lines1446 parameter will be pulled from an environment1447 variable in case a prefix is defined on the1448 context.1449 :param help: the help string.1450+ :param hidden: hide this option from help outputs.1451 """1452 param_type_name = 'option'1453 1454 def __init__(self, param_decls=None, show_default=False,1455 prompt=False, confirmation_prompt=False,1456 hide_input=False, is_flag=None, flag_value=None,1457 multiple=False, count=False, allow_from_autoenv=True,1452- type=None, help=None, **attrs):1458+ type=None, help=None, hidden=False, **attrs):1459 default_is_missing = attrs.get('default', _missing) is _missing1460 Parameter.__init__(self, param_decls, type=type, **attrs)1461 1462 if prompt is True:4 unmodified lines1467 prompt_text = prompt1468 self.prompt = prompt_text1469 self.confirmation_prompt = confirmation_prompt1470 self.hide_input = hide_input1471+ self.hidden = hidden1472 1473 # Flags1474 if is_flag is None:1475 if flag_value is not None:119 unmodified lines1595 kwargs['action'] = action1596 parser.add_option(self.opts, **kwargs)1597 1598 def get_help_record(self, ctx):1599+ if self.hidden:1600+ return1601 any_prefix_is_slash = []1602 1603 def _write_opts(opts):1604 rv, any_slashes = join_options(opts)143 unmodified linesmodified:tests/test_basic.py+44 added
395 unmodified lines396 'normal3',397 'normal1',398 'missing',399 ]400+401+402+def test_hidden_option(runner):403+ @click.command()404+ @click.option('--nope', hidden=True)405+ def cli(nope):406+ click.echo(nope)407+408+ result = runner.invoke(cli, ['--help'])409+ assert result.exit_code == 0410+ assert '--nope' not in result.output411+412+413+def test_hidden_command(runner):414+ @click.group()415+ def cli():416+ pass417+418+ @cli.command(hidden=True)419+ def nope():420+ pass421+422+ result = runner.invoke(cli, ['--help'])423+ assert result.exit_code == 0424+ assert 'nope' not in result.output425+426+427+def test_hidden_group(runner):428+ @click.group()429+ def cli():430+ pass431+432+ @cli.group(hidden=True)433+ def subgroup():434+ pass435+436+ @subgroup.command()437+ def nope():438+ pass439+440+ result = runner.invoke(cli, ['--help'])441+ assert result.exit_code == 0442+ assert 'subgroup' not in result.output443+ assert 'nope' not in result.output