Instantly start a CLI from a function
Project description
startle
[!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,-vabove) 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.
*argsbut also**kwargsare supported, to parse unknown arguments as well as unknown options (--unk-key unk-val).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file startle-0.0.4.tar.gz.
File metadata
- Download URL: startle-0.0.4.tar.gz
- Upload date:
- Size: 28.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9279f6e3d0123843720ae8dcd56dd032dcab2f9b12c4c2b1e27190f6411b45a0
|
|
| MD5 |
f9aae3790bd9ab77e8a05df0896d1bfd
|
|
| BLAKE2b-256 |
cf2306f75c2294e753e75fe61d6ab9783ad25ce4938a8be5c19da23546ea2fa1
|
Provenance
The following attestation bundles were made for startle-0.0.4.tar.gz:
Publisher:
publish.yml on oir/startle
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
startle-0.0.4.tar.gz -
Subject digest:
9279f6e3d0123843720ae8dcd56dd032dcab2f9b12c4c2b1e27190f6411b45a0 - Sigstore transparency entry: 150690330
- Sigstore integration time:
-
Permalink:
oir/startle@05e8a3bacfc9103d087a30e164914a44bdb2378e -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/oir
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@05e8a3bacfc9103d087a30e164914a44bdb2378e -
Trigger Event:
release
-
Statement type:
File details
Details for the file startle-0.0.4-py3-none-any.whl.
File metadata
- Download URL: startle-0.0.4-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4d813e02ad643bc46a7d4dd85e82e567a55fca75481de93c70c7f09eeecbbf1
|
|
| MD5 |
2b078124edc489c33ae3bc483d01f207
|
|
| BLAKE2b-256 |
998d16713be651445cbf98bdcfc34e0a4bdc91c3b5b90e4c53a31571ae7d06d5
|
Provenance
The following attestation bundles were made for startle-0.0.4-py3-none-any.whl:
Publisher:
publish.yml on oir/startle
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
startle-0.0.4-py3-none-any.whl -
Subject digest:
d4d813e02ad643bc46a7d4dd85e82e567a55fca75481de93c70c7f09eeecbbf1 - Sigstore transparency entry: 150690332
- Sigstore integration time:
-
Permalink:
oir/startle@05e8a3bacfc9103d087a30e164914a44bdb2378e -
Branch / Tag:
refs/tags/v0.0.4 - Owner: https://github.com/oir
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@05e8a3bacfc9103d087a30e164914a44bdb2378e -
Trigger Event:
release
-
Statement type: