Click: Global options not visible on sub-commands?

Created on 5 May 2014  路  7Comments  路  Source: pallets/click

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.

  • 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

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?

All 7 comments

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:

  • 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?

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.

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.

Was this page helpful?
0 / 5 - 0 ratings