Skip to main content

Command Line avec Klass

Project description

Clak

Python Version License

Clak (Command Line avec Klass) is a Python library that simplifies the creation of complex command-line interfaces using a class-based approach. It extends Python's argparse to provide an elegant and maintainable way to define nested commands and arguments.

Features

  • Hierarchical command based structure built with python class. No need to learn a new framewok, just use Python!

  • Easy to use, easy to extend, easy to understand. Focus on your app, not your CLI.

  • Based on Python Argparser. All what your learned is still useful, you wont be lost as it follows the same syntax.

  • Light and minimalistic, while providing standard features via optional components.

  • 🎯 Class-based command-line interface definition

  • 🌳 Easy nested subcommand creation

  • 🔄 Automatic help formatting with command tree display

  • 🎨 Clean and intuitive API for defining arguments

  • 📦 Inheritance-based command organization

  • 🚀 Built on top of Python's standard argparse

Requirements

  • Python 3.9 or higher
  • argparse (built into Python)

Quick Start

Add clak to your project dependecies:

pip install clak

# If you use one of those
poetry add clak
pdm add clak

Here's a simple example showing how to create a simple git-like command structure:

from clak import Parser, Argument, Command

class ShowCommand(Parser):
    target = Argument('--target', '-t', help='Target to show')
    format = Argument('--format', choices=['json', 'text'], help='Output format')

class MainApp(Parser):
    debug = Argument('--debug', action='store_true', help='Enable debug mode')
    config = Argument('--config', '-c', help='Config file path')
    
    # Define subcommands
    show = Command(ShowCommand, help='Show something', choices=['phone', 'email', 'address'])

# Instanciate your app, parse command line and run appropiate command.
MainApp().dispatch()

This will create a CLI with the following structure:

$ python demo.py --help
myapp [-h] [--debug] [--config CONFIG] {show} ...
  show [-h] [--target TARGET] [--format {json,text}]

Key Concepts

Arguments

Define arguments using the Argument class:

class MyCommand(ArgumentParserPlus):
    # As class attributes
    verbose = Argument('-v', '--verbose', action='store_true', help='Verbose output')
    
    # Or in __cli__dict__
    __cli__dict__ = {
        'output': Argument('--output', '-o', help='Output file')
    }

Nested Commands

Create complex command hierarchies using the Command class:

class MainApp(ArgumentParserPlus):
    # As class attributes
    status = Command(StatusCommand, help='Show status')
    
    # Or in __cli__dict__
    __cli__dict__ = {
        'config': Command(ConfigCommand, help='Configure settings')
    }

Advanced Usage

Check the examples/ directory in the source code for more complex examples including:

  • Multi-level command nesting
  • Argument inheritance
  • Custom help formatting
  • Command grouping

TODO

Implementation:

  • Use more argparse plugins mechanisms

Features:

Contributing

From reporting a bug to submitting a pull request: every contribution is appreciated and welcome. Report bugs, ask questions and request features using Github issues. If you want to contribute to the code of this project, please read the Contribution Guidelines.

License

This project is licensed under the GPL v3 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

mrjk_clak-0.3.1.tar.gz (45.4 kB view details)

Uploaded Source

Built Distribution

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

mrjk_clak-0.3.1-py3-none-any.whl (50.2 kB view details)

Uploaded Python 3

File details

Details for the file mrjk_clak-0.3.1.tar.gz.

File metadata

  • Download URL: mrjk_clak-0.3.1.tar.gz
  • Upload date:
  • Size: 45.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.11 Linux/6.6.65-1-MANJARO

File hashes

Hashes for mrjk_clak-0.3.1.tar.gz
Algorithm Hash digest
SHA256 eb089b46d0e103e12b082a3a8d54531572a55db1365301a5791bf2e99a5e461f
MD5 c2ed6a3d0c8a026162acc5a7e8ce51fe
BLAKE2b-256 90b436971993a486ed80120ea5070937e37bd5b7c9302921e9f5fd3ac19d9499

See more details on using hashes here.

File details

Details for the file mrjk_clak-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: mrjk_clak-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 50.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.11 Linux/6.6.65-1-MANJARO

File hashes

Hashes for mrjk_clak-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f3dfbb0657ecb9f1587ce91913388744061fe37c0fdd08a8570ca8300340254
MD5 6b2ec4f9eb72c88f188c86de0fdb69b6
BLAKE2b-256 2538911ff9a2959d614aeddf3b8a14d7b7ff344c5defd1070b9baad47bd53cb6

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