Search code examples
pythonpython-click

How to allow either "-h" or "--help" to access help text with Click?


Help messages in Click are accessed via the long option --help by default. How can I also make the short option -h available?


Solution

  • Specify context_settings for your command:

    CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
    
    @click.command(context_settings=CONTEXT_SETTINGS)
    def cli():
        pass
    

    Note, however, the relative difficulty of implementing this behavior, which may suggest that doing so is discouraged. That is, unless you subclass the click.command decorator, you'll have to specify this for every command you create.

    Click was built with composability in mind. You might balance the value derived from allowing an additional way of seeing help text against the cost of potentially breaking composability with other CLI tools.

    Plus, you might want to use -h for something else. E.g. ssh-add uses it for a destination_constraint option (think destination "host").