Skip to main content

Python CLI App

Framework for creating CLI apps using Python

The purpose of this library is to speed up bulding CLI applications by providing abstract classes representing an app and its commands. It uses argparse to define and parse command line arguments.

Concepts

App

An "App" is the main entry point of an application. It groups together one or more commands

Command

A "Command" is a single operation that the application can perform. It is identified by a positional argument on the command line. Let's take git as an example. git would be the app that provides different commands, such as add, commit, merge, checkout, etc.

Example

git.py

#!/usr/bin/env python3

from cli_app import App
from commands.checkout import Checkout
from commands.merge import Merge


class Git(App):
    """Git - fast, scalable, distributed revision control system"""

    def register_commands(self):
        self.add_command('checkout', Checkout)  # make the Checkout command available through `git.py checkout …`
        self.add_command('merge', Merge)  # make the Merge command available through `git.py merge …`


if __name__ == '__main__':
    app = Git()
    app.run()

commands/checkout.py

from cli_app import Command


class Checkout(Command):
    """Switch branches or restore working tree files"""

    @staticmethod
    def register_arguments(parser):
        parser.add_argument('ref', type=str, help='The ref (branch name, tag, commit sha) to checkout')

    def run(self):
        # Do whatever needs to be done to checkout given ref
        print('Checking out %s' % self.app.args.ref)

commands/merge.py

from cli_app import Command


class Merge(Command):
    """Join two or more development histories together"""

    @staticmethod
    def register_arguments(parser):
        parser.add_argument('branch', type=str, help='The branch to merge into the currently checked-out branch')

    def run(self):
        # Do whatever needs to be done to merge given branch
        print('Merging %s into current branch' % self.app.args.branch)

Release files for cli-app 0.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 cli-app 0.1.0
File Size Uploaded
cli-app-0.1.0.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cli-app 0.1.0
File Interpreter ABI Platform
cli_app-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / cli-app-0.1.0.tar.gz

Download URL cli-app-0.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
826e541f3aa3243ba2d134d20435ace714912166b34f74bfe64852e55a49d90d
BLAKE2b-256 checksum
How to use checksums
74aee7a944793bd87ff96e0f488419fe780848f83fea4134329b0893a2ac50e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Release files / cli_app-0.1.0-py3-none-any.whl

Download URL cli_app-0.1.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
652d829e6cfa44a776d9442f142178406741f3cec7e9fa597b1fe954873ed495
BLAKE2b-256 checksum
How to use checksums
02f97cc1737a26d9133e5dd3f94945a5e685cca9735aa93c56c216d0c3865851
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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