Python click, Can you make -h as an alias
Asked Answered
F

2

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?

Fenrir answered 9/12, 2015 at 15:14 Comment(0)
F
31

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
Fenrir answered 9/12, 2015 at 15:29 Comment(2)
By the way, you can also pass context_settings to a group.. just sayin'Sheila
how about -V and --version? any ideas?Dismissal
T
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
Thine answered 26/5 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.