Skip to content

Passing argument as differently named parameter #949

@tuukkamustonen

Description

@tuukkamustonen

Trying to get click.argument passed with different name to function because variable name is shadowed from higher scope (e.g. input, print, or whatever imports you might have).

http://click.pocoo.org/6/parameters/#parameter-names states:

Parameters (both options and arguments) accept a number of positional arguments which are the parameter declarations. Each string with a single dash is added as short argument; each string starting with a double dash as long one. If a string is added without any dashes, it becomes the internal parameter name which is also used as variable name.

I think it indirectly tells that:

  • It is possible to pass option as different variable name (e.g. click.option('--input', 'input_'))
  • It is not not possible to pass argument as different name? Because docs just state that string without dashes becomes parameter name, and with click.argument we are indeed passing string without dashes, so it's not possible to do something like click.argument('input', 'input_')?

Tried it on test.py:

import click

@click.command()
@click.argument('input', 'input_')
def hello(input_):
    print('hello')

if __name__ == '__main__':
    hello()

Run:

$ python test.py hello
Traceback (most recent call last):
    ...
TypeError: hello() got an unexpected keyword argument 'foo'

How can I pass argument as different variable name to the function?

What does click.argument('first', 'second') really do, as there's no direct exception thrown from it (but click.argument('first', 'second', 'third') throws error instead)?

Running click 6.7.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions