Skip to main content

Shell command dispatcher with nested subcommand support

Project description

k3shell

Action-CI Build Status Documentation Status Package

A python module to manage commands.

k3shell is a component of pykit3 project: a python3 toolkit set.

Name

k3shell

Status

The library is considered production ready.

Install

pip install k3shell

Synopsis

import k3shell
import sys
arguments = {
    'echo_repr': (
        lambda x: sys.stdout.write(repr(x)),
        ('x', {'nargs': '+', 'help': 'just an input message'}),
    ),

    'foo': {
        'bar': lambda: sys.stdout.write('bar'),

        'bob': {
            'plus': (
                lambda x, y: sys.stdout.write(str(x + y)),
                ('x', {'type': int, help: 'an int is needed'}),
                ('y', {'type': int, help: 'an int is needed'}),
            ),
        },
    },

    '__add_help__': {
        ('echo_repr',)           : 'output what is input.',
        ('foo', 'bar',)          : 'print a "bar".',
        ('foo', 'bob', 'plus',)  : 'do addition operation with 2 numbers.',
    },

    '__description__': 'this is an example command.',
}

k3shell.command(**arguments)
"""

then you can execute your command as:

python demo.py echo_repr hello!
# 'hello!'

python demo.py foo bar
# 'bar'

python demo.py foo bob plus 1 2
# 3


and you can get a usage help message like:

$ python demo.py -h
---------------------------
usage: demo.py [-h] {echo_repr, foo bar, foo bob plus} ...

this is an example command.

positional arguments:
  {echo_repr, foo bar, foo bob plus} commands to select ...
    echo_repr            output what is input.
    foo bar              print a "bar".
    foo bob plus         do addition operation with 2 numbers.

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


$ python demo.py foo bob plus -h
--------------------------
usage: demo.py foo bob plus [-h] x y

positional arguments:
    x   an int is need
    y   an int is need

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

"""

Author

Wenbo Li(李文博) wenbo.li@baishancloud.com

Copyright and License

The MIT License (MIT)

Copyright (c) 2017 Wenbo Li(李文博) wenbo.li@baishancloud.com

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

k3shell-0.1.3.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

k3shell-0.1.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file k3shell-0.1.3.tar.gz.

File metadata

  • Download URL: k3shell-0.1.3.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3shell-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b5b18f293e590f1833d9bc7c9ae05075b64c0e5c14a2db7a9c2b5e5befc44697
MD5 2290d803c7a66e69afb02c60ad91f1f5
BLAKE2b-256 4a8ada08e0c35359d12bfbee6f72174e13aeab0f25338c541c79dcf7919fce0c

See more details on using hashes here.

File details

Details for the file k3shell-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: k3shell-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3shell-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 32f9907740cb7b3f5a111ffdbd4c4afeafeb53ce8b6c90f81ea79585c359915b
MD5 8c832ac5b5cff3ba504b42d43793c842
BLAKE2b-256 2a33b12cd1d4689dfde3859d4592094c4478a4b8bdffe3fc008cc89951996d98

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