Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global options not visible on sub-commands? #66

Closed
fgimian opened this issue May 5, 2014 · 7 comments
Closed

Global options not visible on sub-commands? #66

fgimian opened this issue May 5, 2014 · 7 comments

Comments

@fgimian
Copy link
Contributor

fgimian commented May 5, 2014

Hey there Armin, hope you're doing well 😄

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.

  • Enabling or disabling colour
  • Verbose switch
  • Help

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

@mitsuhiko
Copy link
Contributor

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'

@tekumara
Copy link

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?

@bittner
Copy link
Contributor

bittner commented Aug 14, 2020

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:

  • Flags such as --debug, --verbose, --silent for global behavior
  • A --host or --uri option to specify, which main resource to connect to
  • --username, --password, --token options, whose values are the same for all subcommands

For what I know, it seems that with Click I only have two choices:

  1. Expect the user to use the option of the lowest group (e.g. cli --debug foo; invalid would be e.g. cli foo --debug)
  2. Repeat those options on each group in the source code (e.g. to allow both 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?

@davidism
Copy link
Member

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.

@jcrotts
Copy link
Contributor

jcrotts commented Aug 15, 2020

@bittner
Copy link
Contributor

bittner commented Aug 16, 2020

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.

@bittner
Copy link
Contributor

bittner commented Aug 16, 2020

An interesting, related discussion is #108 (comment). Includes some usable solutions.

bittner added a commit to vshn/concierge-cli that referenced this issue Aug 18, 2020
We would need options such as `--debug` to be global options.
Unfortunately, Click doesn't support global options (or group
options that are inherited). A deliberate design decision, as
a core developer explains.

See pallets/click#66
bittner added a commit to vshn/concierge-cli that referenced this issue Aug 18, 2020
We would need options such as `--debug` to be global options.
Unfortunately, Click doesn't support global options (or group
options that are inherited). A deliberate design decision, as
a core developer explains.

See pallets/click#66
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants