All Projects → moigagoo → cliar

moigagoo / cliar

Licence: other
Create modular Python CLIs with type annotations and inheritance

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to cliar

Manage
Command Line Manager + Interactive Shell for Python Projects
Stars: ✭ 111 (+136.17%)
Mutual labels:  commandline, click, commandline-interface
Imag
imag - Text based personal information management suite
Stars: ✭ 318 (+576.6%)
Mutual labels:  commandline, commandline-interface
Pxltrm
🖌️ pxltrm - [WIP] A pixel art editor inside the terminal
Stars: ✭ 459 (+876.6%)
Mutual labels:  commandline, commandline-interface
exch
a command-line tool to see currency exchange rates
Stars: ✭ 20 (-57.45%)
Mutual labels:  commandline, click
cmdr
POSIX-compliant command-line UI (CLI) parser and Hierarchical-configuration operations
Stars: ✭ 94 (+100%)
Mutual labels:  commandline, commandline-interface
Gitviper
Enhanced git experience using the command line
Stars: ✭ 35 (-25.53%)
Mutual labels:  commandline, commandline-interface
Socli
Stack overflow command line client. Search and browse stack overflow without leaving the terminal 💻
Stars: ✭ 911 (+1838.3%)
Mutual labels:  commandline, commandline-interface
docopt-ng
Humane command line arguments parser. Now with maintenance, typehints, and complete test coverage.
Stars: ✭ 94 (+100%)
Mutual labels:  docopt, argparse
Comfy Table
🔶 Build beautiful terminal tables with automatic content wrapping
Stars: ✭ 156 (+231.91%)
Mutual labels:  commandline, commandline-interface
Reactopt
A CLI React performance optimization tool that identifies potential unnecessary re-rendering
Stars: ✭ 1,975 (+4102.13%)
Mutual labels:  commandline, commandline-interface
pendfetch
Double Pendulum visualised with fetching system information in Python.
Stars: ✭ 62 (+31.91%)
Mutual labels:  commandline, commandline-interface
declarative-parser
Modern, declarative argument parser for Python 3.6+
Stars: ✭ 31 (-34.04%)
Mutual labels:  argparse, commandline-interface
clickos
The Click modular router: fast modular packet processing and analysis
Stars: ✭ 127 (+170.21%)
Mutual labels:  click
CDT-plusplus
Causal Dynamical Triangulations in C++ using CGAL
Stars: ✭ 49 (+4.26%)
Mutual labels:  docopt
configmanager
Forget about configparser, YAML, or JSON parsers. Focus on configuration. NOT RECOMMENDED FOR USE (2019-01-26)
Stars: ✭ 15 (-68.09%)
Mutual labels:  click
debayer
Debayer raw images automatically, with the option of outputting scene-linear aces exrs.
Stars: ✭ 26 (-44.68%)
Mutual labels:  commandline
xml-lint
A php tool to lint and validate xml files from the commandline.
Stars: ✭ 14 (-70.21%)
Mutual labels:  commandline
YouTube-Tutorials--Italian
📂 Source Code for (some of) the Programming Tutorials from my Italian YouTube Channel and website ProgrammareInPython.it. This is just a small portion of the content: please visit the website for more.
Stars: ✭ 28 (-40.43%)
Mutual labels:  argparse
Data-Scientist-In-Python
This repository contains notes and projects of Data scientist track from dataquest course work.
Stars: ✭ 23 (-51.06%)
Mutual labels:  commandline
tiles
Commandline tool that makes building tilesets and rendering static tilemaps super easy!
Stars: ✭ 51 (+8.51%)
Mutual labels:  commandline

image Build Status image

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.Cliar subclass.

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 add command:

    $ 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.

  • add method is converted to add command, its positional params are converted to positional commandline args.

  • There is no explicit conversion to float for x or y or error handling in the add method body. Instead, x and y are just treated as floats. Cliar converts the types using add's type hints. Invalid input doesn't even reach your code.

  • --help and -h flags 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].