Automatically generate all help documentation for Click commands
Asked Answered
Y

2

8

Is there a way to generate (and export) help documentation using click for all commands and subcommands?

For example,

cli --help all --destination help-docs.txt

would generate help for commands and subcommands following the

cli command subcommand

format and put them into the help-docs.txt file.

The only way I can think that I would accomplish this is to use

cli command subcommand --help

on every subcommand that I wanted to generate help for and cat the output to a file, but it would be nice if there where an easier way to accomplish this using Click --help functionality.

Yeomanry answered 5/9, 2019 at 17:46 Comment(2)
I do not think this is natively possible. Would a solution requiring program changes be acceptable?Skewback
@StephenRauch yesYeomanry
H
9

This code will do for Click 7, using mostly documented APIs. You'd basically call recursive_help somewhere, e.g. as a separate subcommand, and pass it your top-level group object.

def recursive_help(cmd, parent=None):
    ctx = click.core.Context(cmd, info_name=cmd.name, parent=parent)
    print(cmd.get_help(ctx))
    print()
    commands = getattr(cmd, 'commands', {})
    for sub in commands.values():
        recursive_help(sub, ctx)

Update 2019-10-05: one way to use this, assuming cli is a click.group, would be:

@cli.command()
def dumphelp():
    recursive_help(cli)
Hades answered 19/9, 2019 at 20:49 Comment(0)
E
2

Since you are using click package, I know two cool solutions:

  1. Is to use click-man to auto-generate Python click CLI man page.
  2. Is to use md-click to auto-generate Python click CLI help in md file format.
Erk answered 7/8, 2022 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.