Skip to main content

Instantly start a CLI from a function

Project description

startle

tests Coverage Status

[!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 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)

❯ python wc.py --help:

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

❯ python wc.py wc.py -k char --verbose:



startle is inspired by Typer, and Fire, but 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.
  • 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).
  • 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 function.
  • *args but also **kwargs are supported, to parse unknown arguments as well as unknown options (--unk-key unk-val).

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.5.tar.gz (27.9 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.5-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: startle-0.0.5.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for startle-0.0.5.tar.gz
Algorithm Hash digest
SHA256 c2b93cef0314208395cee00af60131b868d43acfa7f0794579100ea05d1d4eda
MD5 3064d642a69543a894fcd950a16f876a
BLAKE2b-256 74e8cd9232fea88293286df542874c4d4db6fc86b396826dc3812a82bc180ffd

See more details on using hashes here.

Provenance

The following attestation bundles were made for startle-0.0.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: startle-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for startle-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 204d3ae24c364df83e0bf673556edc5f06bb99a64bdab192be67ce8b739e9600
MD5 aa64d1d1db1b4b35c132cbb41f000db4
BLAKE2b-256 b2305433c66c0c1a9f40272a6453a1d11512fd56b2508b2d2b2f36d83618b3bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for startle-0.0.5-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