If you don't insist on passing something that looks like a list, but simply want to pass multiple variadic arguments, you can use the multiple option.

From the click documentation

@click.command()
@click.option('--message', '-m', multiple=True)
def commit(message):
    click.echo('\n'.join(message))
$ commit -m foo -m bar
foo
bar
Answer from Jarno on Stack Overflow
🌐
Click Documentation
click.palletsprojects.com › en › stable › options
Options — Click Documentation (8.5.x)
Adding options to commands can be accomplished with the option() decorator. At runtime the decorator invokes the Option class. Options in Click are distinct from positional arguments.
🌐
Readthedocs
pocoo-click.readthedocs.io › en › latest › options
Options — click
If no type is provided, the type of the default value is used. If no default value is provided, the type is assumed to be STRING. By default, the name of the parameter is the first long option defined; otherwise the first short one is used. @click.command() @click.option('--n', default=1) def dots(n): click.echo('.' * n)
Discussions

Documentation: mention required=True for options
This is never mentioned as a possibility for click.option anywhere in the docs as far as I can tell (or at least it should be more prominent, maybe a section). (it talks about it in click.argument, but its unclear that it exists for both... More on github.com
🌐 github.com
12
February 4, 2016
How to give click option I python?
You would need to use a GUI library (such as tkinter), or a TUI library with mouse support (PyTermGUI seems like a viable option). Without those it's not really feasible. More on reddit.com
🌐 r/learnpython
10
3
September 22, 2024
Support click.option with default values / optional arguments
Since the default value always applies, there should be a way to differentiate between the following options usage: file.py file.py --foo file.py --foo 2 where '2' is an argument to --foo. ... More on github.com
🌐 github.com
24
April 1, 2016
Top answer
1 of 6
93

If you don't insist on passing something that looks like a list, but simply want to pass multiple variadic arguments, you can use the multiple option.

From the click documentation

@click.command()
@click.option('--message', '-m', multiple=True)
def commit(message):
    click.echo('\n'.join(message))
$ commit -m foo -m bar
foo
bar
2 of 6
41

You can coerce click into taking multiple list arguments, if the lists are formatted as a string literals of python lists by using a custom option class like:

Custom Class:

import click
import ast

class PythonLiteralOption(click.Option):

    def type_cast_value(self, ctx, value):
        try:
            return ast.literal_eval(value)
        except:
            raise click.BadParameter(value)

This class will use Python's Abstract Syntax Tree module to parse the parameter as a python literal.

Custom Class Usage:

To use the custom class, pass the cls parameter to @click.option() decorator like:

@click.option('--option1', cls=PythonLiteralOption, default=[])

How does this work?

This works because click is a well designed OO framework. The @click.option() decorator usually instantiates a click.Option object but allows this behavior to be over ridden with the cls parameter. So it is a relatively easy matter to inherit from click.Option in our own class and over ride the desired methods.

In this case we over ride click.Option.type_cast_value() and then call ast.literal_eval() to parse the list.

Test Code:

@click.command(context_settings=dict(help_option_names=['-h', '--help']))
@click.option('--option1', cls=PythonLiteralOption, default=[])
@click.option('--option2', cls=PythonLiteralOption, default=[])
def cli(option1, option2):
    click.echo("Option 1, type: {}  value: {}".format(
        type(option1), option1))
    click.echo("Option 2, type: {}  value: {}".format(
        type(option2), option2))

# do stuff
if __name__ == '__main__':
    import shlex
    cli(shlex.split(
        '''--option1 '["o11", "o12", "o13"]' 
        --option2 '["o21", "o22", "o23"]' '''))

Test Results:

Option 1, type: <type 'list'>  value: ['o11', 'o12', 'o13']
Option 2, type: <type 'list'>  value: ['o21', 'o22', 'o23']
🌐
Click Documentation
click.palletsprojects.com › en › stable › parameter-types
Parameter Types — Click Documentation (8.5.x)
$ repeat --count=100 --digit=5 55555555555555555555 $ repeat --count=6 --digit=12 Usage: repeat [OPTIONS] Try 'repeat --help' for help. Error: Invalid value for '--digit': 12 is not in the range 0<=x<=9. ... bool / click.BOOL: A parameter that accepts boolean values.
🌐
Devnet-academy
docs.devnet-academy.com › docs › click_v7.1.2 › options.html
Options — Click Documentation (7.x)
# How to make an option required @click.command() @click.option('--n', required=True, type=int) def dots(n): click.echo('.' * n) # How to use a Python reserved word such as `from` as a parameter @click.command() @click.option('--from', '-f', 'from_') @click.option('--to', '-t') def reserved_param_name(from_, to): click.echo('from %s to %s' % (from_, to))
🌐
Medium
medium.com › @afimaamedufie › getting-acquainted-with-python-click-fbfccf2bd990
Getting Acquainted with Python Click | by Afi Maame Dufie | Medium
November 25, 2022 - Values in options follow the name of the option while arguments are taken positionally. You can install Click with pip.
Find elsewhere
🌐
ZetCode
zetcode.com › python › click
Python click - creating command line interfaces
January 29, 2024 - Options are added to commands with the click.option decorator. Option names are prefixed with one or two dashes. ... #!/usr/bin/python import click @click.command() @click.option('--n', type=int, default=1) def dots(n): click.echo('.' * n) if __name__ == '__main__': dots()
🌐
Zonca
zonca.dev › posts › 2022-10-26-click-commandline-class-arguments
Generate Click command line options dynamically from class arguments – Andrea Zonca
October 26, 2022 - def options_from_class(cls): def decorator(f): for par in inspect.signature(cls.__init__).parameters.values(): if par.name not in ["self"]: click.option("--" + par.name, required=True, type=par.annotation)(f) return f return decorator
🌐
PyPI
pypi.org › project › click
click · PyPI
2 weeks ago - import click @click.command() @click.option("--count", default=1, help="Number of greetings.") @click.option("--name", prompt="Your name", help="The person to greet.") def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for _ in range(count): click.echo(f"Hello, {name}!") if __name__ == '__main__': hello() $ python hello.py --count=3 Your name: Click Hello, Click!
🌐
Real Python
realpython.com › python-click
Click and Python: Build Extensible and Composable CLI Apps – Real Python
March 18, 2026 - Note: Your use of deque for this example comes from the Python documentation on deque. Check out the section on deque recipes for further examples. In the highlighted line, you call the @click.option decorator to add a new option to your Click command.
🌐
GitHub
github.com › pallets › click › issues › 514
Documentation: mention required=True for options · Issue #514 · pallets/click
February 4, 2016 - This is never mentioned as a possibility for click.option anywhere in the docs as far as I can tell (or at least it should be more prominent, maybe a section). (it talks about it in click.argument, but its unclear that it exists for both...
Author: pallets
🌐
Click Documentation
click.palletsprojects.com › en › stable › prompts
User Input Prompts — Click Documentation (8.5.x)
value = click.prompt('Please enter a number', default=42.0) If the option has prompt enabled, then setting prompt_required=False tells Click to only show the prompt if the option’s flag is given, instead of if the option is not provided at all.
🌐
Slidedeck
slidedeck.io › telemaco › python-click
python-click
#!/usr/bin/env python import click @click.command() @click.option('--count-down', default=10, help='suprise countdown') @click.argument('name') def surprise(count_down, name): while count_down != 0: click.echo("%s..." % count_down) count_down = count_down - 1 click.echo("Supriseeeeee!!!!
🌐
Simon Willison
til.simonwillison.net › python › click-option-names
Understanding option names in Click | Simon Willison’s TILs
@click.command() # ... @click.option("-o", "--open", "open_browser", is_flag=True, help="Open Datasette in your web browser") def my_command(open_browser): # Now open() can be used safely · Click will use the positional argument without any hyphen prefixes as the name of the argument. If none is provided it will use the first -- one. If that's not defined it will use the -o one - all with the hypens stripped. gpt3 Using ChatGPT Browse to name a Python package - 2023-06-18
🌐
Click Documentation
click.palletsprojects.com › en › stable › documentation
Help Pages — Click Documentation (8.5.x)
Options: --count INTEGER number of greetings --help Show this message and exit. For subcommands, a short help snippet is generated. By default, it’s the first sentence of the docstring. If too long, then it will ellipsize what cannot be fit on a single line with .... The short help snippet can also be overridden with short_help: import click @click.group() def cli(): """A simple command line tool.""" @cli.command('init', short_help='init the repo') def init(): """Initializes the repository."""
🌐
dbader.org
dbader.org › blog › python-commandline-tools-with-click
Writing Python Command-Line Tools With Click – dbader.org
December 6, 2017 - I mentioned before that click creates the argument passed to the main function from the long version of the name. In case of an option, it strips the leading dashes and turns them into snake case. --api-key becomes api_key. Last thing we have to do to make this work is passing the API key through to our current_weather function. Boom 👊🏼 · We’ve made it possible for our CLI user to use their own key and check out any location: $ python cli.py --api-key <your-api-key> Canmore The weather in Canmore right now: broken clouds.
🌐
Click Documentation
click.palletsprojects.com › en › stable › parameters
Parameters — Click Documentation (8.5.x)
The name must match the python arg name. To provide a different name for use in help text, see Placeholder / Meta Variable. The option’s names are -t and --times. More names are available for options and are covered in Options. @click.command() @click.argument('filename') @click.option('-t', '--times', type=int) def multi_echo(filename, times): """Print value filename multiple times.""" for x in range(times): click.echo(filename)
🌐
Click Documentation
click.palletsprojects.com › en › stable › quickstart
Quickstart — Click Documentation (8.5.x)
@click.command() @click.option('--count', default=1, help='number of greetings') @click.argument('name') def hello(count, name): for x in range(count): click.echo(f"Hello {name}!") ... $ python hello.py --help Usage: hello.py [OPTIONS] NAME Options: --count INTEGER number of greetings --help ...
🌐
GitHub
github.com › pallets › click › issues › 549
Support click.option with default values / optional arguments · Issue #549 · pallets/click
April 1, 2016 - Since the default value always applies, there should be a way to differentiate between the following options usage: file.py file.py --foo file.py --foo 2 where '2' is an argument to --foo. ...
Author: pallets