19

I have recently found the click library (http://click.pocoo.org/6/) and I love it.

I am trying to figure out if it is possible to create an alias for the --help option which shortcuts to the help. So, for example:

app.py --help 

gives the help for the main app and

app.py sub --help 

will give the help for the sub. I want to be able to use -h as well. If I were creating the option, it may look something like:

@click.option('-h', '--help') 

but the --help option is built in. Is there a way to extend that option or create an alias for it?

2 Answers 2

32

Well, I found it:

https://click.palletsprojects.com/en/7.x/documentation/#help-parameter-customization

@click.command(context_settings=dict(help_option_names=["-h", "--help"])) def cli(): pass 
Sign up to request clarification or add additional context in comments.

2 Comments

By the way, you can also pass context_settings to a group.. just sayin'
how about -V and --version? any ideas?
0

thank you, using it already

as I wanted -v, and someone asked -V, for version_option

import click import cards # CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]} PARAM_DECLS_VERSION = ("-V", "--version") @click.group(context_settings=CONTEXT_SETTINGS) @click.version_option(cards.__version__, *PARAM_DECLS_VERSION) def cli() -> None: """CLI main group.""" 

for param_decls I used a tuple as in the docs and in the library code, to remember that it's positional after version

(for context_settings used different syntax than the docs to make Ruff happy)

outs:

$ .venv/bin/cards Usage: cards [OPTIONS] COMMAND [ARGS]... CLI main group. Options: -v, --version Show the version and exit. -h, --help Show this message and exit. 

$ .venv/bin/cards -v cards, version 0.1.0 

$ .venv/bin/cards --version cards, version 0.1.0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.