Clios#
Clios, create chainable command line operators
Installation#
Example: numbers.py
#
Let’s create a dumb CLI app numbers.py
using Clios to do some basic arithematic on integer data.
First, import the Clios class and create an instance, we call this an app:
from clios import Clios
app = Clios()
Next, define a function that takes a single string as input and returns an integer. Use the app.reader decorator to register this function as a Reader for the int type:
@app.reader()
def reader_int(input: str) -> int:
return int(input)
In a Clios CLI application, a Reader is responsible for converting a string argument to the required data type, such as converting a string to an integer or processing a file path and returning its content.
Next, define a function which takes a single argument of type ‘int’ and prints the output. The app.writer decorator registers this function as Writer for type ‘int’:
@app.writer()
def writer_int(input: int):
print(input)
A Writer is responsible for handling the output of a command, displaying it or writing it to a file (which will be covered later).
Now, lets define some operators:
Define a function that adds two integers and register it as an Operator:
@app.operator()
def add(input: tuple[int, int]) -> int:
return input[0] + input[1]
lets, define another function that subtracts one integer from another and register it as an Operator:
@app.operator()
def sub(input: tuple[int, int]) -> int:
return input[0] - input[1]
Define a function that sums a list of integers and register it as an Operator, with a custom name sum:
@app.operator(name="sum")
def sum_int(input: tuple[int, ...]) -> int:
return sum(input)
Finally, set up the application to parse command-line arguments and execute the corresponding commands:
if __name__ == "__main__":
import sys
app.parse_args(sys.argv[1:]).execute()
Now lets try the numbers.py: