A modular package for building CLI applications
Project description
CLI App
A simple framework for building command-line interface applications in Python.
Overview
cliapp provides a structured way to create interactive command-line applications with support for configuration loading, command definition, and argument parsing. It leverages cmd2 for the core interactive shell functionality and argparse for command argument handling.
The framework is centered around two key components:
Application: The main class that initializes the application, loads configuration, manages commands, and runs the command loop.Command: A class representing a single command within the application, defining its executable function and arguments.
Installation
Assuming cliapp is packaged correctly (e.g., with setuptools or poetry), you would typically install it using pip:
pip install cliapp
You will also need to install the dependencies used by the framework:
pip install cmd2 pyfiglet
If you plan to use the asynchronous execution features, you'll need an async-compatible environment (like asyncio, which is in the standard library).
Usage
Here's a basic example demonstrating how to create a simple CLI application with a command:
# main.py
from cliapp import Application, Command
import sys
# Define a simple command executable function
def exec(name="World", greeting="Hello"):
"""A command that greets the user."""
print(f"{greeting}, {name}!")
# Define an async command executable function
async def async_exec(name="World", greeting="Hello"):
"""An async command example."""
import asyncio
await asyncio.sleep(2) # Simulate async work
print(f"Asynchronous {greeting}, {name}!")
if __name__ == "__main__":
# Create the application instance
# Configuration path is optional, defaults will be used if None is passed
# or if the file doesn't exist/fails to load.
app = Application(config="./config.json")
# Create Command instances
command = Command("hello", executable=exec)
# Add arguments to the command
command.addOption(full="name", help="an individual's name", default="World")
command.addOption(short="g", help="a greeting", default="Hello", required=False)
async_command = Command("asynchello", executable=async_exec)
async_command.addOption(full="name", help="an individual's name", default="World")
async_command.addOption(short="g", help="a greeting", default="Hello", required=False)
# Add commands to the application
app.add(command)
app.add(async_command)
# Run the application command loop
app.run()
Save the above as main.py and run it:
python main.py
You will see the intro banner and the command prompt. You can type help to see available commands:
>> help
And run your defined command:
>> greet --name Devin -g Hey
Hi, Devin!
>> asyncgreet --name Dev
Asynchronous Hello, Dev!
Configuration
The application uses a JSON configuration file. The structure is defined by the ApplicationConfig class and its nested configuration classes (VersionConfig, ThemeConfig, ShellConfig, ColorConfig).
- The configuration file is in JSON format.
- There is no default location or name for the configuration file.
- You must provide the path to the configuration file when creating the
Applicationinstance using theconfig_pathargument, likeApplication(config_path="./path/to/your/config.json"). - If no path is provided, or if the file cannot be loaded (e.g.,
FileNotFoundError,JSONDecodeError), the application will start with default values defined in thecliapp.util.defaultsmodule.
Shown below is a sample config.json file:
{
"shortname": "MyApp",
"fullname": "My Awesome Application",
"author": "Devin Green",
"version": {
"major": 1,
"minor": 1,
"patch": 0
},
"theme": {
"color": {
"primary": "#00FF00",
"secondary": "#FFFF00",
"text": "#CCCCCC"
},
"font": "ansi_regular"
},
"shell": {
"prompt": "$ ",
"intro": "Ready to go!",
"outro": "Goodbye!"
}
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to open an issue on the repository to discuss bugs or feature requests, or submit a pull request.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dev_cliapp-1.0.0.tar.gz.
File metadata
- Download URL: dev_cliapp-1.0.0.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59cda5d8ddacf3269f6b2ecf2bef7f0c4af96aa44845de4effb795dd47a05f2a
|
|
| MD5 |
e2820d08e6df4a2ca01304bec2890d15
|
|
| BLAKE2b-256 |
68632f3a99b884df80419bc1b0e0c13095eaa691875fa04aa61eeeb9ac27062b
|
File details
Details for the file dev_cliapp-1.0.0-py3-none-any.whl.
File metadata
- Download URL: dev_cliapp-1.0.0-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bf60914733d732bd522c8739d6eca80d5f9dfc9555340ed995d0ce2aa7b7ba5
|
|
| MD5 |
dc6d617b04bc99726a8a782a0a44db2e
|
|
| BLAKE2b-256 |
d87d8ce7a49555d4738dcbd967795f4d4dd815505a81ecca27ba396e6e12408f
|