Skip to main content

Instantly start a CLI from functions, classes, or TypedDicts

Project description

Give your code a start. ⚡👀

tests Coveralls Docs
Supported Python Versions PyPI Version Ruff Pyright

[!WARNING]
Startle is alpha and should be considered unstable as its interface is fluid 😅, consider pinning to a version.

Startle lets you transform a python function (or functions) into a command line entry point, e.g:

wc.py:

from pathlib import Path
from typing import Literal

from startle import start


def word_count(
    fname: Path, /, kind: Literal["word", "char"] = "word", *, verbose: bool = False
) -> None:
    """
    Count the number of words or characters in a file.

    Args:
        fname: The file to count.
        kind: Whether to count words or characters.
        verbose: Whether to print additional info.
    """

    text = open(fname).read()
    count = len(text.split()) if kind == "word" else len(text)

    print(f"{count} {kind}s in {fname}" if verbose else count)


start(word_count)

When you invoke start(), it will construct an argparser (based on type hints and docstring), parse the arguments, and invoke word_count.

You can also invoke start() with a list of functions instead of a single function. In this case, functions are made available as commands with their own arguments and options in your CLI. See here.


Startle also allows you to transform a class (possibly a dataclass) into a command line parser:

import random
from dataclasses import dataclass
from typing import Literal

from startle import parse


@dataclass
class Config:
    """
    Configuration for the dice program.

    Attributes:
        sides: The number of sides on the dice.
        count: The number of dice to throw.
        kind: Whether to throw a single die or a pair of dice.
    """

    sides: int = 6
    count: int = 1
    kind: Literal["single", "pair"] = "single"


def throw_dice(cfg: Config) -> None:
    """
    Throw the dice according to the configuration.
    """
    if cfg.kind == "single":
        for _ in range(cfg.count):
            print(random.randint(1, cfg.sides))
    else:
        for _ in range(cfg.count):
            print(random.randint(1, cfg.sides), random.randint(1, cfg.sides))


if __name__ == "__main__":
    cfg = parse(Config, brief="A program to throw dice.")
    throw_dice(cfg)

Then dice.py can be executed like:



Startle is inspired by Typer, Fire, and HFArgumentParser, but aims to be non-intrusive, to have stronger type support, and to have saner defaults. Thus, some decisions are done differently:

  • Use of positional-only or keyword-only argument separators (/, *, see PEP 570, 3102) are naturally translated into positional arguments or options. See above example (wc.py).
  • Like Typer and unlike Fire, type hints strictly determine how the individual arguments are parsed and typed.
  • Short forms (e.g. -k, -v above) are automatically provided based on the initial of the argument.
  • Variable length arguments are more intuitively handled. You can use --things a b c (in addition to --things=a --things=b --things=c). See example.
  • Like Typer and unlike Fire, help is simply printed and not displayed in pager mode by default, so you can keep referring to it as you type your command.
  • Like Fire and unlike Typer, docstrings determine the description of each argument in the help text, instead of having to individually add extra type annotations. This allows for a very non-intrusive design, you can adopt (or un-adopt) Startle with no changes to your functions.
  • *args but also **kwargs are supported, to parse unknown arguments as well as unknown options (--unk-key unk-val). See example.

See all examples, or the documentation for more.

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

startle-0.0.24.tar.gz (217.6 kB view details)

Uploaded Source

Built Distribution

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

startle-0.0.24-py3-none-any.whl (42.9 kB view details)

Uploaded Python 3

File details

Details for the file startle-0.0.24.tar.gz.

File metadata

  • Download URL: startle-0.0.24.tar.gz
  • Upload date:
  • Size: 217.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for startle-0.0.24.tar.gz
Algorithm Hash digest
SHA256 ab37469ee6440e51800628d10d1c5addd4e135c54b785892075db0d808d1a0f6
MD5 f0570e70134302b713bcb359e5f6e798
BLAKE2b-256 456512f770560cafb789f067b13b7a1b2f5e4706d43d848acc9983292334c000

See more details on using hashes here.

Provenance

The following attestation bundles were made for startle-0.0.24.tar.gz:

Publisher: publish.yml on oir/startle

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file startle-0.0.24-py3-none-any.whl.

File metadata

  • Download URL: startle-0.0.24-py3-none-any.whl
  • Upload date:
  • Size: 42.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for startle-0.0.24-py3-none-any.whl
Algorithm Hash digest
SHA256 8a1eb5011c183ea097a136590f70d9c752450890098a28c14ea090d57526373b
MD5 ace509eb346ff9ff7f86e02bca798d61
BLAKE2b-256 189e3c02c94529e45255f0bd191750f1e8974763e5f9238ab2e3c43318e7a926

See more details on using hashes here.

Provenance

The following attestation bundles were made for startle-0.0.24-py3-none-any.whl:

Publisher: publish.yml on oir/startle

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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