A micro command-line argument helper for tiny Python scripts.
TinyArgs gives you the 80/20 of CLI parsing in a few functions—no boilerplate, no dependencies. It’s perfect for quick utilities where argparse or click feel like overkill.
- Parse flags in 1–3 lines instead of 20+
- Accepts both
--key valueand--key=value - Types, defaults, and required args
- Simple boolean flags via
flag("--debug") - Minimal, readable API built on
sys.argv - Zero dependencies, tiny footprint
Add tinyargs.py to your project (or install from your own distribution, if you publish it). Then:
from tinyargs import args, get, flag, TinyArgsErrorTip: If you prefer a package layout, place the code in a module (e.g.,
tinyargs/__init__.py) and install locally withpip install -e ..
# script.py
from tinyargs import get, TinyArgsError
try:
port = get("--port", type=int, default=8000)
host = get("--host", default="127.0.0.1")
api_key = get("--api-key", required=True) # raises TinyArgsError if missing
print(host, port, api_key)
except TinyArgsError as e:
print(f"Error: {e}")
raise SystemExit(2)Run it:
python script.py --host 0.0.0.0 --port=8080 --api-key secretfrom tinyargs import flag
verbose = flag("--verbose") # True if provided, else FalseCLI:
python script.py --verbosefrom tinyargs import args
name, age = args(
"--name", "--age",
types={"--age": int},
defaults={"--age": 18}
)
print(name, age)Run:
python script.py --name Alice --age=21Fetch a single argument.
-
Parameters
key(str): e.g.,"--host".type(type): caster for the value. Defaults tostr. Useint,float,bool, etc.default(Any): returned when the key is not present (unlessrequired=True).required(bool): ifTrueand the key is missing, raisesTinyArgsError.
-
Returns: casted value or default.
-
Raises:
TinyArgsErrorif a required key is missing or casting fails.
If you set type=bool and pass the flag without a value (e.g., --flag), TinyArgs returns True.
If you pass --flag false, Python’s bool("false") is True—avoid that style.
Shorthand for boolean flags.
- Returns:
Trueif--keyappears on the command line, elseFalse.
Fetch multiple arguments in one call and unpack them.
-
Parameters
*keys(str): e.g.,"--name", "--age".types(dict): optional per-key type mapping, e.g.{"--age": int}.defaults(dict): default values per key.required(list[str]): keys that must appear.
-
Returns: tuple of parsed values in the same order as keys.
-
Raises:
TinyArgsErrorif a required key is missing or a cast fails.
All errors raise TinyArgsError with a descriptive message. Wrap calls in try/except for robustness.
Contributions welcome!
Open an issue or PR with improvements, bug fixes, or new features.
MIT License. See LICENSE for details.