1
1
import traceback
2
+ import warnings
2
3
3
4
import click
4
5
from docutils import nodes
11
12
LOG = logging .getLogger (__name__ )
12
13
CLICK_VERSION = tuple (int (x ) for x in click .__version__ .split ('.' )[0 :2 ])
13
14
15
+ NESTED_FULL = 'full'
16
+ NESTED_SHORT = 'short'
17
+ NESTED_NONE = 'none'
18
+
14
19
15
20
def _indent (text , level = 1 ):
16
21
prefix = ' ' * (4 * level )
@@ -263,7 +268,7 @@ def _filter_commands(ctx, commands=None):
263
268
return [lookup [name ] for name in names if name in lookup ]
264
269
265
270
266
- def _format_command (ctx , show_nested , commands = None ):
271
+ def _format_command (ctx , nested , commands = None ):
267
272
"""Format the output of `click.Command`."""
268
273
if CLICK_VERSION >= (7 , 0 ) and ctx .command .hidden :
269
274
return
@@ -318,7 +323,7 @@ def _format_command(ctx, show_nested, commands=None):
318
323
yield line
319
324
320
325
# if we're nesting commands, we need to do this slightly differently
321
- if show_nested :
326
+ if nested in ( NESTED_FULL , NESTED_NONE ) :
322
327
return
323
328
324
329
commands = _filter_commands (ctx , commands )
@@ -337,14 +342,29 @@ def _format_command(ctx, show_nested, commands=None):
337
342
yield ''
338
343
339
344
345
+ def nested (argument ):
346
+ values = (NESTED_FULL , NESTED_SHORT , NESTED_NONE )
347
+ if not argument :
348
+ return None
349
+
350
+ if argument not in values :
351
+ raise ValueError (
352
+ "%s is not a valid value for ':nested:'; allowed values: %s"
353
+ % directives .format_values (values )
354
+ )
355
+
356
+ return argument
357
+
358
+
340
359
class ClickDirective (rst .Directive ):
341
360
342
361
has_content = False
343
362
required_arguments = 1
344
363
option_spec = {
345
364
'prog' : directives .unchanged_required ,
346
- 'show- nested' : directives . flag ,
365
+ 'nested' : nested ,
347
366
'commands' : directives .unchanged ,
367
+ 'show-nested' : directives .flag ,
348
368
}
349
369
350
370
def _load_module (self , module_path ):
@@ -387,17 +407,15 @@ def _load_module(self, module_path):
387
407
)
388
408
return parser
389
409
390
- def _generate_nodes (
391
- self , name , command , parent = None , show_nested = False , commands = None
392
- ):
410
+ def _generate_nodes (self , name , command , parent , nested , commands = None ):
393
411
"""Generate the relevant Sphinx nodes.
394
412
395
413
Format a `click.Group` or `click.Command`.
396
414
397
415
:param name: Name of command, as used on the command line
398
416
:param command: Instance of `click.Group` or `click.Command`
399
417
:param parent: Instance of `click.Context`, or None
400
- :param show_nested: Whether subcommands should be included in output
418
+ :param nested: The granularity of subcommand details.
401
419
:param commands: Display only listed commands or skip the section if
402
420
empty
403
421
:returns: A list of nested docutil nodes
@@ -421,7 +439,7 @@ def _generate_nodes(
421
439
source_name = ctx .command_path
422
440
result = statemachine .ViewList ()
423
441
424
- lines = _format_command (ctx , show_nested , commands )
442
+ lines = _format_command (ctx , nested , commands )
425
443
for line in lines :
426
444
LOG .debug (line )
427
445
result .append (line , source_name )
@@ -430,12 +448,10 @@ def _generate_nodes(
430
448
431
449
# Subcommands
432
450
433
- if show_nested :
451
+ if nested == NESTED_FULL :
434
452
commands = _filter_commands (ctx , commands )
435
453
for command in commands :
436
- section .extend (
437
- self ._generate_nodes (command .name , command , ctx , show_nested )
438
- )
454
+ section .extend (self ._generate_nodes (command .name , command , ctx , nested ))
439
455
440
456
return [section ]
441
457
@@ -449,9 +465,23 @@ def run(self):
449
465
450
466
prog_name = self .options .get ('prog' )
451
467
show_nested = 'show-nested' in self .options
468
+ nested = self .options .get ('nested' )
469
+
470
+ if show_nested :
471
+ if nested :
472
+ raise self .error (
473
+ "':nested:' and ':show-nested:' are mutually exclusive"
474
+ )
475
+ else :
476
+ warnings .warn (
477
+ "':show-nested:' is deprecated; use ':nested: full'" ,
478
+ DeprecationWarning ,
479
+ )
480
+ nested = NESTED_FULL if show_nested else NESTED_SHORT
481
+
452
482
commands = self .options .get ('commands' )
453
483
454
- return self ._generate_nodes (prog_name , command , None , show_nested , commands )
484
+ return self ._generate_nodes (prog_name , command , None , nested , commands )
455
485
456
486
457
487
def setup (app ):
0 commit comments