• Stars
    star
    105
  • Rank 317,571 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created about 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Build a simple command-line interface from your functions 💻

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 (unlike the click library)
  • ✓ Works with booleans (flags) and lists (multiple args) automatically
  • ✓ Standing on the shoulders of giants (i.e. it uses argparse and learnings from others)
  • ✓ Prints returned python objects in JSON (unless passing --raw)
  • ✓ Colorized output automatically
  • ✓ Allows creating executable by using cliche install <mycli>
  • ✓ Creates shortcuts, e.g. a variable "long_option" will be usable like --long-option and -l
  • ✓ No external dependencies -> 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):
    return a + b

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

pascal@archbook:~/calc$ cliche install calc
pascal@archbook:~/calc$ calc add --help

usage: calc add [-h] a b

positional arguments:
  a           |int|
  b           |int|

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

thus:

pascal@archbook:~/calc$ calc add 1 10
11

Installation of commands

You noticed we ran

cliche install calc

We can undo this with

cliche uninstall calc

Note that installing means that all @cli functions will be detected in the folder, not just of a single file, even after installation. You only have to install once, and on Linux it also adds autocompletion to your CLI if argcomplete has been installed.

Advanced Example

from cliche import cli

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

    :param a_number: the first one
    :param b_number: second one
    :param sums: Sums when true, otherwise multiply
    """
    if sums:
        print(a_number + b_number)
    else:
        print(a_number * b_number)

Help:

cliche rendered

Calling it:

pascal@archbook:~/calc$ calc add_or_mul 1
10

pascal@archbook:~/calc$ calc add_or_mul --sum 1
11

pascal@archbook:~/calc$ calc add_or_mul 2 -b 3
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. It does not keep things DRY. Also, the annotated function is not usable.
  • 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

More Repositories

1

whereami

Uses WiFi signals 📶 and machine learning to predict where you are
Python
5,081
star
2

yagmail

Send email in Python conveniently for gmail using yagmail
Python
2,579
star
3

neural_complete

A neural network trained to help writing neural network code using autocomplete
Python
1,149
star
4

gittyleaks

💧 Find sensitive information for a git repo
Python
687
star
5

sky

🌅 next generation web crawling using machine intelligence
Python
321
star
6

contractions

Fixes contractions such as `you're` to `you are`
Python
301
star
7

access_points

Scan your WiFi and get access point information and signal quality
Python
181
star
8

textsearch

Find strings/words in text; convenience and C speed 🎆
Python
121
star
9

brightml

Convenient Machine-Learned Auto Brightness (Linux)
Python
119
star
10

shrynk

Using Machine Learning to learn how to Compress ⚡
Python
109
star
11

loco

Share localhost through SSH. Local/Remote port forwarding made safe and easy.
Python
105
star
12

tok

Fast and customizable tokenization 🚤
Python
65
star
13

aserve

Easily mock an API ☕
Python
50
star
14

just

Just is a wrapper to automagically read/write a file based on extension
Python
48
star
15

xtoy

Automated Machine Learning: go from 'X' to 'y' without effort.
Python
47
star
16

spacy_api

Server/Client around Spacy to load spacy only once
Python
46
star
17

requests_viewer

View requests objects with style
Python
42
star
18

cant

For those who can't remember how to get a result
Python
33
star
19

aioyagmail

makes sending emails very easy by doing all the magic for you, asynchronously
Python
29
star
20

sysdm

Scripts as a service. Builds on systemd (for Linux)
Python
21
star
21

deep_eye2mouse

Move the mouse by your webcam + eyes
Python
20
star
22

reddit_ml_challenge

Reddit Machine Learning: Tagging Challenge
Python
19
star
23

inthenews.io

Get the latest and greatest in news (on Python)
CSS
19
star
24

crtime

Get creation time of files for any platform - no external dependencies ⏰
Python
16
star
25

natura

Find currencies / money talk in natural text
Python
15
star
26

rebrand

✨ Refactor your software using programming language independent, case-preserving string replacement 💄
Python
15
star
27

emacs-kooten-theme

Dark color theme by kootenpv
Emacs Lisp
14
star
28

justdb

Just a thread/process-safe, file-based, fast, database.
Python
8
star
29

fastlang

Fast Detection of Language without Dependencies
Python
7
star
30

quickpip

A template for creating a quick, maintainable and high quality pypi project
Python
7
star
31

xdb

Ambition: Single API for any database in Python
Python
6
star
32

nostalgia_chrome

Self tracking your online life!
Python
5
star
33

cnn_basics

NLP using CNN on Cornell Movie Ratings
Python
4
star
34

kootenpv.github.io

Pascal van Kooten's website hosted on github.io
CSS
3
star
35

gittraffic

Save your gittrafic data so it won't get lost!
Python
3
star
36

flymake-solidity

flymake for solidity, using flymake-easy: live feedback on writing solidity contracts
Emacs Lisp
3
star
37

ppm

Safe password manager
C
2
star
38

automl_presentation

Example code for the presentation "Automated Machine Learning"
Python
2
star
39

dot_access

Makes nested python objects easy to go through
Python
1
star
40

feedview

View a feed url with `feedview <URL>`
Python
1
star
41

PassMan

android app for ppm
C
1
star
42

mockle

Automatic Mocking by Pickles
Python
1
star
43

emoji-picker

Python
1
star