Click: use -h as a shorthand for --help?

Created on 8 May 2014  路  10Comments  路  Source: pallets/click

Is it possible to use -h as a shorthand for --help? or is it better to even make that default?

Thanks,

All 10 comments

No, i intentionally did not make it a default because -h should not be the help option. You can manually make it the default by removing the help option with a parameter and adding it back with the help_option decorator and by passing in the arguments manually.

Thanks for the clarification!

How do you remove the default '--help' option? I've added @click.help_option('-h', '--help') to my commands, but this just adds an additional help option instead of overriding the default.

No, it did not add to the default, it changed the default. If you just want '-h' then just list -h:

@click.help_option('-h')

Note though that with click 2.0 there are better ways:

http://click.pocoo.org/documentation/#help-parameter-customization

I'm trying to get '-h' and '--help' to work. Here's my code:

@click.group()
@click.help_option('-h', '--help')
@click.pass_context
def cli(context):
    ....

If I run 'myapp -h' I get:

Usage: myapp [OPTIONS] COMMAND [ARGS]...

  ...

Options:
  -h, --help                 Show this message and exit.
  --help                     Show this message and exit.

Commands:
    ...

I'm staying on click 1.1 until 2.0 is in pypi. How can I remove the duplicate message for just '--help'?

Great library BTW, thanks!

Have a look at the link I sent before. This is how you can do it now:

CONTEXT_SETTINGS = dict(help_option_names=['-h'])

@click.group(context_settings=CONTEXT_SETTINGS)
@click.pass_context
def cli(context):
    ....

I tried it but got an error:

  ...
  File "cli.py", line 20, in <module>
    @click.pass_context
  File "/venv/lib/python2.7/site-packages/click/decorators.py", line 110, in decorator
    return _make_command(f, name, attrs, cls)
  File "/venv/lib/python2.7/site-packages/click/decorators.py", line 84, in _make_command
    callback=f, params=params, **attrs)
  File "/venv/lib/python2.7/site-packages/click/core.py", line 646, in __init__
    MultiCommand.__init__(self, name, **attrs)
  File "/venv/lib/python2.7/site-packages/click/core.py", line 549, in __init__
    Command.__init__(self, name, **attrs)
TypeError: __init__() got an unexpected keyword argument 'context_settings'

Not to worry. I see 2.0 is in pypi now. I've upgraded and your instructions work. Thanks.

Is it possible to setup it once globally instead of bloating every decorator?

@mitsuhiko @click.group(context_settings=CONTEXT_SETTINGS) is undocumented https://click.palletsprojects.com/en/7.x/api/#click.Group

Was this page helpful?
0 / 5 - 0 ratings