Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to hide commands and options from help #500

Merged
merged 1 commit into from
Jan 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,11 +737,13 @@ class Command(BaseCommand):
shown on the command listing of the parent command.
:param add_help_option: by default each command registers a ``--help``
option. This can be disabled by this parameter.
:param hidden: hide this command from help outputs.
"""

def __init__(self, name, context_settings=None, callback=None,
params=None, help=None, epilog=None, short_help=None,
options_metavar='[OPTIONS]', add_help_option=True):
options_metavar='[OPTIONS]', add_help_option=True,
hidden=False):
BaseCommand.__init__(self, name, context_settings)
#: the callback to execute when the command fires. This might be
#: `None` in which case nothing happens.
Expand All @@ -757,6 +759,7 @@ def __init__(self, name, context_settings=None, callback=None,
short_help = make_default_short_help(help)
self.short_help = short_help
self.add_help_option = add_help_option
self.hidden = hidden

def get_usage(self, ctx):
formatter = ctx.make_formatter()
Expand Down Expand Up @@ -996,6 +999,8 @@ def format_commands(self, ctx, formatter):
# What is this, the tool lied about a command. Ignore it
if cmd is None:
continue
if cmd.hidden:
continue

help = cmd.short_help or ''
rows.append((subcommand, help))
Expand Down Expand Up @@ -1442,14 +1447,15 @@ class Option(Parameter):
variable in case a prefix is defined on the
context.
:param help: the help string.
:param hidden: hide this option from help outputs.
"""
param_type_name = 'option'

def __init__(self, param_decls=None, show_default=False,
prompt=False, confirmation_prompt=False,
hide_input=False, is_flag=None, flag_value=None,
multiple=False, count=False, allow_from_autoenv=True,
type=None, help=None, **attrs):
type=None, help=None, hidden=False, **attrs):
default_is_missing = attrs.get('default', _missing) is _missing
Parameter.__init__(self, param_decls, type=type, **attrs)

Expand All @@ -1462,6 +1468,7 @@ def __init__(self, param_decls=None, show_default=False,
self.prompt = prompt_text
self.confirmation_prompt = confirmation_prompt
self.hide_input = hide_input
self.hidden = hidden

# Flags
if is_flag is None:
Expand Down Expand Up @@ -1589,6 +1596,8 @@ def add_to_parser(self, parser, ctx):
parser.add_option(self.opts, **kwargs)

def get_help_record(self, ctx):
if self.hidden:
return
any_prefix_is_slash = []

def _write_opts(opts):
Expand Down
44 changes: 44 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,47 @@ def cli(**x):
'normal1',
'missing',
]


def test_hidden_option(runner):
@click.command()
@click.option('--nope', hidden=True)
def cli(nope):
click.echo(nope)

result = runner.invoke(cli, ['--help'])
assert result.exit_code == 0
assert '--nope' not in result.output


def test_hidden_command(runner):
@click.group()
def cli():
pass

@cli.command(hidden=True)
def nope():
pass

result = runner.invoke(cli, ['--help'])
assert result.exit_code == 0
assert 'nope' not in result.output


def test_hidden_group(runner):
@click.group()
def cli():
pass

@cli.group(hidden=True)
def subgroup():
pass

@subgroup.command()
def nope():
pass

result = runner.invoke(cli, ['--help'])
assert result.exit_code == 0
assert 'subgroup' not in result.output
assert 'nope' not in result.output