Skip to main content

The fastest, simplest way to create CLIs in Python

Project description

The fastest, simplest way to create a CLI in Python

cli-buddy provides an ergonomic syntax for creating command-line interfaces in Python.

With cli-buddy, 👇 this code snippet is all it takes to build a CLI.

from cli_buddy import CLI

class MyCLI(CLI):
    arg: str

Under the hood, cli-buddy is a pydantic-flavored wrapper around the built-in argparse module.

Requirements

  • Python 3.11+

Install

pip install cli-buddy

Usage

Use the CLI class to define your CLI's arguments and options.

This code snippet...

# main.py
from cli_buddy import CLI

class TerminalTimer(CLI):
    seconds: int
    # boolean fields are automatically converted to flags
    show_time: bool

... will produce the following CLI:

$ python main.py --help
usage: main.py [-h] seconds

positional arguments:
  seconds

options:
  -h, --help  show this help message and exit
  --show_time

A CLI class alone won't run anything, obviously. We need to define our behavior first.

Define your CLI's behavior

Either use your CLI instance however you like...

# main.py
...

cli = TerminalTimer()

for _ in range(cli.seconds):
    print(_)
    time.sleep(1)

... or just put your logic within the __call__() method of your CLI class...

class TerminalTimer(CLI):
    seconds: int
    show_time: bool

    def __call__(self):
        for _ in range(self.seconds):
            print(_)
            time.sleep(1)

TerminalTimer()  # That's it!

Give your arguments default values to convert arguments to flags.

The CLI class will automatically convert arguments to flags if:

  • they are assigned a default value,
  • or if the field is a boolean.

For example, this...

class TerminalTimer(CLI):
    seconds: int = 1
    show_time: bool

... will produce the following CLI:

$ python main.py --help
usage: main.py [-h] [--seconds SECONDS] [--show_time]

options:
  -h, --help         show this help message and exit
  --seconds SECONDS  (default: 1)
  --show_time

Use the Argument class to further configure your CLI's arguments.

The Argument function takes the same arguments as the built-in argparse.ArgumentParser.add_argument function.

For example, this...

from cli_buddy import CLI, Argument


class TerminalTimer(CLI):
    seconds: int = Argument(help="Number of seconds to wait")
    show_time: bool = Argument("-t", help="Show seconds in terminal", action="store_true")

... will produce the following CLI:

$ python main.py --help
usage: main.py [-h] -t seconds

positional arguments:
  seconds          Number of seconds to wait

options:
  -h, --help       show this help message and exit
  -t, --show_time  Show seconds in terminal

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

cli_buddy-0.2.6.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

cli_buddy-0.2.6-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file cli_buddy-0.2.6.tar.gz.

File metadata

  • Download URL: cli_buddy-0.2.6.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Darwin/23.5.0

File hashes

Hashes for cli_buddy-0.2.6.tar.gz
Algorithm Hash digest
SHA256 a61882b15862a1783120bcd2d97444dec95408c7b63010e6b8d4efcae6f48979
MD5 a3e49f3a5bc41f3d1decf6b0586ee5e3
BLAKE2b-256 d45742adad3dedc1393f1a21c590d45570009ff6207f338e014183f4090b6d32

See more details on using hashes here.

File details

Details for the file cli_buddy-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: cli_buddy-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Darwin/23.5.0

File hashes

Hashes for cli_buddy-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 461fcf9b51b038fa84839588a7b54bc6bd782e66a31a8ebe8854b4495c7d6286
MD5 ac8022c1a80478e8fb51cf0c8d41939a
BLAKE2b-256 fd22b24f698cc9b952695509484062f986b87cec77c4a9292e2c8dda37480ea4

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