A couple of weeks ago, I started to wonder if it is possible to write a library in Python to create a CLI application that uses only standard packages as dependencies.
The idea seems fun, so I've started to build it.
The main features in my mind were:
- command register via decorator,
- autocomplete,
- command history,
- help menu via docstring,
- Use of typing hints and annotations to retrieve:
- mandatory/optional parameter
- parameter type (and check types).
Long road to accomplish something more complete, like sub-commands.
The meaning of the name
The 'mustiolo' is the smallest mammal in the world, weighing about 1.2-2.5 grams as an adult.
It is present in Sardinia, in the Italian, Balkan, Iberian peninsulas, and in North Africa.
This library aims to be the smallest library for building CLI applications in Python just like a mustiolo is the smallest mammal.
Example
from mustiolo.cli import CLI
cli = CLI()
@cli.command()
def greet(name: str):
"""Greet a user by name."""
print(f"Hello {name}!")
@cli.command()
def add(a: int, b: int):
"""Add two numbers and print the result."""
print(f"The result is: {a + b}")
if __name__ == "__main__":
cli.run()
This example allows to have
> ?
greet Greet a user by name.
add Add two numbers and print the result.
> ? add
Usage add Add two numbers and print the result.
add A B
Parameters:
A Type INTEGER [required]
B Type INTEGER [required]
> exit
If you're interested please visit Mustilo repository on GitHub.