Search code examples
pythonpython-click

adding help parameter to @app.cli.command()


So I got the following click command working

from flask import Flask

app = Flask(__name__)

@app.cli.command()
def hello():
    print("hello from inside flask")

then tried to add a 'help' to it like this:

@app.cli.command()
@app.cli.argument(help='test command to say hello from inside flask')
def hello():
    print("hello from inside flask")

..but it said

AttributeError: 'AppGroup' object has no attribute 'argument'

I believe this supposed to work if you have

import click
@click.option(help="use this command like this")

or

@click.argument(help="use this command like this")

Does anyone know how to get it to work if you have @app.cli.command() ?


Solution

  • Click does not support passing a help argument to click.argument, only to click.option. This is referenced in a few places in the docs:

    https://click.palletsprojects.com/en/7.x/documentation/#help-texts

    Arguments cannot be documented this way. This is to follow the general convention of Unix tools of using arguments for only the most necessary things and to document them in the introduction text by referring to them by name.

    https://click.palletsprojects.com/en/7.x/parameters/#differences

    options are fully documented in the help page, arguments are not (this is intentional as arguments might be too specific to be automatically documented)

    https://click.palletsprojects.com/en/7.x/arguments/

    Click will also not attempt to document arguments for you and wants you to document them manually in order to avoid ugly help pages.

    Instead, Click would like you to document arguments directly in the command's docstring. This is because the arguments are generally integrated more closely with the command help text, so Click doesn't try to write that itself.

    import click
    
    @click.command()
    @click.argument("name")
    def hello(name):
        """Say hello to NAME."""
        click.echo(f"Hello, {name}!")
    

    You can find discussions and some examples adding argument help on Click's issue tracker: https://github.com/pallets/click/pull/1051. There are not currently plans to change how Click handles this though.