Create CLIs with classes and type hints.
Project description
Cliar
Cliar is a Python package to help you create commandline interfaces. It focuses on simplicity and extensibility:
- Creating a CLI is as simple as subclassing from
cliar.Cliar. - Extending a CLI is as simple as subclassing from a
cliar.Cliarsubclass.
Cliar's mission is to let you focus on the business logic instead of building an interface for it. At the same time, Cliar doesn't want to stand in your way, so it provides the means to customize the generated CLI.
Installation
$ pip install cliar
Cliar requires Python 3.6+ and is tested on Windows, Linux, and macOS. There are no dependencies outside Python's standard library.
Basic Usage
Let's create a commandline calculator that adds two floats:
from cliar import Cliar
class Calculator(Cliar):
'''Calculator app.'''
def add(self, x: float, y: float):
'''Add two numbers.'''
print(f'The sum of {x} and {y} is {x+y}.')
if __name__ == '__main__':
Calculator().parse()
Save this code to calc.py and run it. Try different inputs:
-
Valid input:
$ python calc.py add 12 34 The sum of 12.0 and 34.0 is 46.0. -
Invalid input:
$ python calc.py add foo bar usage: calc.py add [-h] x y calc.py add: error: argument x: invalid float value: 'foo' -
Help:
$ python calc.py -h usage: calc.py [-h] {add} ... Calculator app. optional arguments: -h, --help show this help message and exit commands: {add} Available commands: add Add two numbers. -
Help for
addcommand:$ python calc.py add -h usage: calc.py add [-h] x y Add two numbers. positional arguments: x y optional arguments: -h, --help show this help message and exit
A few things to note:
-
It's a regular Python class with a regular Python method. You don't need to learn any new syntax to use Cliar.
-
addmethod is converted toaddcommand, its positional params are converted to positional commandline args. -
There is no explicit conversion to float for
xoryor error handling in theaddmethod body. Instead,xandyare just treated as floats. Cliar converts the types usingadd's type hints. Invalid input doesn't even reach your code. -
--helpand-hflags are added automatically and the help messages are generated from the docstrings.
Setuptools and Poetry
To invoke your CLI via an entrypoint, wrap parse call in a function and point to it in your setup.py or pyproject.toml.
calc.py:
...
def entry_point():
Calculator().parse()
setup.py:
setup(
...
entry_points = {
'console_scripts': ['calc=calc:entry_point'],
}
...
)
pyproject.toml:
...
[tool.poetry.scripts]
calc = 'calc:entry_point'
Read Next
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cliar-1.3.5.tar.gz.
File metadata
- Download URL: cliar-1.3.5.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0a1 CPython/3.10.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ff471cb78f1a191d62589f9722c2dd93fa50b803063a411e4fa28741e591633
|
|
| MD5 |
5e0a1c31394b023b04c9a60518dbda0f
|
|
| BLAKE2b-256 |
bcc0e449bd83c9128c22cdd50e0dbc2b49b0c7fd980af765b35f492e37d0d95b
|
File details
Details for the file cliar-1.3.5-py3-none-any.whl.
File metadata
- Download URL: cliar-1.3.5-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0a1 CPython/3.10.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6983c9a34c7be69f9679e7dce5aa05bd77ef61ec9dc85733d9d59a3c0b832be
|
|
| MD5 |
38df8503af609f1ef926e070b50831fd
|
|
| BLAKE2b-256 |
fe0f03d0134bdb9e9c2ff4c43a25ff810ef61c16dba11f9c70110591f34a8340
|