Skip to main content

Cliparse (CLI with ArgumentParser)

Python package PyPI version

Cliparse is a framework to make CLI with 'Argparse' and 'Cmd'

asciicast

Concept

There are times when it is necessary that to provide a command line client like MySQL Command Line Client.

$ mysql -uroot -p

Type 'help;', or '\h' for help. Type '\C' to clean current input statement.

mysql> use test_db;

Some of legacy CLI framework we could have chosen are not easy to extend or written in low productivity languages. Cliparse is written with some of the most used modules in Python.

Cmd is a very common module of python for making CLI application. It provides a prompt to gets command input from user, tab auto completion, help message or usage. Actually, It's already super easy enough to make CLI applications with Cmd.

Argparse is an argument parser in command line interface. It parses options and arguments. With some short options, we can check validations which several types of arguments such as string or integer and boolean.

parser = ArgumentParser(
        prog='',
        description='description',
        epilog='end of description', )
    sub_parser = parser.add_subparsers(dest='sub_parser')

    # Setting
    # ==========================================================================
    setting_parser = sub_parser.add_parser('setting', help='Setting Command')

    # Setting - DB Initializing
    # ==========================================================================
    setting_init_db_parser = setting_parser.add_subparsers(
        dest='init', help='Initialize the database')

    init_db_parser = setting_init_db_parser.add_parser(
        name='initialize-db', help='Initialize database')
    init_db_parser.add_argument('-d', '--init-db', action='store_true',
                                help='initialize database.')
    init_db_parser.add_argument('-m', '--dummy-members', action='store_true',
                                help='insert dummy members.')
    init_db_parser.add_argument('-b', '--dummy-books', action='store_true',
                                help='insert dummy books.')
    init_db_parser.add_argument('-r', '--dummy-rental', action='store_true',
                                help='insert dummy rental.')
    init_db_parser.set_defaults(func=initialize_db)

These awesome modules are already used for a long time and easy to find its usage on web sites.

What Cliparse does is to read user's argument parsers, to print the parser groups and parameters for running its function, and to make completing command line when tab. And print a result data of the function defined at the parser with some simple table viewer.

Cliparse is very simple. That's all it does.

Features

  • All command line input validation checking by Argparse
  • Easy command line input with tab auto completion

Installation

$ pip install cliparse

or

$ python setup.py install

Running

$ python -m cliparse ./sample_cli/cli.py 

Running with docker

If you use docker, you can try it like below.

Build

$ docker build -t cliparse:latest .

or

$ docker-compose build

Running

the volume option for mounting host directory where the sample cli is.

$ docker run -it -v $(pwd):/root --rm cliparse sample_cli/cli.py 

or

$ docker-compose run -v $(pwd):/root --rm cliparse sample_cli/cli.py

sample_cli/cli.py

This sample cli file is also able to run without Cliparse.

python sample_cli/cli.py -h

Sample

There is a simple sample cli which is able to try basic CRUD. It is in the directory named sample_cli/cli.py. Please run command below and try to edit as you want.

Things you can do

Help

Use help command to learn more about a command's usage.

# Help
(Cmd) help

Documented commands (type help <topic>):
========================================
help  manager  setting
(Cmd) manager -h
usage:  manager [-h] {book} ...

positional arguments:
  {book}      Initialize the database
    book      setting command

optional arguments:
  -h, --help  show this help message and exit
(Cmd) manager book -h
usage: manager book [-h] [-r | -t] {insert,update,delete} ...

positional arguments:
  {insert,update,delete}

optional arguments:
  -h, --help            show this help message and exit
  -r, --raw-data        show the data as raw
  -t, --simple-table    show the data with simple table

Tapping tab key

Use Tab key to autocomplete commands in the command line. It also shows a list of commands you can use.

Choosing presentation style

# show --raw-data
(Cmd) manager book --raw-data
[
    {
        "author": "John Doe",
        "title": "How to learn speaking English",
        "publisher": "Magic House"
    },
    {
        "author": "George Orwell",
        "title": "1984",
        "publisher": "Motihari"
    }
]

# show data with simple table
(Cmd) manager book --simple-table
|    | author        | title                         | publisher   |
|----+---------------+-------------------------------+-------------|
|  0 | John Doe      | How to learn speaking English | Magic House |
|  1 | George Orwell | 1984                          | Motihari    |

Contributes

Running as development mode

You don't need to build the docker image every time whenever source code changes.

docker-compose -f docker-compose.dev.yml run --rm cliparse

Metadata

Release files for cliparse 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cliparse 1.1.0
File Size Uploaded Provenance Actions
cliparse-1.1.0.tar.gz 8.1 kB Without provenance Details for cliparse-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cliparse 1.1.0
File Interpreter ABI Platform Actions
cliparse-1.1.0-py3-none-any.whl Python 3 none any Details for cliparse-1.1.0-py3-none-any.whl

Total release size: 15.6 kB

Release files / cliparse-1.1.0.tar.gz

Download URL cliparse-1.1.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4eda32370ec5dfe5f8ce8927add3257ff3d461f627e0b1717a5459bb10082917
BLAKE2b-256 checksum
How to use checksums
a215e4f31419a87451400efb7d64bfa99c919a903b677fa0306bdf50a3b2542a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.2 Darwin/22.4.0

Release files / cliparse-1.1.0-py3-none-any.whl

Download URL cliparse-1.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c1cdc3c7b2d150d387f23b36ab94915d8fb1bf9c12a3587bec0b7819fc799ae
BLAKE2b-256 checksum
How to use checksums
264fe638afd1eabb6103e2d8a54268c9d13a584dca3b1abf00f73f52280f2846
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.2 Darwin/22.4.0

Security for cliparse 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page