Konubinix' opinionated web of thoughts

Clk Python Customcommand

Fleeting

clk python customcommand

To do so, simply run the command:

clk customcommand create python mycommand

Your editor will open with the new command to be written, pre-filled with a default template.

At the end of the command, see the python code of the function of the commands? It should look like this.

@command()
def mycommand():
    "Description"

The only limitation is that this command should have the name of the module that contains it. In that case, it is called mycommand.

This is a standard click command, so please take a look at click for more detail.

Say we want to make a command that makes an animal say hello.

First install the cowsay package.

python3 -m pip install cowsay

Then use it to create your command.

import cowsay

@command()
@argument("content", help="The content to display")
@option("--who", type=click.Choice(cowsay.char_names), help="Who says")
def mycommand(content, who):
    "Praise someone for good services"
    who = (
        getattr(cowsay, who) if who
        else print
    )
    who(content)

Then save the file and see the result

clk mycommand test --who cow
  ____
| test |
  ====
    \
     \
       ^__^
       (oo)\_______
       (__)\       )\/\
           ||----w |
           ||     ||

Notes pointant ici