Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename python click argument

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

like image 963
kaligne Avatar asked Jul 24 '26 16:07

kaligne


1 Answers

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}")
like image 137
wisbucky Avatar answered Jul 27 '26 06:07

wisbucky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!