Skip to main content

Ultra-light command-line argument grabber for quick scripts

Project description

TinyArgs

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.


✨ Features

  • Parse flags in 1–3 lines instead of 20+
  • Accepts both --key value and --key=value
  • Types, defaults, and required args
  • Simple boolean flags via flag("--debug")
  • Minimal, readable API built on sys.argv
  • Zero dependencies, tiny footprint

📦 Installation

Add tinyargs.py to your project (or install from your own distribution, if you publish it). Then:

from tinyargs import args, get, flag, TinyArgsError

Tip: If you prefer a package layout, place the code in a module (e.g., tinyargs/__init__.py) and install locally with pip install -e ..


🚀 Quick Start

Single value with type, default, and required

# 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 secret

Boolean flag

from tinyargs import flag
verbose = flag("--verbose")  # True if provided, else False

CLI:

python script.py --verbose

Unpack multiple at once

from tinyargs import args

name, age = args(
    "--name", "--age",
    types={"--age": int},
    defaults={"--age": 18}
)

print(name, age)

Run:

python script.py --name Alice --age=21

🧠 API Reference

get(key, type=str, default=None, required=False)

Fetch a single argument.

  • Parameters

    • key (str): e.g., "--host".
    • type (type): caster for the value. Defaults to str. Use int, float, bool, etc.
    • default (Any): returned when the key is not present (unless required=True).
    • required (bool): if True and the key is missing, raises TinyArgsError.
  • Returns: casted value or default.

  • Raises: TinyArgsError if a required key is missing or casting fails.

⚠️ Note on booleans
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.


flag(key)

Shorthand for boolean flags.

  • Returns: True if --key appears on the command line, else False.

args(*keys, types=None, defaults=None, required=None)

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: TinyArgsError if a required key is missing or a cast fails.


⚠️ Error Handling

All errors raise TinyArgsError with a descriptive message. Wrap calls in try/except for robustness.


🤝 Contributing

Contributions welcome!
Open an issue or PR with improvements, bug fixes, or new features.


📜 License

MIT License. See LICENSE for details.

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

tinyargs-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

tinyargs-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file tinyargs-0.1.1.tar.gz.

File metadata

  • Download URL: tinyargs-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for tinyargs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 192cedf1a881c0fc8d760bdb63cafdf34b9ae9603960d3203fad73565350b78e
MD5 84e0c9abf0263d7a56fbd7e7395e622c
BLAKE2b-256 52f043952530d8914285b029e41b7d84010c2fa5722bde0f96e0d099a7a1f8d5

See more details on using hashes here.

File details

Details for the file tinyargs-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tinyargs-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for tinyargs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77ba5e76fcfa5105adb073f80b1728f6584dd33ba076e2476af29034edfc4036
MD5 78553a340f3fb022f97352fc2e7aa22c
BLAKE2b-256 8dd1d5f687d9c2b44b4d3139a49b5e4dd358772ea8624586282a8a67cb6848b0

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