Skip to main content

Simple Multi Command Line Parser

Project description

https://travis-ci.org/vkrizan/python-smclip.svg?branch=master https://codecov.io/github/vkrizan/python-smclip/coverage.svg?branch=master

SMCLIP is a simple framework for parsing multi command line arguments using python’s builtin ArgumentParser. With SMCLIP you are able to built git-like commands. Commands are created by inheriting from base Command classes and extending their callbacks.

Core features includes:

  • building trees of multi-level sub-commands

  • support for default sub-commands (e.g. git stash [save])

  • support for fallback sub-commands (when sub-command is not found; e.g. catching ID)

  • support for chained sub-commands

  • sub-command lazy loading

Differences with …

Why you should choose SMCLIP when [X] supports sub-commands?

ArgumentParser

https://docs.python.org/3/library/argparse.html#sub-commands

Python’s ArgumentParser support sub-commands by adding sub-parsers to the parent parser. The ArgumentParser with sub-commands considers that a CLI application is simple and does simple operations. A CLI application starts can get confusing to users (in help) when there are more and more sub-commands being added.

Subparsers of ArgumentParser should be constructed prior to calling the base parser. Subparsers don not support any routing based on the commands.

Click

http://click.pocoo.org/latest/

Click utilizes Python’s decorators to create sub-commands hierarchy. Using decorators makes the code more readable. On the other hand, extending Click is done by creating new decorators. Supporting new parsing techniques (default or fallback commands) and conditional callback triggering is not done simplistically.

Cement

http://builtoncement.com/

Cement is a framework intended for complex CLI applications. Cement comes with multiple set of utilities (logging, caching). Command line parsing is a one part of Cement framework. Cement support sub-commands by nesting controllers. Default and fallback commands and conditional callback triggering is done by extending core controllers.

Argument Examples

# Simple command
(1)$ app help
         *--*
           +---- sub-command of base application

# Two commands
(2)$ app task list
(3)$ app --debug task list --listopt
     *----+----* *--* *-----+------*
           \       \         \
            \       \         +------- list sub-command with option
             \       +---------------- task command
              +----------------------- application with its own option

# fallback command
(4)$ app task 1234 --viewopt
              *-----+------*
                     \
                      +------- fallback command catching ID

# chained commands
(5)$ app task 1234 change --state ASSIGNED assign person
                   *----------+----------* *-----+-----*
                              |                   \
                               \                   +------ chained command
                                \                          with positional
                                 \                         argument
                                  \
                                   +---------------------- chained command
                                                           with option

Callbacks

preprocess

preprocess is called after successful parsing of command’s arguments. Invocation is done even when a sub-command is being called. preprocess is not called when default command is in effect.

this_action

this_action is a main callback for actions in current command. This callback is called after preprocess and only when current command is final (no other sub-commands are being invoked). Exceptions are chained command, in which the this_action is invoked every time.

results_callback

(commands group only)

results_callback is called after a sub-command invocation is done. A return value from sub-command, returned by this_action callback, is passed as a positional argument. Results from chained commands are wrapped and passed in ChainedOutputResults class object.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smclip-0.3.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smclip-0.3.0-py2.py3-none-any.whl (12.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file smclip-0.3.0.tar.gz.

File metadata

  • Download URL: smclip-0.3.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for smclip-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ffe111e9a127cbe11a6687df95a3c89108d2893da0a327b7f4f63e1e12266af1
MD5 e30a675cb35efd53e177e8f5dd789327
BLAKE2b-256 14b7c2c15d6ef5e7b63d59e56467eef0c7ea442b98281655113d361464e7ac67

See more details on using hashes here.

File details

Details for the file smclip-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for smclip-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 70dcab275ac5bc3dca34df5a8d6507b163131e22401e98dccb47ef0f2d8270c0
MD5 b964497b32f2d5f230edb13f4757b0b1
BLAKE2b-256 7b69703023ea511fca630516d2571b23a2056abd7443322cf2a590a9bdfcc13e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page