Skip to main content

A CLI Frame-work

Project description

CliFrame - Python CLI Development Framework

A simple and powerful framework for building interactive command-line applications with dynamic command discovery and hierarchical command organization.

๐Ÿš€ Features

  • Hierarchical Command Structure - Organize commands in folders and files
  • Dynamic Command Discovery - Automatically discovers commands based on file structure
  • Interactive Shell - Rich interactive CLI with tab completion
  • Built-in Help System - Use ? to get help on any command or subcommand
  • Configuration Mode - Special configuration mode with _configure folder support
  • Rich Text Support - Color formatting and styled output
  • Customizable Prompts - Set custom prompt text and mode indicators
  • Method Chaining - Fluent API for easy CLI setup

๐Ÿ“ฆ Installation

pip install cliframe

๐Ÿ—๏ธ Quick Start

1. Basic Setup

from cliframe import CliService

# Create and run CLI service
cli = CliService()
cli.motd("Welcome to My CLI App!", rich=True)
cli.prompt("myapp")
cli.run()

2. Command Structure

Organize your commands in a cli/ directory structure:

cli/
โ”œโ”€โ”€ show/
โ”‚   โ”œโ”€โ”€ __init__.py          # 'show' command handler
โ”‚   โ”œโ”€โ”€ users.py             # 'show users' command
โ”‚   โ””โ”€โ”€ logs/
โ”‚       โ””โ”€โ”€ __init__.py      # 'show logs' command
โ”œโ”€โ”€ start.py                 # 'start' command
โ”œโ”€โ”€ stop.py                  # 'stop' command  
โ””โ”€โ”€ _configure/              # Configuration commands
    โ”œโ”€โ”€ network.py
    โ””โ”€โ”€ system.py

3. Command Handlers

Create command handlers as Python classes:

# cli/start.py
class StartCommand:
    """Start the application service."""
    
    def __init__(self, core):
        self.core = core
    
    def run(self, args=None):
        """Start the service with optional arguments."""
        print("Service started successfully!")
        return "Service is now running"

# Alternative: Use Command or StartCommand class names
class Command:  # Will be auto-discovered
    def run(self, args=None):
        print("Command executed!")

4. Advanced Configuration

from cliframe import CliService

cli = (CliService()
       .folder("/path/to/custom/cli/commands")  # Custom command folder
       .prompt("myapp")                         # Custom prompt
       .prompt_mode_sign("#")                   # Custom prompt sign
       .motd("๐ŸŽ‰ Welcome to MyApp CLI!", rich=True))

cli.run()

๐ŸŽฏ Command Organization

File-based Commands

  • command.py โ†’ command CLI command
  • Each file should contain a command handler class

Directory-based Commands

  • command/ โ†’ command CLI command group
  • command/__init__.py โ†’ Handler for base command
  • command/subcommand.py โ†’ Handler for command subcommand

Handler Class Discovery

CliFrame looks for handler classes in this order:

  1. StartCommand class
  2. Command class
  3. Any class ending with Command
  4. First class found in the module

๐Ÿ› ๏ธ Interactive Features

Help System

  • Type ? at the end of any command to get help
  • command ? - Shows help for the command
  • command sub ? - Shows help for subcommands

Tab Completion

  • Smart tab completion for all available commands
  • Context-aware completion based on current command path

Configuration Mode

  • Create _configure/ folder for configuration commands
  • Type configure to enter configuration mode
  • Prompt changes to (config)#
  • Type exit to leave configuration mode

๐Ÿ“ Example Usage

# Interactive CLI session
myapp> start ?                    # Get help for start command
myapp> show users                 # Execute show users command  
myapp> configure                  # Enter configuration mode
myapp(config)# network setup      # Run configuration command
myapp(config)# exit               # Exit configuration mode
myapp> exit                       # Exit CLI

๐ŸŽจ Styling and Output

Color Markers

Use color markers in your command output:

def run(self, args=None):
    return "[green]Success![/green] Operation completed [yellow]successfully[/yellow]"

Available Styles

  • [red]text[/red] - Red text
  • [green]text[/green] - Green text
  • [yellow]text[/yellow] - Yellow text
  • [blue]text[/blue] - Blue text
  • [magenta]text[/magenta] - Magenta text
  • [cyan]text[/cyan] - Cyan text

๐Ÿ”ง API Reference

CliService Class

Methods

  • __init__(core=None, cli_root=None) - Initialize CLI service
  • folder(path) - Set custom CLI commands directory
  • prompt(text) - Set prompt text (default: "cli")
  • prompt_mode_sign(sign) - Set prompt mode sign (default: ">")
  • motd(message, rich=True) - Set message of the day
  • run() - Start the interactive CLI

Method Chaining

All configuration methods return self for easy chaining:

cli = (CliService()
       .folder("./my-commands")
       .prompt("myapp")
       .motd("Welcome!"))

๐Ÿ“ Project Structure

your-project/
โ”œโ”€โ”€ cli/                    # Default CLI commands directory
โ”‚   โ”œโ”€โ”€ show/
โ”‚   โ”œโ”€โ”€ start.py
โ”‚   โ”œโ”€โ”€ stop.py
โ”‚   โ””โ”€โ”€ _configure/        # Configuration commands
โ”œโ”€โ”€ main.py                # Your CLI application entry point
โ””โ”€โ”€ requirements.txt

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This project is licensed under the MIT License.

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

cliframe-0.1.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

cliframe-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file cliframe-0.1.1.tar.gz.

File metadata

  • Download URL: cliframe-0.1.1.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cliframe-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8e20205149ba1b0e4002c81689d2c5d35db6e85a6cb3d4542071c03ebe6144e4
MD5 72840ce95f0daf5dec28ae1db2fc1577
BLAKE2b-256 9d37ed843d13954a6dd9e69277814c39a73c845c56e1e63b99cf80c86f488619

See more details on using hashes here.

File details

Details for the file cliframe-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cliframe-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cliframe-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 572c8cade1da4943b7384befced6b97abb05c1751782500b8e3383c5bbeff603
MD5 8d7ccf825539ecac7d61c564309aac53
BLAKE2b-256 8e94227733aa72fa7ea77b32cc452b32218e0207670d8764854c3b6541bda770

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