Skip to main content

A lightweight and minimal CLI framework

Project description

from cli import UnknownCommandError> [!IMPORTANT]

While foxcli is pre-1.0.0, breaking changes may be made without bumping the major version!

foxcli is a minimal-by-design CLI framework for Python.

There are many great CLI frameworks out there, but some features I like in one aren't present in another. Likewise, one framework I like may have features that I don't like or has a bunch of extra features that I don't need.

foxcli being minimal-by-design means that it does not include any built-in commands, no TUI helpers, no command usage generation. You will have to handle all of this yourself.

This framework features:

  • Class-based commands + being able to subclass its Command class
  • Multi-level commands (like user create)
  • Global options that can appear anywhere in command invocation
  • Hooks to customize error handling

foxcli is still very much in early development but certainly usable, in fact I am using it in a testdrive with my project WinUtils!

Planned features include:

  • Counted arguments like -vvv

Installation

pip install foxcli

Sample

from sys import exit
from foxcli.cli import CLI
from foxcli.command import Command
from foxcli.argument import Argument
from foxcli.option import Opt, Option

class App(CLI):
    # hooks to customize error handling
    def on_unknown_command(self, e):
        # command not found, show a custom usage message?
        return 1

app = App(
    name='myapp',
    version='1.0.0',
    description='My app',
    global_options=[
        # global options, accessible in commands via `self.ctx.global_options`
        Option(name='verbose', short='v', default=False, help='Show verbose output'),
    ]
)

@app.command()
class Version(Command):
    name = 'version'
    description = 'Show version'

    def run(self, args) -> int:
        print(self.ctx.cli.version)
        return 0

# subclassing `Command` to add options
class UserableCommand(Command):
    arguments = [
        Argument('username'),  # supports `nargs` which takes int, '*', '+', and '?'. defaults to 1, which implicitly makes it required
    ]

@app.command()
class User(Command):
    name = 'user'
    aliases = ['u']
    description = 'User management commands'

# multi-level command
@app.command(parent='user')
class UserCreate(UserableCommand):
    name = 'create'
    description = 'Creates a new user'
    aliases = ['c', 'add']
    arguments = [
        # positional arguments
        Argument('avatar', default='https://website.com/image.png'),  # supports default values
    ]
    options = [
        Opt('rank', short='r', required=True)  # shortcut for `Option`, `Arg` also exists
    ]

    # `myapp user add Caim -r "Super Admin" -v`
    def run(self, args) -> int:
        # self.ctx.global_options.get('verbose', bool) -> True
        print(self.ctx.global_options.to_dict())  # {'verbose': True}

        # args.get('username', str) -> 'Caim'
        # args.get('avatar', str) -> 'https://website.com/image.png'
        # args.get('rank', str) -> 'Super Admin'
        print(args.to_dict())  # {'username': 'Caim', 'avatar': 'https://website.com/image.png', 'rank': 'Super Admin'}
        return 0

exit(app.run())

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

foxcli-0.4.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

foxcli-0.4.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file foxcli-0.4.0.tar.gz.

File metadata

  • Download URL: foxcli-0.4.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for foxcli-0.4.0.tar.gz
Algorithm Hash digest
SHA256 66b1d5b69b6e28e2daf30ddd4058f85063ffddf7a20191763ce34a4a5d70f5e6
MD5 1b8dc557319c0827672b72e58fabccbf
BLAKE2b-256 18c6772e7ca7bfb2c8aac459449dcf13a1150686f45799075a117749c74b3bda

See more details on using hashes here.

File details

Details for the file foxcli-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: foxcli-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for foxcli-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d71e8bfde071a719d201328ae7f3b30df2046aaf458c6ff12fe830af0bb4953
MD5 d0fe57532ce5abed8b45ab26edc53964
BLAKE2b-256 dce0332f1f660eae767442f1b196707ab72857d9d7bcf8df388ed7771afc799b

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