Skip to main content

a lightweight framework for creating command line interfaces quickly

Project description

cliq: creating command line interface quickly

cliq is a lightweight framework for creating a command line application or writing a libary powered with command line tools.

  • supports nested subcommands
  • equiped with init and config system
  • supports multiple command line tools in a single library
  • only depends on the standard library

Quick Start

Install cliq:

$ pip install cliq

Create your project:

$ cliq create project ./myapp
$ pip install -e ./myapp
$ myapp

Create a new command:

$ cliq create command do.py
$ python do.py -h

Add the command to your project:

$ mv do.py ./myapp/myapp/main/command/
$ myapp do -h

Edit your command:

"""do: 
"""

_setup_ = {
    'version' : '0.0.0',
    'description' : '',
}

import sys
from cliq.main.command import SimpleCommand

def init(app):
    return Command(app)

class Command(SimpleCommand):
    def __init__(self, app = None, name = __name__):
        super().__init__(app, name)

        # self.parser is an argparse.ArumentParser
        # see https://docs.python.org/3/library/argparse.html       
        #
        # add arguments. for example:
        #
        # self.parser.add_argument('input', type=str, help='input filename')
        # self.parser.add_argument('-v', '--verbose', action='store_true', help='verbose')
        # self.parser.add_argument('-o', '--output', type=str, help='output filename')

    def run(self, argv):
        args = self.parser.parse_args(argv)

        # implement command line functionalities
        print(args)
 
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    command = Command()
    command.run(argv)

if __name__ == '__main__' :
    main()

Remove help, init, config commands if you don't need them:

$ cd ./myapp/myapp/main/command
$ rm help.py init.py config.py

Commands

  • A command is standalone and complete by itself.
  • You can run it as an independent script.
  • Just copy a command script into your project.
  • There is nothing to be configured.

Simple command

Generate a simple command template script file:

$ cliq create command say.py  
$ python say.py -h
usage: __main__ [-h]

options:
  -h, --help  show this help message and exit

Add arguments to self.parser and implement run method:

""" say: a toy simple command
"""

_setup_ = {
    'version' : '0.1.0',
    'description' : 'a toy simple command'
}

import sys
from cliq.main.command import SimpleCommand

def init(app):
    return SayCommand(app)

class SayCommand(SimpleCommand):
    def __init__(self, app = None, name = 'say'):
        super().__init__(app, name)
        
        self.parser.add_argument('something', type=str, help='something')
        
    def run(self, argv):
        args = self.parser.parse_args(argv)
        print(args.something)
 
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    command = SayCommand()
    command.run(argv)

if __name__ == '__main__' :
    main()

Use it:

$ python say.py hello
hello

Complex command with nested subcommands

Tutorials

Nested commands

Create a command script file with the subcommands option:

$ cliq create command do.py --with-subcommands something,anything,nothing 

Test it:

$ python do.py
usage: __main__ [-h] {something,anything,nothing} ...

options:
  -h, --help            show this help message and exit

subcommands:
  {something,anything,nothing}
                        command help
    something           something
    anything            anything
    nothing             nothing

Edit it and put it into the command directory:

$ mv do.py myapp/myapp/main/command/

Project with multiple command line modules

You can create a project with multiple command line interface modules.

$ cliq create project ./holy --with-cli graham,terry 
$ pip install -e ./holy

Directory strucutre:

holy/
└── holy
    ├── graham
    │   └── main
    │       └── command
    └── terry
        └── main
            └── command

Test cli modules:

$ graham -h 
...

$ terry -h 
...

Add commands:

$ cliq create command holy/holy/graham/main/command/play.py
$ graham play -h

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

cliq-0.9.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

cliq-0.9.1-py2.py3-none-any.whl (18.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cliq-0.9.1.tar.gz.

File metadata

  • Download URL: cliq-0.9.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for cliq-0.9.1.tar.gz
Algorithm Hash digest
SHA256 70a2dac4edfb8fe8708f4dfe3178fef74d2a831a4ae9a3b50ce92f23cd672982
MD5 a71890ceec74850d7271e1de647d09f7
BLAKE2b-256 2a3d6d0c4abe7ca9ee5ab31ba65faed4fc697aba539bbd646866c4939e813a23

See more details on using hashes here.

File details

Details for the file cliq-0.9.1-py2.py3-none-any.whl.

File metadata

  • Download URL: cliq-0.9.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for cliq-0.9.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b3f6eea29d318c81ce4b722ea893751303ab247a4c6f17b44b6a1f13888adba7
MD5 036be3152877c6d78c282f2e00fc64f0
BLAKE2b-256 fc0c36fa7294a5c2b29068664d295097573cb3b5eea6560ed2c5d263d38e7885

See more details on using hashes here.

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