Skip to main content

An automatic CLI interface framework.

Project description

clidesc

clidesc is a CLI interface framework that can be used to build simple, yet functional, command line interfaces with minimal client code.

The goal is to create a framework to build the command line interface only using configuration files (YAML or JSON format), and minimizing the need to write code for it.

Usage

To create a simple "Greeting" application, the CLI definition file, should look like:

---
program: greeting
description: A greeting application.
version: 1.0
handler: greeting.hello
arguments:
  - name: someone
    description: Someone to greet.
    type: string
    required: true

And the application code would be:

# Contents of greeting.py

from clidesc import CLIDesc

def hello(someone):
    print(f"Hello, {someone}!")

if __name__ == "__main__":
    cli = CLIDesc.from_file("greeting.yml")
    cli.run()

With this configuration, the application will have options to display its version (--version), help instructions (-h or --help), and a required positional argument. If run with --help, the output is:

usage: greeting [-h] [--version] someone

A greeting application.

positional arguments:
  someone     Someone to greet.

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

If the application does not receive the required argument, an error is displayed. For example, the output for running greeting is:

usage: greeting [-h] [--version] someone
greeting: error: the following arguments are required: someone

When running an application with one parameter, greeting World, the output would be:

Hello, World!

You may also use clidesc to automatically format the output returned by the handler methods. Use the output attribute along with the handler method to configure the output format.

The next example configures the output format, with a formatting string, that follows Python's formatting rules.

---
program: output
description: Auto-formatting output.
version: 1.0
handler: output.hello
output:
  format: "Hello, {someone}"
arguments:
  - name: someone
    description: Someone to greet.
    required: true

And the code for this application would be:

# contents of output.py.

from clidesc import CLIDesc


def hello(someone):
    """Greet someone."""
    return {"someone": someone}


if __name__ == "__main__":
    cli = CLIDesc.from_file('output.yaml'))
    cli.run()

Applications with multiple commands and command groups (like git) are supported through sub_commands. Each command in sub_command can have its own sub_command, creating a command hierarchy (deep hierarchies are not recommended).

The configuration for such application would be:

---
program: multi
description: A multi-command application.
version: 1.0
sub_commands:
  title: Commands
  description: Application sub-commands
  group_name: Sub commands
  commands:
    - name: abc
      description: First command.
      handler: multi.abc
      arguments:
      - name: some_arg
        description: Some argument.
    - name: xyz
      description: Second command.
      handler: multi.xyz
      arguments:
      - name: another_arg
        description: Another argument.

And the client code:

# contents of multi.py

from clidesc import CLIDesc

def abc(some_arg):
    """Greet someone."""
    print(f"ABC: {some_arg}")


def xyz(another_arg):
    """Greet someone."""
    print(f"XYZ: {another_arg}")


if __name__ == "__main__":
    cli = CLIDesc.from_file("multi.yml")
    cli.run()

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

clidesc-0.3.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

clidesc-0.3.0-py3.9.egg (9.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: clidesc-0.3.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for clidesc-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2285e22badb4a710c6013e426867cb46b07419d4c5791148148c75f9b2306b95
MD5 1281ecee64657294ac33cdad5721954c
BLAKE2b-256 0912498cf611229d799d536799960b3e13995e0508a9cc2039e324c49aed47f8

See more details on using hashes here.

File details

Details for the file clidesc-0.3.0-py3.9.egg.

File metadata

  • Download URL: clidesc-0.3.0-py3.9.egg
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for clidesc-0.3.0-py3.9.egg
Algorithm Hash digest
SHA256 c604bfec1ce1c7fc36e8da9c137889b3d907bc03e242db371d691c247871639e
MD5 4d4f87fa04ac4a7899f88fb032821c56
BLAKE2b-256 ff3c5fc14a3d31ad43ee4d989e6f9f8f2bd166e7b3d3818747f0ea0cf77e175c

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