I am using the Click
library but I can't seem to find a behavior similar to dest
from argparse
.
For example, I have
@click.option('--format', type=click.Choice(['t', 'j']))
def plug(format):
pass
Notice that I am using a flag with --format
that gets translated into a built-in Python construct format
which is not ideal.
Is there a way to change the argument passed into the click function for options?
While
Click
doesn't havedest
-equivalent ofargparse
, it has certain argument-naming behavior which can be exploited. Specifically, for parameters with multiple possible names, it will prefer non-dashed to dashed names, and as secondary preference will prioritize longer names over shorter names.URL: http://click.pocoo.org/dev/parameters/#parameter-names
So if you declare your option as...
...then Click will prioritize non-dashed variant (
'not-format'
) and call your function withnot_format=...
argument.Of course it also means that this alternative spelling can also be used in command line. If that is not desired, then I guess you could add a decorator to rename keyword arguments:
Testing code:
Test output:
In your case, it would look like:
If you decorate your method with
then it will map the option to the
format_arg_name
parameter.format_arg_name
will not be available on the command line.