Skip to main content

Command Line Object Chaining (cloc) - Modern cli framework for simple and complex cli applications

Project description

Command Line Object Chaining - cloc

MIT badge Python3.6 badge Platform badge

Modern cli framework for simple and complex cli applications

ToC

Requirements

  • System

    • Python 3.6+
  • Python Pip

    • requests

Installation

Virtual Environment is recommended

$ git clone https://www.github.com/tannerburns/cloc
$ cd cloc
$ pip3 install .

Information

Command line framework for making simple and complex command line applications.
  • Easily group commands together
  • Connect commands with classes for querysets
  • Create command line viewsets for abstracting user interaction on command querysets

Examples

Simple example

from cloc import cmd, grp, opt
from cloc.types import IntRange

@grp('cli')
def cli():
    """base cli"""
    pass

@cmd('hello')
@opt('--count', '-c', type=IntRange, default=1, help='Number of greetings: ex -c 0,5 OR -c 5')
@opt('--name', '-n', type=str, help='The person to greet')
def hello(count: IntRange, name: str):
    """Simple program that greets NAME for a total of COUNT times."""
    for _ in count:
        print(f'Hello {name!r}')

if __name__ == '__main__':
    cli.add_command(hello)
    cli()
$ python example4.py hello --help

Hello

        Simple program that greets NAME for a total of COUNT times.

USAGE: hello --count|-c [value] --name|-n [value] 

Parameters:
| Name               | Short    | Type             | Help                                                   |
| ------------------ | -------- | ---------------- | ------------------------------------------------------ |
| --count            | -c       | cloc.IntRange    | [default: 1] Number of greetings: ex -c 0,5 OR -c 5    |
| --name             | -n       | str              | [default: None] The person to greet                    |

Using a viewset

from cloc import grp
from cloc.viewsets import ReqSessionViewset

@grp('cli')
def cli():
    """requests session cli"""
    pass

session_viewset = ReqSessionViewset(raise_exception=False)

cli.add_command(session_viewset)

if __name__ == '__main__':
    cli()

The get command is implemented by the ReqSessionViewset and does not have to be defined

$ python example3.py get https://jsonplaceholder.typicode.com/todos/1
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Using a queryset

from cloc import grp
from cloc.viewsets import ReadOnlyViewset, GrpQueryset

@grp('cli')
def cli():
    """user and permissions queryset cli"""
    pass

@grp('users1')
def users1():
    """users1 queryset"""
    pass

@grp('perms1')
def perms1():
    """perms1 queryset"""
    pass

@grp('users2')
def users2():
    """users2 queryset"""
    pass

@grp('perms2')
def perms2():
    """perms2 queryset"""
    pass

class UserViewset(ReadOnlyViewset):
    version = '1.0.0'
    queryset = GrpQueryset

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

class PermissionViewset(ReadOnlyViewset):
    version = '0.0.1'
    queryset = GrpQueryset

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


uvs1 = UserViewset(users=['user1', 'user2'])
pvs1 = PermissionViewset(roles=['role1', 'role2'])
uvs2 = UserViewset(users=['user1', 'user2', 'user3'])
pvs2 = PermissionViewset(roles=['role1', 'role2', 'role3'])

perms1.add_command(pvs1)
users1.add_command(uvs1)
users1.add_command(perms1)

perms2.add_command(pvs2)
users2.add_command(uvs2)
users2.add_command(perms2)

cli.add_command(users1)
cli.add_command(users2)

if __name__ == '__main__':
    cli()
$ python example2.py users1 list
'users' user1, user2
'version' 1.0.0

$ python example2.py users2 list
'users' user1, user2, user3
'version' 1.0.0

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

cloc-0.1.5.tar.gz (13.2 kB view details)

Uploaded Source

File details

Details for the file cloc-0.1.5.tar.gz.

File metadata

  • Download URL: cloc-0.1.5.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for cloc-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9b0d4bea21ae91fa9280053a3d893c669c902c7552e4ad4d442808438f61ecaf
MD5 6c3182f1e2654d3a8b428f7aa5a52b67
BLAKE2b-256 a2abc5759b4053ee61323bc1a8f2d0e58f51c67ecdd0952b7eb3a6e09e8abeee

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