Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically generate all help documentation for Click commands

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.

like image 672
scottlittle Avatar asked Sep 05 '19 17:09

scottlittle


People also ask

How do I get the help script in PowerShell?

The following command gets the script Help. Because the script is not in a directory that is listed in the Path environment variable, the Get-Help command that gets the script Help must specify the script path. NAME C:\ps-test\Update-Month.ps1 SYNOPSIS Performs monthly data updates.

What is the default CLI method in PowerShell?

click.echo ("This is a simple cli.") This is the default CLI method. Error Handling: Error handling is an important part of a CLI. How your script handles and manages the errors matters a lot and also helps the user to better understand the mistake.

Why should I use click?

Click has strong information available for all parameters and commands so that it can generate unified help pages for the full CLI and to assist the user in converting the input data as necessary. Click has a strong understanding of what types are and can give the user consistent error messages if something goes wrong.

What is click in Python?

Well, click is a Python Package that does just that. There are many Python packages that we could use instead of click such as argparse, docopt, etc., so we will first look at why we are using click.


1 Answers

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)
like image 100
fpbhb Avatar answered Sep 27 '22 20:09

fpbhb