1

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 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

1 Comment

What if test1() has its own @cli.command() and click.option()s?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.