Skip to content

Commit 3caea15

Browse files
James Ennisstephenfin
authored andcommitted
Do not show hidden subcommands as valid command options
1 parent b5e5f89 commit 3caea15

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

sphinx_click/ext.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ def _format_command(ctx, show_nested, commands=None):
271271
yield ''
272272

273273
for command in commands:
274+
# Don't show hidden subcommands
275+
if CLICK_VERSION >= (7, 0):
276+
if command.hidden:
277+
continue
278+
274279
for line in _format_subcommand(command):
275280
yield line
276281
yield ''

tests/test_formatter.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,58 @@ def get_command(self, ctx, name):
396396
397397
A world command.
398398
""").lstrip(), '\n'.join(output))
399+
400+
401+
@unittest.skipIf(ext.CLICK_VERSION < (7, 0),
402+
'The hidden flag was added in Click 7.0')
403+
def test_hidden(self):
404+
"""Ensure 'hidden' subcommands are not shown."""
405+
@click.command()
406+
def hello():
407+
"""A sample command."""
408+
409+
@click.command()
410+
def world():
411+
"""A world command."""
412+
413+
@click.command(hidden=True)
414+
def hidden():
415+
"""A hidden command."""
416+
417+
class MyCLI(click.MultiCommand):
418+
_command_mapping = {
419+
'hello': hello,
420+
'world': world,
421+
'hidden': hidden,
422+
}
423+
424+
def list_commands(self, ctx):
425+
return ['hello', 'world', 'hidden']
426+
427+
def get_command(self, ctx, name):
428+
return self._command_mapping[name]
429+
430+
cli = MyCLI(help='A sample custom multicommand.')
431+
ctx = click.Context(cli, info_name='cli')
432+
output = list(ext._format_command(ctx, show_nested=False))
433+
434+
# Note that we do NOT expect this to show the 'hidden' command
435+
self.assertEqual(
436+
textwrap.dedent("""
437+
A sample custom multicommand.
438+
439+
.. program:: cli
440+
.. code-block:: shell
441+
442+
cli [OPTIONS] COMMAND [ARGS]...
443+
444+
.. rubric:: Commands
445+
446+
.. object:: hello
447+
448+
A sample command.
449+
450+
.. object:: world
451+
452+
A world command.
453+
""").lstrip(), '\n'.join(output))

0 commit comments

Comments
 (0)