0

I'm creating a deeply nested set of commands as click.group()s. I would like to ONLY execute the last group (command) input to the cli when I press the Enter key.

For instance:

cli sub_command subsub_command # < -- should only execute subsub_command

... should ONLY execute the last command subsub_command, however, it appears that click want's to execute the full stack of commands. (oddly it excludes subsub_command?):

$ cli sub-command subsub-command 
I am the root_command
I am sub_command
Usage: cli sub-command subsub-command [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

It also appears that it is running everything EXCEPT for the last command. Why is it displaying the help for subsub_command instead of simply executing it?

Here is my click code:

import os
import sys

import click

@click.group(invoke_without_command=True)
def cli():
    print('I am the root_command')

@cli.group()
def sub_command(invoke_without_command=True):
    print('I am sub_command')

@sub_command.group()
def subsub_command(invoke_without_command=True):
    print('I am the subsub_command')

if __name__ == '__main__':
    cli()

Any thoughts are helpful. Thanks!

1 Answer 1

1

That's because you are using @cli.group over and over.

Commands are defined with @cli.command

So for example:

import click


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


@cli.command(name='hello')
def hello():
    print('hello world!')

The idea of @group is to combine multiple commands together, the group method is used for defining a common context code for the entire group.

Sign up to request clarification or add additional context in comments.

9 Comments

@orvelli - Thanks! yeah, I want to use the click.group simply as a hierarchy. This example doesn't show it, but I am generating these commands / groups dynamically, so I won't know ahead of time what is a leaf / command and what is a group. I had hoped that I could simply put in an arbitrary depth command and have it execute the command at that level. Basically a tree of executable click.groups. Any ideas?
What is the use-case of generating dynamic commands?
Anyway, you can always use sys.argv[-1] to get the last subcommand name. You will have to add a condition on each group to verify if you need to execute the code
looks like the class Multicommand has a invoke_without_command, but that doesn't seem to make any difference.
The use-case is that I have an already generated, quite complex, tree of nodes in a custom object that I am passing in to generate the commands. Each node contains the kwargs and executable code. ultimately, I will be calling mylib.execute(path="|cli|sub_command|subsub_command") with that path being specified by the cli.
|

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.