3

is there a way in Python Click library change the execution order?

I want to have cli my_command --options --options

now I have cli --options --options my_command

I don't want a command to be called at the end.

1 Answer 1

1

The structure of a click command is as follows:

command <options> subcommand <subcommand options>

I'm not sure how you would have two options with the same name for one command. However, the two '--options' options apply to the 'cli' command and not your 'my_command' command.

To achieve something like what you want:

import click

@click.group()
@click.option('--options/--not-options', default=False)
def cli(options):
    if options:
        click.echo("Recieved options")

@cli.command()
@click.option('--options/--not-options', default=False)
def my_command(options):
    if options:
        click.echo("Recieved options")

if __name__ == '__main__':
    cli(obj={})

To run this from the terminal (filename replaces the 'cli' command entrypoint):

python mytool.py --options my-command --options

>>Recieved options
>>Received options
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I've done the same way. I found a solution in Click documentation, but you also right

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.