Giter Site home page Giter Site logo

cliche's Introduction

Cliche

Build a simple command-line interface from your functions.

Features:

  • Least syntax required: you do not need to learn a "library" to use this
  • keeps it DRY (Don't Repeat yourself):
    • it uses all information available like annotations, default values and docstrings... yet does not require them.
  • Just decorate a function with @cli - that is it - it can now be called as CLI but also remains usable by other functions
  • Standing on the shoulders of giants (i.e. it uses argparse and learnings from others) -> lightweight

Examples

Simplest Example

You want to make a calculator. You not only want its functions to be reusable, you also want it to be callable from command line.

# calculator.py
from cliche import cli

@cli
def add(a: int, b: int):
    print(a + b)

Now let's see how to use it from the command-line:

pascal@archbook:~/$ cliche calculator.py add --help

usage: cliche add [-h] a b

positional arguments:
  a           |int|
  b           |int|

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

thus:

pascal@archbook:~/$ cliche calculator.py add 1 10
11

Advanced Example

from cliche import cli


@cli
def sum_or_multiply(a_number: int, b_number: int = 10, sums: bool = False):
    """ Sums or multiplies a and b

    :param a_number: the first one
    :param b_number: This parameter seems to be
    :param sums: Sums when true, otherwise multiply
    """
    if sums:
        print(a_number + b_number)
    else:
        print(a_number * b_number)

Help:

pascal@archbook:~/$ cliche calculator.py sum_or_multiply --help

usage: cliche sum_or_multiply [-h] [--b_number B_NUMBER] [--sums] a_number

Sums or multiplies a and b

positional arguments:
  a_number             |int| the first one

optional arguments:
  -h, --help           show this help message and exit
  --b_number B_NUMBER  |int| Default: 10 | This parameter seems to be
  --sums               |bool| Default: False | Sums when true, otherwise multiply

Calling it:

pascal@archbook:~/$ cliche calculator.py sum_or_multiply 1
10

pascal@archbook:~/$ cliche calculator.py sum_or_multiply --sum 1
11

pascal@archbook:~/$ cliche calculator.py sum_or_multiply --b_number 3 2
6

More examples

Check the example files here

Comparison with other CLI generators

  • argparse: it is powerful, but you need a lot of code to construct an argparse CLI
  • click: you need a lot of decorators to construct a CLI, and not obvious how to use it
  • hug (cli): connected to a whole web framework, but gets a lot right
  • python-fire: low set up, but annoying traces all the time / ugly design, does not show default values nor types
  • cleo: requires too much code/objects to construct

cliche's People

Contributors

bambalaam avatar kootenpv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.