I am trying to build a command line interface using click-shell in python but I am unable to figure out how to provide inputs together with the commands. That is, say I have a function/command named sum
, that requires two inputs a
and b
, on cmd how do I invoke myapp > sum 2 3
? Currently all I have to do is invoke the inputs in the functions themselves.
this works:
from click_shell import shell
@shell(prompt='myapp> ', intro='Command me!')
def myapp():
os.system("cls")
@myapp.command()
def sum():
a = input('enter num1 > ')
b = input('enter num2 > ')
r = float(a) + float(b)
print(f"{a} + {b} = {r}")
but what I want is something like this:
@myapp.command()
def sum(a, b):
print(f"{a} + {b} = {float(a) + float(b)}")
such that on CMD all I have to do is:
myapp> sum 2 3
:> 2 + 3 = 5
myapp>
Anyone who knows how to achieve this please help. Suggestion on better package to use is accepted too.
I was attempting various approaches and one of the attempt was to add @click.argument
decorator and it worked as I wanted. Here
from click_shell import shell
import os, click
@shell(prompt='myapp> ', intro='Hello There! Enter your commands')
def myapp():
os.system("cls")
@myapp.command()
@click.argument('nums', type=float, nargs=-1)
def add(nums: list[float]):
print(f"sum = {sum(nums)}")
if __name__ == '__main__':
myapp()
any better solution will be accepted