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

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

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

Parameters
================================================================================
--count -c 'Opt' 'cloc.IntRange' [default: 1] Number of greetings: ex -c 0,5 OR -c 5
--name -n 'Opt' 'str' 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.4.tar.gz (13.1 kB view hashes)

Uploaded Source

Supported by

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