Suppose we have the following function, which
has an optional parameter exclude
, which is expected
to be a list of strings:
@group.command()
@click.option(???)
def load(exclude = None):
# ...
I want the user to be able to execute the script like so:
python script.py load --exclude one two three
or
python script.py load --exclude something
In which case, I would expect exclude
to be
['one', 'two', 'three']
and ['something']
, respectively.
But I want the number of values in --exclude
to be variable, including 0. I.e. The user should be able to execute
python script.py load
In which case exclude
should be None
(or []
, does not matter).
How can I do that using click?
My only viable option so far is forcing the user to use
python script.py load --exclude "one two three"
and then calling exclude.split()
, but something cleaner would be better.
It doesn't appear to be supported, in the way that you want, by Click:
According to the docs Multi Value Options must have the number of options provided as specified in nargs
, so nargs=2
requires 2 values.
additionally, multiple options will not negate the need to pass the option multiple times.
Your most likely solution is to pass a string and split, although I haven't used Click too deeply, so someone may have a better solution.