Search code examples
pythonpython-3.xparameter-passingcommand-line-interfacepython-click

Python Click: How to change the value of `info_name` for the global context


In a Python script using Click for the command line handling, I would like to change the info_name in the global context (in order to modify the help output).

Here is a contrived minimal example mycmd.py where I try using the context_settings:

import click

CONTEXT_SETTINGS = dict(info_name="hallo")

@click.command('mycmd', context_settings=CONTEXT_SETTINGS)
@click.pass_context
def click_cli(ctx):
   pass

click_cli()

However when I run this script with python mycmd.py --help I get the traceback:

...
  File ".../click/core.py", line 639, in make_context
    ctx = Context(self, info_name=info_name, parent=parent, **extra)
TypeError: type object got multiple values for keyword argument 'info_name'

So the problem is that the arguments in **extra (that's where the context_settings end up) cannot overwrite the hard-coded info_name=info_name parameter. How can I do this?


Solution

  • Use the prog_name parameter for the cli function, to change the help name (info_name) like:

    cli(prog_name='hallo')
    

    Test Code:

    import click
    
    @click.command('mycmd')
    @click.pass_context
    def cli(ctx):
        pass
    
    cli(['--help'], prog_name='hallo')
    

    Results:

    Usage: hallo [OPTIONS]
    
    Options:
      --help  Show this message and exit.