0

Is there a way to group multiple commands, each with their own different parameters under a single function.

At first glance, a MultiCommand or Group might seem like a natural way of doing what I'd like, i.e. have a single main command act as the Group (and pass the invoke_without_command=True flag) then nest auxiliary Commands beneath it as subcommands. However this doesn't quite have the behavior that I'd want, since I'd like all the options from all commands to be able to be specified without explicitly invoking a subcommand. Additionally, using a Group would also not display the help text of the subcommands without invoking the subcommand on the command line as well.

I guess what I'd ideally like to have is a way to group multiple commands together without the nesting inherent to Click's Group API.

Sorry if this question might be somewhat general. Any help, ideas or tips that can point me in the right direction would be much appreciated.

Here's an outline of what I'd like (file name: cli_test.py):

import click


@click.command()
@click.option('--db-file', type=click.File(mode='r'))
def db_reader(db_file):
    click.echo(db_file)


@click.command()
@click.option('--xval', type=float)
@click.option('--yval', type=float)
def get_vals(xval, yval):
    return xval, yval


@click.command()
@click.option('--absolutize/--no-absolutize')
def flagger(absolutize):
    click.echo(absolutize)


@click.command()
def cli_runner():
    db = db_reader.main(standalone_mode=False)
    vals = flagger.main(standalone_mode=False)
    flag = flagger.main(standalone_mode=False)


if __name__ == '__main__':
    cli_runner()

Essentially, I'd like a single command that can be run on the CLI (cli_runner in the above example), which would be able to take the parameters of all Click commands called within it, and then dispatch the processing of them to the appropriate Command instance. However as it stands right now, if I were to invoke on the CLI: $ python cli_test.py --xval 4 I'd get the error Error: no such option: --xval. I have also tried playing around with the pass_context and then ctx.invoke() approach, but the same problem exists.

I suppose I could pass parameters from all contained commands into cli_runner, but that would defeat the purpose of what I want to do, which is to ultimately have 3-4 modular "subcommands", that can then be grouped together in different combinations into larger interfaces that serve slightly different use cases.

3
  • 1
    Any chance you can expand this question to show EXACTLY what you are trying to achieve? Maybe the desired help, and the desired commands. Also some CONCRETE examples would be very helpful. Commented Feb 28, 2020 at 3:45
  • @StephenRauch I added a snippet of what I'd like to have Commented Feb 28, 2020 at 7:08
  • @StephenRauch I sort of found a workaround for doing what I'd want in this question, but ran into another problem during its implementation. I made a post here: stackoverflow.com/q/60469503/5437918, and would appreciate it if you could weight in. Commented Feb 29, 2020 at 20:51

0

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.