Hey there Armin, hope you're doing well :smile:
In many cases (as per your Git example), there is a need for certain options to be available across the entire CLI application.
e.g.
Using the exact Git example code provided, the options registered against the global group do not appear in the help of sub-commands.
e.g. when running python git.py --help, we get:
Usage: git.py [OPTIONS] COMMAND [ARGS]...
Options:
--repo-home REPO_HOME
--debug / --no-debug
--help Show this message and exit.
Commands:
clone
and now when running python git.py clone --help, the repo-home and debug options are no longer shown:
Usage: git.py clone [OPTIONS] SRC [DEST]
Options:
--help Show this message and exit.
Any ideas?
Thanks heaps
Fotis
That is correct and intentional. Why should the flags on the outer command show up on the inner command? It's the same behavior as in git:
$ git --git-dir=.foo init
Initialized empty Git repository in /private/tmp/.foo/
$ git init --git-dir=.foo
error: unknown option `git-dir=.foo'
I'd be interested in this as well. Whilst this might be the intentional default behaviour, is there a way to work around this, and inherit group options on child commands? Or is the only way to repeat options on every command?
I have the same problem with likely every CLI program I write with subcommands. As a user I want to have the freedom to place an option "anywhere" (where it logically makes sense) in the command line. Examples:
--debug, --verbose, --silent for global behavior--host or --uri option to specify, which main resource to connect to--username, --password, --token options, whose values are the same for all subcommandsFor what I know, it seems that with Click I only have two choices:
cli --debug foo; invalid would be e.g. cli foo --debug)cli --debug foo and cli foo --debug)Other CLI frameworks, such as Go's Cobra, have "global flags" for that. Is there absolutely no way to do this with Click?
Can we reopen this issue and discuss possible solutions?
We do not plan to change this. A third solution is to write a Command class or decorator that populates a default set of options.
https://stackoverflow.com/questions/32493912/is-it-possible-to-add-a-global-argument-for-all-subcommands-in-click-based-inter SO example that might fit your use case @bittner
Thanks Jay! Unfortunately, the example on SO is a bit hard to grasp.
We should come up with a simple, easy-to-understand solution and add that to the Click documentation. That would already help.
An interesting, related discussion is https://github.com/pallets/click/issues/108#issuecomment-280489786. Includes some usable solutions.
Most helpful comment
I'd be interested in this as well. Whilst this might be the intentional default behaviour, is there a way to work around this, and inherit group options on child commands? Or is the only way to repeat options on every command?