I have this chunk of code:
import click
@click.option('--delete_thing', help="Delete some things columns.", default=False)
def cmd_do_this(delete_thing=False):
print "I deleted the thing."
I would like to rename the option variable in --delete-thing. But python does not allow dashes in variable names. Is there a way to write this kind of code?
import click
@click.option('--delete-thing', help="Delete some things columns.", default=False, store_variable=delete_thing)
def cmd_do_this(delete_thing=False):
print "I deleted the thing."
So delete_thing will be set to the value of delete-thing
As gbe's answer says, click will automatically convert - in the cli parameters to _ for the python function parameters.
But you can also explicitly name the python variable to whatever you want. In this example, it converts --delete-thing to new_var_name:
import click
@click.command()
@click.option('--delete-thing', 'new_var_name')
def cmd_do_this(new_var_name):
print(f"I deleted the thing: {new_var_name}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With