Skip to main content

Create CLIs with classes and type hints.

Project description

image image image

Cliar

Cliar is yet another Python package to create commandline interfaces. It focuses on simplicity, extensibility, and testability:

  • Creating a CLI is as simple as creating subclassing cliar.Cliar.
  • Extending a CLI is as simple as subclassing from the base CLI class.
  • Testing a CLI is as simple as unittesting a class.

Cliar's mission is to let the programmer focus on the business logic instead of building an interface for it. At the same time, Cliar doesn't want to stand in the programmer's way, so it provides means to customize the generated CLI.

Installation

pip install cliar

Cliar runs with Python 3.6+ on Windows, Linux, and Mac. There are no dependencies outside Python's standard library.

Basic Usage

Let's create a calculator app. Version 1.0.0 will implement only addition of two real numbers.

Here's how you do it with Cliar:

from cliar import Cliar


class Calculator(Cliar):
    '''Calculator app.'''

    def add(self, x: float, y: float):
        '''Add two real numbers.'''

        print(f'The sum of {x} and {y} is {x+y}.')


if __name__ == '__main__':
    Calculator().parse()

Save this code as calc.py and run it with different inputs:

$ python run calc.py add 12 34
The sum of 12.0 and 34.0 is 46.0.

$ python run calc.py foo bar
usage: calc.py add [-h] x y
calc.py add: error: argument x: invalid float value: 'foo'

$ python run 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 real numbers.

$ python calc.py add -h
usage: calc.py add [-h] x y

Add two real numbers.

positional arguments:
  x
  y

optional arguments:
  -h, --help  show this help message and exit

There are a few things to note in the code above:

  • It's a regular Python class with a regular Python method. You don't need to learn any new syntax to use Cliar.
  • The add method was converted into add command, and its positional params were converted into positional commandline args.
  • We don't convert x or y to float or handle any potential conversion errors in the add body. Instead, we treat x and y as if they were already guaranteed to be floats. That's because Cliar does the validation and conversion for us, using the information from add's type hints. Note how invalid input doesn't even reach your code.
  • The --help and -h flags are added automatically and the help messages were generated from the docstrings.

Project details


Release history Release notifications

This version
History Node

1.2.2

History Node

1.2.1

History Node

1.2.0

History Node

1.1.9

History Node

1.1.8

History Node

1.1.7

History Node

1.1.6

History Node

1.1.5

History Node

1.1.4

History Node

1.1.3

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.9

History Node

1.0.8

History Node

1.0.7

History Node

1.0.6

History Node

1.0.5

History Node

1.0.4

History Node

1.0.3

History Node

1.0.2

History Node

1.0.1

History Node

1.0.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
cliar-1.2.2-py3-none-any.whl (13.5 kB) Copy SHA256 hash SHA256 Wheel py3 Jun 3, 2018
cliar-1.2.2.tar.gz (5.2 kB) Copy SHA256 hash SHA256 Source None Jun 3, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page