1
votes

Attempt to run test program (shown later below) results in error:

$ python -m test test2 --all

test #2 True

Usage: main.py [OPTIONS]

Try "main.py --help" for help.

Error: no such option: --all

However, running test program with click "--help" option clearly shows that "-all" is an available option on the test2 command.

$ python -m test test2 --help

Usage: main.py test2 [OPTIONS]

Options:
--all
--help Show this message and exit.

import click

@click.group()
def cli():
    pass

@cli.command()
def test1():
    print("test #1")

@cli.command()
@click.option("--all", is_flag=True)
def test2(all):
    print("test #2", all)
    if all:
        test1()


if __name__ == '__main__':
    cli()
1

1 Answers

4
votes

After some basic research and a bunch of print statements, finally realized that having command "test2" call command "test1" was not a good idea. Apparently when the test1() call happens, click's decorators assist by passing in the options and the "test1" command does not recognize the "--all" option, hence the error.

Better way to set up code is to pull out the stuff that test1() does, and put into a helper like _test1() and use that in both test1() and test2():

import click

@click.group()
def cli():
    pass

@cli.command()
def test1():
    print("test #1")

@cli.command()
@click.option("--all", is_flag=True)
def test2(all):
    print("test #2", all)
    if all:
        _test1()

def _test1():
    """ worker for test1 """
    print("test #1")

if __name__ == '__main__':
    cli()