Build a simple command-line interface from your functions.
@cli- that is it - it can now be called as CLI but also remains usable by other functions (unlike the click library)
cliche install <mycli>
You want to make a calculator. You not only want its functions to be reusable, you also want it to be callable from command line.
# calculator.py from cliche import cli def add(a: int, b: int): print(a + b)
Now let's see how to use it from the command-line:
pascalint| b |int| optional arguments: -h, --help show this help message and exit:~/calc$ cliche install calc pascal :~/calc$ calc add --help usage: calc add [-h] a b positional arguments: a |
pascal@archbook:~/calc$ calc add 1 10 11
You noticed we ran
cliche install calc
We can undo this with
cliche uninstall calc
Note that installing means that all
@cli functions will be detected
in the folder, not just of a single file, even after installation. You
only have to install once, and on Linux it also adds autocompletion to
your CLI if
argcomplete has been installed.
from cliche import cli def add_or_mul(a_number: int, b_number=10, sums=False): """ Adds or multiplies a and b :param a_number: the first one :param b_number: second one :param sums: Sums when true, otherwise multiply """ if sums: print(a_number + b_number) else: print(a_number * b_number)
pascal@archbook:~/calc$ calc add_or_mul 1 10 pascal@archbook:~/calc$ calc add_or_mul --sum 1 11 pascal@archbook:~/calc$ calc add_or_mul 2 -b 3 6
Check the example files here