click icon indicating copy to clipboard operation
click copied to clipboard

dynamic choice options

Open chaoflow opened this issue 7 years ago • 2 comments

We have dynamic default values via lambda, I'd see myself often hacking around what could be achieved with dynamic types, ideally with the context available to react upon already parsed options/arguments:

@click.group()
@click.option('--foo')
def cmd(foo):
    pass

@cmd.command()
@click.option('--bar')
@click.option('--tag', type=lambda ctx: click.Choice(dynamic_list_depending_on_foo_and_bar))

With the above I see the issue often knowing how many arguments tag is going to eat from the command line. Maybe better:

@click.group()
@click.option('--foo')
def cmd(foo):
    pass

@cmd.command()
@click.option('--bar')
@click.option('--tag', type=click.Choice(lambda ctx: dynamic_list_depending_on_foo_and_bar))

chaoflow avatar Feb 10 '18 16:02 chaoflow

You may be interested in the autocompletion argument that can supply a callback for this usecase. See https://github.com/pallets/click/blob/master/docs/bashcomplete.rst

stopthatcow avatar Feb 12 '18 20:02 stopthatcow

Is there still any interest in this feature? I could have a go at a PR if so.

For my use case, I'd only be interested in dynamic choices, not dynamic types. I pretty much want exactly what ls does if there are a lot of directories to show as possible completions: get confirmation from the user before doing some slow operation to load all the possible choices.

❯ ls /dev/
zsh: do you wish to see all 344 possibilities (86 lines)?

zakini avatar Nov 21 '22 23:11 zakini