Python Click: make option depend on previous option
<p>Is there an idiomatic way, using the <a href="http://click.pocoo.org/5/" rel="nofollow noreferrer" title="Python Click">Python Click library</a>, to create a command where one option depends on a value set by a previous option?</p>

<p>A concrete example (my use case) would be that a command takes an option of type <code>click.File</code> as input, but also an encoding option which specifies the encoding of the input stream:</p>

<pre><code>import click

@click.command()
@click.option("--encoding", type=str, default="utf-8")
@click.option("--input",
              type=click.File("r", encoding="CAN I SET THIS DYNAMICALLY BASED ON --encoding?"))
def cli(encoding, input):
    pass
</code></pre>

<p>I guess it would have to involve some kind of deferred evaluation using a callable, but I'm not sure if it's even possible given the current Click API.</p>

<p>I've figured out I can do something along the following lines:</p>

<pre><code>import click

@click.command()
@click.pass_context
@click.option("--encoding", type=str, default="utf-8")
@click.option("--input", type=str, default="-")
def cli(ctx, encoding, input):
    input = click.File("r", encoding=encoding)(input, ctx=ctx)
</code></pre>

<p>But it somehow feels less readable / maintainable to decouple the option decorator from the semantically correct type constraint that applies to it, and put <code>str</code> in there instead as a dummy. So if there's a way to keep these two together, please enlighten me.</p>

<p><strong>A proposed workaround:</strong></p>

<p>I guess I could use the <code>click.File</code> type twice, making it lazy in the decorator so that the file isn't actually left opened, the first time around:</p>

<pre><code>@click.option("--input", type=click.File("r", lazy=True), default="-")
</code></pre>

<p>This feels semantically more satisfying, but also redundant.</p>