Skip to main content

No project description provided

Project description

CLI builder

A simple class to ease up the process of building applications run in a terminal. For the most part, it only offers a python class which will load predefined commands, parse input, ensure the user inputs the command properly, and finally features a prebuilt "help" command which will display the name, description, and syntax of all predefined functions.

Documentation

Creates a class:

from simple_CLI_builder import *

app = CLI()

Load predefined functions

from a json file:

from simple_CLI_builder import *

app = CLI()
app.json_to_lookup('defined_functions.json')

where defined_functions.json:

{
    "commands": [
        {
            "name": str,
            "description": str,
            "syntax": str,
            "min_args": int,
            "max_args": int,
            "defined_flags": str
        }
    ]
}

Only the name field is required. Not including the other fields will have their values defaulted to None.

If you would like to not use a .json and manually populate the table for whatever god forsaken reason you can create a list of objects identical to the ones seen in the .json file and call app.populate_lookup(list) instead of app.json_to_lookup(path)

Parsing input

from simple_CLI_builder import *

app = CLI()
app.json_to_lookup('defined_functions.json')

while True:
    app.parse_input(input("> "))

Upon input, the property app.current_command will be populated with an instance of the class Executed_Command() where the class is defined as follows:

class Executed_Command():
    def __init__(self):
        self.name = None
        self.args = []
        self.flags = []
        self.properties = None

The command's arguements can be accessed through app.current_command.property. For example:

from simple_CLI_builder import *

app = CLI()
app.json_to_lookup('defined_functions.json')

app.parse_input("command arg1 arg2")
print(app.current_command.args)

would return:

['arg1', 'arg2']

This works with flags as well.

To access the predefined properties of the command (such as its name, description, min and max number of args, etc) being called use app.current_command.properties.property. For example:

from simple_CLI_builder import *

app = CLI()
app.json_to_lookup('defined_functions.json')

app.parse_input("command arg1 arg2")
print(app.current_command.properties.name)

would return the name value of the current command found in the previously defined commands (same as the ones found in defined_functions.json)

From here, a simple switch statement can be used to provide functionality to the command. For example:

from simple_CLI_builder import *

app = CLI()
app.json_to_lookup('defined_functions.json')

while True:
    app.parse_input(input("> "))

    match app.current_command.properties.name:
        case 'help':
            app.help() # Prints all command names along with their syntax
                       # This is a predefined function in the class

        case 'hello':
            print('Hello, world!') 

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

simple_CLI_builder-0.4.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

simple_CLI_builder-0.4-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file simple_CLI_builder-0.4.tar.gz.

File metadata

  • Download URL: simple_CLI_builder-0.4.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for simple_CLI_builder-0.4.tar.gz
Algorithm Hash digest
SHA256 885d7e6fdc4d195f87a0ce8abffa54514054d90d43322d80b68248a828bc5f6e
MD5 47074b9a5764f8b5f60b22a6469a321f
BLAKE2b-256 722a12a48b65d40d5afe18c1c693cf92c0e71256346371ee4b4189e6c0b79131

See more details on using hashes here.

File details

Details for the file simple_CLI_builder-0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_CLI_builder-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 92d14d4f721f2355eb16b0a832ba7e9a1a1e73bf5379d050c687c85f3bd81316
MD5 c243683730e3c718f1db3c7b227f2bff
BLAKE2b-256 33870aa059befd034f235950bf46c242a18d916236f1c0c9b1ac158384b9b283

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