Here's what I have that does not behave as I'd like it to:
import click
xmin_chlist = ['clauset', 'manual', 'percentile']
@click.command()
@click.option('-x', '--xmin', default=('clauset', None),
type=(click.Choice(xmin_chlist), float),
help=f'CHOICE one of {xmin_chlist}')
def get_uis(xmin):
print(xmin)
if __name__ == '__main__':
get_uis()
The above code is written toui.py
, and invoking $ python ui.py
on the command line without specifying the --xmin
option prints the default ('clauset', None)
as expected; so does providing 2 values to --xmin
, such as $ python ui.py --xmin percentile 99
.
But I'd also like to only be able to provide a single value to --xmin
, such as $ python ui.py --xmin clauset
and still get None
as the default to the second value in the tuple, but instead I get the error Error: --xmin option requires 2 arguments
. Furthermore, if possible I'd also like to be able to provide different second element defaults to 'manual'
and 'percentile'
, when they are passed alone to --xmin
instead of just the None
for 'clauset'
.
I tried using a callback to provide defaults based on the number of values passed to --xmin
, but that does not appear to work either as the error prevents the callback from firing.
I understand that there is no built-in option type in Click which allows passing a variable number of values. So should I be creating my custom variable type? Or something else? What would be the best approach here?