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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for simple_CLI_builder-0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2482ae6254c8d9ce2635bfe8dc5931cf58c5a3fcfc86888612ad5c830eb976c7 |
|
MD5 | 4870bacc6cdf5755b7cd5c9bf16943ba |
|
BLAKE2b-256 | 3ecb67ac45311bad7a94ff9c55b2ea275b17aa58707045598bc453bf63aa6e67 |