Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Click command names

I'm using the click package for creating a command line tool. However, I would like to have a 'list' command. For example:

@click.command
@click.option(help='list...')
def list():
    # do stuff here

Is there another way in click to pass in a command name other than having it as the function name? I don't want this function to shadow python's built in list. I've looked through the documentation and can't really find anything about command names -- I've read up on command aliases but that doesn't seem to help this problem. Or do I not need to worry about list being shadowed since it's being wrapped by the click decorator? Thanks in advance.

like image 696
user5004049 Avatar asked Oct 12 '15 18:10

user5004049


People also ask

What is click command Python?

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It's the “Command Line Interface Creation Kit”. It's highly configurable but comes with sensible defaults out of the box.

What is click Pass_context?

When a Click command callback is executed, it's passed all the non-hidden parameters as keyword arguments. Notably absent is the context. However, a callback can opt into being passed to the context object by marking itself with pass_context() .


Video Answer


1 Answers

You can provide the name argument when you use the command decorator. Once you've done that, you can name your function whatever you want:

@click.command(name='list')
def list_command():
    pass

See the Click documentation for details.

like image 144
Rob Kennedy Avatar answered Oct 19 '22 20:10

Rob Kennedy