In python 3.8 I want to define some click options that are common to multiple commands. I tried the following piece of code:
import click
@click.group()
@click.option(
"-v",
"--verbose",
count=True,
default=0,
help="-v for DEBUG",
)
@click.option(
"--path",
help="Main Path.",
)
def cli():
pass
@click.command("list")
@click.option(
"--list-option",
help="Special option for list command.",
)
def my_list_command(verbose, path, list_option):
print(verbose, path, list_option)
@click.command("find")
@click.option(
"--find-option",
help="Special option for find command.",
)
def my_find_command(verbose, path, find_option):
print(verbose, path, find_option)
cli.add_command(my_list_command)
cli.add_command(my_find_command)
if __name__ == '__main__':
cli()
But when I try to run the command
python script.py list
I get an error
TypeError: cli() got an unexpected keyword argument 'verbose'
What I want, is that the command list
has the following three options: verbose
, path
and list-option
and that the command find
has the following three options: verbose
, path
and find-option
. I do not want to define the options for verbose
and path
twice.
Is there a way to do this?
I also tried to use @click.pass_context
but that does not seem to solev the issue.