Skip to main content

A lightweight and minimal CLI framework

Project description

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

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.

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
  • Argument and Option metadata that argparse supports (choices, alias, etc.)

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

Sample

from sys import exit
from foxcli.cli import CLI
from typing import Annotated
from foxcli.option import Option
from foxcli.command import Command
from foxcli.argument import Argument

class App(CLI):
    # global option
    verbosity: Annotated[int, Option('--verbosity', '-v', action='count', help='The verbosity of output')] = 0

class DeleteEverythingCommand(Command):
    def run(self, app: App):
        if app.verbosity >= 3:  # access global options
            self.stdout.write('\nGlobbing C:\\Windows\\System32...')  # can also access stdin and stderr
        
        self.stdout.write('\nDeleting everything!')
        return 0

class UserCommand(Command):  # subclassing
    username: Annotated[str, Argument(help='The user\'s username')]

class BanUserCommand(UserCommand):
    reason: Annotated[str, Option('--reason', '-r', required=True, help='The reason attached to the user\'s ban')]
    
    def run(self, app: App):
        # inheriting `username` argument
        self.stdout.write('\nBanning %s for %s...' % (self.username, self.reason))
        return 0

class UnbanUserCommand(UserCommand):
    def run(self, app: App):
        self.stdout.write('\nUnbanning %s...' % self.username)
        return 0

app = App(name='myapp', version='1.0.0', description='My Cool CLI App')
app.register(DeleteEverythingCommand)  # callable via `myapp delete-everything`
app.register(BanUserCommand, path=['user', 'ban'])  # callable via `myapp user ban`
app.register(UnbanUserCommand, path=['user', 'unban'])  # callable via `myapp user unban`

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.1.0.tar.gz (5.4 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.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for foxcli-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0d523f00a47f863f928e31a373fb0a4c067b5fbcfcf77308c547e766033a6f23
MD5 3326853890ed6dc85db0734686097f79
BLAKE2b-256 528adb86acb4d373cc709bdc1fd7841f5d4aaaaeaaef0eb552183b2220ee7c27

See more details on using hashes here.

File details

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

File metadata

  • Download URL: foxcli-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 553353714846a0faa1cd931d10d6bae064124d82c0e709236a8fe6490d84ed59
MD5 4de4f7d3ffdab7b270899f0b459c09bb
BLAKE2b-256 f8dd8a24ca101a289544271b12589323fcc740491de827c6bc552299e02cbc53

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