Skip to content
myon-bioinformaticsPublic

About

A single-file, standard-library-only Python toolkit for reusable CLI argument definitions, validation, command construction, execution, and JSON/text output, backed by practical CLI tests.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

cli_args

📘 利用ガイド / Usage guide (GitHub Pages)

cli_args icon

Icon · Favicon

A single-file, standard-library-only Python toolkit for reusable CLI argument definitions, validation, command construction, execution, and JSON/text output, backed by practical CLI tests.

Python 3.11+ (declared in pyproject.toml). Vendor it by copying cli_args.py. The module runs on the standard library alone and does no argument parsing or command execution at import time. Packaging for pip is out of scope for the initial version. ✅ 3.12 confirmed by SHA comparison / 🧪 built and tested on the newest 3.x / ⭐ recommended: the newest 3.x.

Loading a vendored copy

Load vendored single-file modules (cli_args.py, fleet_check.py, …) by path, under a name unique to your repository. Do not write from vendor import … or from vendor.cli_args import …: a top-level vendor package collides when several repositories run in one process (Aoi runs its children together in one pytest), and sibling-import guards reject it. This is the one pattern the fleet uses; it is standard library only and works on Python 3.9+:

import importlib.util
import sys
from pathlib import Path


def load_vendored(name, relpath):
    """Load a vendored file by path under a repo-unique name (cli_args README: Loading a vendored copy)."""
    if name not in sys.modules:
        spec = importlib.util.spec_from_file_location(name, Path(__file__).resolve().parents[1] / relpath)
        module = importlib.util.module_from_spec(spec)
        sys.modules[name] = module  # before exec: dataclasses and pickling look the module up here
        spec.loader.exec_module(module)
    return sys.modules[name]


fleet = load_vendored("myrepo_fleet_check", "vendor/fleet_check.py")
run_cli = load_vendored("myrepo_cli_args", "vendor/cli_args.py").run_cli

Name each module <repo>_<module> (lowercase repository name). parents[1] is the repository root for a file in tests/; use parents[0] at the root, parents[2] one level deeper. Keep the vendored file where vendor.lock.json says it is (for example vendor/cli_args.py, or vendor/cli_args/cli_args.py) and pass that path. Reusing the same name returns the same module, so Command and other classes stay identical across test files. Put the helper and the loads after the file's imports, so no # noqa: E402 is needed.

Reusing argument definitions

argparse's choices / required / nargs / action / type are used as-is. Abbreviated option names are rejected by default.

import argparse
from cli_args import Argument, parse_args

args = parse_args([
    Argument(("--mode",), {"choices": ("read", "convert"), "required": True}),
    Argument(("--enabled",), {"action": argparse.BooleanOptionalAction}),
    Argument(("files",), {"nargs": "+"}),
], ["--mode", "read", "--no-enabled", "a.html", "b.html"])

🤔 Typos get a hint by default (exit stays 2, parsing never changes): --fomrat → did you mean '--format'?, jsno → maybe you meant 'json'?, instal → maybe you meant 'install'? — on every Python 3.11+, with 3.15's own argparse wording. make_parser(suggest_unknown=False) turns it off (details).

For finer control, call add_argument() or add_mutually_exclusive_group() directly on the parser returned by make_parser(). Argument.names, an explicit parse_args(..., argv=...), and require_when(..., required=...) all take an ordered, finite sequence; a single string or implicit expansion of a set/dict/generator is rejected. require_when(parser, args, when=..., required=["output"]) rejects, via parser.error(), any listed item whose value is None when the condition holds. False and 0 count as given values.

TopOntoLogy's snapshots and TaxOntOy's graph inputs pair a label with a path. Use named_path with argparse's append action to collect them in order, including repeated labels. It splits on the first =, strips surrounding whitespace from both fields, and returns a name and a Path. The caller decides which labels are valid and when to open the file.

from cli_args import int_range, make_parser, named_path

parser = make_parser()
parser.add_argument("--graph", action="append", type=named_path, metavar="NAME=PATH")
parser.add_argument("--max-files", type=int_range(minimum=1))
args = parser.parse_args(["--graph", "audit=資料/a=b.json", "--max-files", "3"])
# args.graph == [("audit", Path("資料/a=b.json"))], args.max_files == 3

int_range(minimum=..., maximum=...) accepts either or both bounds, inclusive. Use a minimum of 1 for a positive count, as in gh_identity's --max-files, or 0 for a byte limit that allows empty output.

add_output_arguments(parser) adds --format text/json and --output; add_output_arguments(parser, json_flag=True) also accepts --json as sugar for --format json, for the many existing callers that grew a boolean flag first. add_execution_arguments(parser) adds --dry-run / --timeout / --cwd. Pick only the sets you need and avoid name clashes on the caller side.

add_selection_arguments(parser) adds repeatable glob selection (--case / --exclude / --list by default; rename them to the caller's noun), the pattern measured in the markdown converter tools. select_keys(keys, patterns, exclude) applies it deterministically: a pattern matches the whole key or the part after the first :, input order is preserved, and duplicate keys or an empty selection raise ValueError instead of silently running everything or nothing.

Agent-facing failure contract, shared across the repositories (measured in docs/cli-argument-patterns.md): EXIT_OK / EXIT_FAILURE / EXIT_USAGE / EXIT_PARTIAL are 0 / 1 / 2 / 3; requested_format(argv) reads the last --format before -- from raw argv; write_error(message, error="usage", exit_code=EXIT_USAGE, format="json") writes one JSON record (ok, error, message, exit_code) or one error: code: message line to stderr and returns the exit code; make_parser(json_errors=True) makes argparse usage errors use that record when --format json was requested. scripts/survey_cli_surfaces.py CLONES_DIR re-measures the org-wide option vocabulary with exactly these helpers.

Building commands

from cli_args import Command, run_command, write_output

command = (Command(("git",))
    .option("-C", "/path/to/repository")
    .positional("status")
    .flag("--short"))
result = run_command(command, timeout=10)
write_output(result, format="json")
Method Behavior
flag(name, enabled) Added only when the bool is True
option(name, value) Adds the name and value. None is omitted; an empty string is kept
option(..., attached=True) Adds --name=value. Use when the target supports it
repeated(name, values) Repeats, as in -H value1 -H value2. values must be a multi-token collection
multiple(name, values) Lists values, as in --files a b. values must be a multi-token collection
positional(*values) Adds positional arguments in the given order
passthrough(values, separator=False) Appends argv as-is. Prepends -- only when separator=True. values must be a multi-token collection

The meaning of each command and its valid combinations are defined by the caller. Pass arguments as strings or text paths, and convert numbers to strings explicitly. No shell expansion, quote interpretation, or string splitting is done. The collection arguments of repeated() / multiple() / passthrough() accept only ordered, finite Sequences (list/tuple, etc.). A single str / PathLike, an unordered set/dict, or a one-shot generator is rejected with TypeError, so that argv order and reproducibility are part of the API contract. To add a single token, use option() / positional().

pytest_command(paths, quiet=True, tb="short", summary=True, extra=[...]) is an optional preset that builds python -m pytest -q --tb short -ra .... paths and extra also accept only ordered, finite Sequences; a single str / PathLike, set/dict, or generator is rejected. pytest is not imported by the module and is never auto-installed. This repository's own tests run with uv run --locked pytest. pytest_command only builds the target pytest CLI; it does not call uv.

For tests, run_cli(script, *args) is the thin counterpart: it assembles python script args... with Command (the interpreter defaults to sys.executable; pass python= to override, python_options=("-S",) for interpreter flags such as a stdlib-only run, and encoding= for the codec used for stdin text and the captured output (UTF-8 by default, replacement on invalid bytes); python_options must be an ordered, finite Sequence, and a single str / PathLike, an unordered set/dict, or a generator is rejected with TypeError, as with node_flags) and returns a CliResult, a CommandResult whose .json() parses the stdout payload and .error() parses the stderr error object. A stream that is not JSON raises ValueError naming the stream and the exit code, so a failure says which command produced the text. Prefer it over hand-rolled subprocess.run([sys.executable, ...]) in test code; keep Command + run_command for non-Python targets.

GitHub links (assembled, never written down)

from cli_args import github_links, ls_remote, remote_repository

repository = remote_repository()                     # "OWNER/NAME" of origin: https, ssh, git@host: or a proxy URL alike
github_links("myon-bioinformatics/pysaiyanweboo")    # repository · git (git+https://…, what pip / uv install) · pages · portfolio
github_links(repository, ref="main", path="README.md")["raw"]   # + blob / raw for a path
ls_remote("myon-bioinformatics/cli_args")            # the HEAD commit, or None (missing, offline, no git); never prompts

Every link is built from OWNER/NAME the same way (OWNER.github.io is the Pages root of its own repository; GitHub ignores case, so a module name can stand for its repository). Only ls_remote touches the network, once, through git.

Running Node.js from argv

node_command(target, *args, node=None, node_flags=()) builds [node, *node_flags, target, *args]. run_node(...) is a thin wrapper over run_command() and keeps the existing execution contract (timeout / max_output_bytes / env / dry_run, etc.). dry_run rejects non-bool values with TypeError; a string is never treated as false and executed. With dry_run=True, neither the node --version probe nor a child process runs; only the argv[0] token following the lookup order is built (the last fallback is the literal node). is_node_target(path) checks for .js / .cjs / .mjs. node_or_command([]) raises ValueError, as Command(()) does.

Node lookup order: the node= argument, --node, and CLI_ARGS_NODE are explicit and do not fall back on failure. After those, the NODE environment variable and shutil.which('node') are tried. Windows .cmd / .bat wrappers are rejected; the executable found is checked once with node --version within 5 seconds and must be Node 26 or later (the probe is skipped under dry_run). Not found, too old, or timed out raises NodeNotFoundError (a FileNotFoundError subclass, message cli_args: node …). The child's exit code is passed through in returncode. shell=True is never used. engines.node in package.json is also >=26.

To opt in from a CLI, add_node_arguments(parser) adds --node PATH and a repeatable --node-flag (use the --node-flag=--import=x form when the value starts with -); call node_or_command(argv, node=args.node, node_flags=args.node_flags) right before building the argv to run. It wraps the argv with Node only when the first token is .js / .cjs / .mjs, and otherwise returns Command(argv) unchanged. Existing argv semantics are unchanged unless you use this helper.

from cli_args import node_command, run_node, node_or_command

result = run_node("tools/probe.cjs", "--", "-flag", "", "日本 語", timeout=30)
command = node_or_command(["playwright.cjs", "test", "--list"], node_flags=["--no-warnings"])

Calling cli_args.py from Node.js

There is no per-tool cli_args.cjs wrapper: Node 26+ runs cli_args.py directly through the parent repository myon-bioinformatics's shared launcher vendor/py_launch.cjs (pinned in vendor.lock.json). py_launch.cjs rejects Node older than 26, never launches Node from Node, and never uses shell: true. Use maxInputBytes (CLI_STDIN_MAX_BYTES, 4 MiB) and responseMaxBytes (96 MiB; maxBuffer adds only the shared 512-byte HOST_RESPONSE_MARGIN_BYTES margin) when invoking cli_args.py run / render.

Lookup Meaning
opts.python Explicit (no fallback on failure)
CLI_ARGS_PYTHON Explicit environment variable (--env-var CLI_ARGS_PYTHON / envVar)
PYTHON → python3 → python (py -3 on Windows) Automatic lookup
printf '%s\n' '{"argv":["git","status","--short"]}' | node vendor/py_launch.cjs --env-var CLI_ARGS_PYTHON -- cli_args.py run
printf '%s\n' '{"title":"日本語"}' | node vendor/py_launch.cjs --env-var CLI_ARGS_PYTHON -- cli_args.py render --format json
node vendor/py_launch.cjs --env-var CLI_ARGS_PYTHON --call trim_output -- cli_args.py $'a\nb\nc\nd' 1 1
const path = require("node:path");
const {run, call} = require("./vendor/py_launch.cjs");

const script = path.join(__dirname, "cli_args.py");
const py = {envVar: "CLI_ARGS_PYTHON", noSite: true};

const result = JSON.parse(
  run(script, ["run"], {
    ...py,
    input: `${JSON.stringify({argv: ["git", "status", "--short"], max_output: 4 * 1024 * 1024, response_max_bytes: 96 * 1024 * 1024})}\n`,
    maxInputBytes: 4 * 1024 * 1024,
    responseMaxBytes: 96 * 1024 * 1024,
  }).stdout.toString("utf8"),
);
console.log(result.returncode, result.stdout, result.stdout_b64);

const rendered = call(script, "render_output", {args: [{title: "日本語"}], kwargs: {format: "json"}}, py);

Python checks the UTF-8 length of the JSON it actually serialized against response_max_bytes (run) or --response-max-bytes (render) before writing to stdout. Exceeding it exits 1 (response exceeds response_max_bytes on stderr). Invalid JSON or unknown fields make Python exit 2. run JSON always includes output_limited (false is not omitted).

The vendored launcher is byte-for-byte from parent commit de63762d8485e4d398a651715d34ebf4a04c23f2 (parent PR #296, including --call / call() UTF-8 stdin decoding). noSite: true covers interpreter discovery as well as execution. The shared launcher's opt-in isolated flag also covers discovery; it remains off for this entry. On POSIX, repeated SIGINT/SIGTERM are forwarded until the Python child exits, including when it handles an earlier signal and keeps running. Launcher signal failures still exit 2 in text and JSON diagnostic modes. Node >=26 and the existing runtime CI lanes are unchanged.

Passing stdin explicitly (collected from cross-repo use)

run_command(command, stdin=...) passes any str (encoded to UTF-8) or bytes (unchanged) to the child process once. No trailing newline is added and no shell is used. None means no input.

from cli_args import Command, run_command
import sys

# JSON stdin worker such as Flutter's. Pass the generated JSON string as-is.
result = run_command(
    Command((sys.executable, "-S", "worker.py")),
    stdin='{"operation":"inspect","values":["日本語"]}',
    timeout=5,
)
assert result.returncode == 0

# Git NUL-safe pathspec. Sends names with spaces or a leading - exactly.
git = Command(("git", "add", "--pathspec-from-file=-", "--pathspec-file-nul"))
result = run_command(git, stdin=b"-leading name.txt\0", timeout=10)

run_command() handles only synchronous, finite input; file objects and iterators are not accepted as stdin. Streaming/bidirectional batch protocols and long-running resident processes are not supported. Dry-run validates type and UTF-8 encoding but never prints the input, so secrets are not leaked. The existing UTF-8 replacement policy for stdout/stderr and the exit/timeout contract are unchanged.

Cross-repo examples: flutter_navigation_basic's stdin JSON, BrowserTestKit's stdin-based child-process tests, and external CLI wrappers such as gh_identity. Argument parsing and domain-specific rules stay in each project. Git NUL pathspecs and finite git cat-file --batch-command --buffer -Z calls are verified against real Git. Interactive, long-running bidirectional sessions are not supported.

Output protection and byte limits

write_output(..., overwrite=False) is an opt-in that never overwrites an existing path. For an existing file, symlink, or broken symlink it raises FileExistsError without deleting the target. The content is rendered first, saved to a temporary file in the destination directory, and then published with an exclusive hard link. The default overwrite=True keeps the previous behavior. On filesystems without hard-link support it fails safe. Making a new path visible is atomic, but crash durability, fsync, protection against third-party changes after publishing, and automatic creation of missing directories are not guaranteed.

run_command(..., max_output_bytes=N) caps capture at the combined raw byte count of stdout/stderr and stops the child process when exceeded. Output kept in memory totals at most N bytes, and CommandResult.output_limited=True with returncode=None distinguishes it from a normal success. The default None is the previous unlimited mode, and the existing JSON shape is kept when omitted. When the limit is hit, the display may be cut in the middle of a UTF-8 character; decoding stays errors="replace" as before. Only the launched child is monitored; stopping the whole descendant tree is not guaranteed.

result = run_command(Command(("git", "status")), max_output_bytes=65536, timeout=10)
if result.output_limited:
    raise RuntimeError("command output exceeded limit")
write_output({"ok": True}, format="json", output="result.json", overwrite=False)

examples/run_cli.py exposes --max-output-bytes N, --no-overwrite, --stdin TEXT / --stdin-file PATH (mutually exclusive), and --env KEY=VALUE (repeatable). Exceeding the output limit exits 125, a timeout exits 124 as before, and failing to create the file or read the stdin file exits 1. CLI options go before --; the external command argv goes after it.

Handing long output to the reader (explicit omissions, pytest deciding lines)

trim_output(text, head=40, tail=40) keeps the first head lines and the last tail lines, with a single ... N line(s) omitted ... line in between. Output cut with plain head/tail looks complete even when lines are missing, so the omission is made explicit for the reader (human or agent). Output that fits is returned unchanged.

trim_output(text, bounded_memory=True) is an opt-in (Python only) that retains just the head/tail line spans instead of splitting every line; the default behavior and speed are unchanged. The input string itself is still held, so this is not a streaming API. See the measurement and limits.

pytest_summary(output) returns only the lines in captured pytest output that decide the result: the final N failed, M passed in Xs line (summary/counts), -ra FAILED/ERROR lines (failed/errors), pytest-cov's TOTAL line and the Required test coverage verdict (coverage), and deciding_lines with the first E line of each failure and any INTERNALERROR (60 lines by default; excess sets truncated).

result = run_command(pytest_command(["tests"], quiet=True, summary=True), timeout=600)
digest = pytest_summary(result.stdout)
print(digest["summary"], digest["failed"], digest["coverage"]["required_met"])
print(trim_output(result.stdout, head=20, tail=40))

Execution and output

python -S examples/run_cli.py --format json --dry-run -- git status --short
python -S examples/run_cli.py --format json --output result.json --timeout 30 -- uv run --locked pytest -q --tb=short -ra

cli_args.py itself also has a machine-facing entry point that hosts such as Node can call with python -S cli_args.py (argv is not parsed at import time).

Subcommand Input Output
run JSON request on stdin CommandResult as JSON on stdout
render --format text|json Any JSON value on stdin Result of render_output on stdout

The run JSON object takes argv (required) and optionally timeout / cwd / env / input / max_output / dry_run. stdin is UTF-8 up to 4 MiB (exceeding it, non-UTF-8, or JSON containing NaN/Infinity exits 2). Unknown keys, type mismatches, and invalid JSON also exit 2 (one line on stderr). When the request is processed it exits 0, and the child's exit code goes into the JSON returncode. A launch failure such as a missing executable or missing cwd exits 1. shell is not accepted.

Separately from the library's render_output, run JSON always carries Base64 fields stdout_b64 / stderr_b64 with the captured raw bytes. output_limited is always returned as true / false (CLI only; the library's render_output JSON still omits it when false). stdout / stderr are decoded as UTF-8 with invalid bytes replaced by U+FFFD. Use *_b64 to handle binary or non-UTF-8 data without loss. --version prints the __version__ constant in cli_args.py (the same value in a standalone vendored copy).

printf '%s' '{"argv":["python","-c","print(1)"],"dry_run":true}' | python -S cli_args.py run
printf '%s' '{"argv":["日本語"],"returncode":0,"stdout":"","stderr":"","executed":true}' | python -S cli_args.py render --format json

Wrapper arguments and target arguments are separated by a required --. Target options such as --format can go after the boundary.

  • Result: argv, returncode, stdout, stderr, executed, timed_out.
  • A non-zero exit is returned as a result too. A launch failure raises OSError; a timeout returns timed_out=True / returncode=None with partial output.
  • Dry-run does not execute and returns executed=False / returncode=None, distinct from a successful run.
  • The JSON is an envelope for the execution result. Stdout from Git, curl, etc. is not turned into domain data automatically.
  • The text result is stdout as-is. Stdout/stderr captured by run_command() is only UTF-8 decoded; newlines emitted by the child (\n / \r\n) are not normalized. Library users should check stderr and the exit status. The example CLI also prints stderr, reports non-zero exits, and exits 124 on timeout and 127 when the executable is missing. If writing --output fails (missing parent directory, insufficient permissions, etc.), it prints failed to write output: ... to stderr without a traceback and exits 1.
  • Saving is UTF-8, always LF (\n), and overwrites existing files. Newline bytes are LF on every OS, including Windows. Parent directories are not created. The file is written only after JSON conversion succeeds, but the save is not atomic.
  • Unlimited execution (the default) keeps output in memory. Limit large output by setting max_output_bytes explicitly. Lossless binary output, interactive stdin, streaming, and stopping the whole descendant tree are out of scope.
  • env replaces the whole environment. To inherit, merge with os.environ on the caller side or use the build_env(extra, inherit=True) helper. build_env() takes a Mapping or a sequence of "KEY=VALUE" strings and merges environment variables safely.

The standard library as a third CLI surface

Before adding an external CLI, check whether Python's standard library (python -m / python -c / plain import) can meet the same goal safely. The default order of choice for AI coding is stdlib → existing host CLI → Docker/extra dependencies.

Typical examples are json.tool, zipfile, tarfile, http.server, platform, compileall, and tempfile. PR #1 (merged) runs the main examples in CI so this is not just a cheat sheet. Standard modules get no custom aliases; showing the real python -m module name keeps the Python version and provenance unambiguous.

For details and "standard features to check before installing an external tool", see docs/stdlib-command-surface.md.

"Unglamorous but automation-friendly arguments" from classic to modern CLIs are collected in docs/cli-argument-patterns.md, with links to official docs, real/construct-only evidence levels, and design principles such as NUL-safe, fail-fast, and machine-readable in one place.

Use standard library functions and types first

Below python -m there are stdlib functions and types to use before writing a custom helper or adding a small dependency. In particular, check print / pprint / textwrap / shlex / pathlib / tempfile / collections / itertools / functools / contextlib / difflib / csv / configparser / hashlib / urllib.parse first as candidates for "small things people often write themselves".

Use the real API instead of adding custom aliases. Write a wrapper only when there is an extra contract such as bounds, encoding policy, domain validation, provenance, or an error taxonomy.

See docs/stdlib-function-surface.md for details.

Building staged execution in plain Python

The shell bucket brigade often seen as VAR=... → command → echo $? → if → exit 0/1/2 → next command can be structured, when Python is available, with ordinary for / if / variables / CommandResult.returncode / os.environ / print / sys.exit.

Control such as "if step 1 fails, do not run steps 2–4" needs no new workflow DSL; run structured argvs in order with normal Python control flow. A child process cannot change its parent's environment variables, so state between steps is passed as Python values, values parsed from stdout, temporary files, or an explicit env mapping.

See docs/stdlib-orchestration-surface.md for details and examples.

Start with the smallest observation path

For read-only data retrieval, do not start with a custom API/MCP; pick the smallest surface that meets the needed contract, in the order local/static data → raw HTTP document → generic local parser → official API → MCP. If the full HTML is enough, use the full text fetched with urllib.request or existing curl as-is.

Move up to an API when you need auth, a stable schema, pagination, server-side filtering/search, mutation, or computation; move up to MCP when the agent protocol itself (tool discovery, typed invocation, sessions/resources, etc.) adds value.

When Python is the orchestration layer, prefer stdlib logging for timestamped, leveled diagnostics over a custom shell echo convention.

See docs/minimal-transport-surface.md for details.

Keeping failures as evidence

PR #1 (merged) does not throw away failures found during exploration as noise. In a negative evidence / intentional failures lane kept apart from normal CI, child pytest runs are made to fail on purpose, and raw JUnit, stdout, stderr, and receipts are saved.

Pass / assertion failure / setup error / strict xfail / strict XPASS are currently recorded under explicit testcase names as past patterns, and an outer validator confirms that the child pytest really exits non-zero and that the JUnit classification matches. That raw JUnit is not mixed into the normal 7-report collector; it goes to a dedicated pinned shared failure-identity consumer.

This mechanism is not for "treating unknown failures as expected to turn CI green". Adding or changing an expected failure means updating the testcase name and expected classification explicitly, so the evidence diff can be reviewed. See tests/failure_lab/README.md for details.

Verification scope

Evidence levels are kept distinct.

  • Host-real: real commands run on a GitHub Linux runner.
  • Container-real: real commands run inside a test-only image built in CI.
  • System-service-real: run against a real backing service/state. This term is used only when a service is really running under journal/systemd, etc.
  • Construct-only/stub: the exact argv/transport is verified, but the real tool, auth, or service is not measured.
Target Evidence Contract
Python / argparse Host-real (3.15), output parity (3.11–3.14) Argument parsing, typo hint, invalid numeric input, JSON rendering and stdlib-only python -S
subprocess Host-real Spaces, Japanese text, empty strings, leading hyphens, shell-like strings, cwd, env, non-zero exit, timeout, invalid UTF-8
pytest Host-real Multiple files, -q, --tb short, -k "a or b", explicit --, failure results
Git Host-real + Container-real + Construct-only Measured: temp repo, -C, add --, leading-hyphen/space/Unicode paths, ls-files -z, status --porcelain=v2 -z --branch, for-each-ref --format=...%00 --sort=.... --pathspec-from-file=- --pathspec-file-nul and finite cat-file --batch-command --buffer -Z calls are verified with NUL I/O against real Git. Resident/bidirectional sessions are not measured
curl Host-real + Container-real Local HTTP, repeated headers, empty option values, Unicode URL/output paths, HTTP failure, plus measured --fail-with-body exiting 22 while keeping the body, --fail-early stopping multiple transfers, and --write-out %{http_code}
ls/coreutils Host-real + Container-real ls with multiple flags, explicit --, leading-hyphen/space/Unicode filenames, plus measured NUL-delimited record handling with GNU sort -z --stable
journalctl Container-real + Construct-only Real binary/version run in the container. Exact argv for repeated -u / since / until / -n / no-pager recorded as past patterns
journal/systemd backing service Not measured The default CI image does not start systemd PID 1 or a journal service, so System-service-real is not claimed
Docker Host-real → Container-real Exact transport of --, empty strings, leading hyphens, spaces, and Unicode argv from the host's cli_args.Command into a locally built image
Docker Compose Host-real Runs repeated -f + config --services referencing only the locally built image. No registry/network needed
Node / npm Host-real Argv building, lookup, and version probe for node_command / run_node / node_or_command (Node 26+, explicit-choice failures, .cmd rejection) and real .cjs / .mjs runs (--, empty strings, leading hyphens, Unicode paths, timeout and exit code). CI has dedicated Node 26 and latest lanes on Ubuntu/Windows/macOS. Transport of npm run ... -- <args> is also measured with a temp package + local JS probe
Playwright CLI shape Construct-only Records the direct Node CLI form actually used in the org as a past pattern, avoiding greedy --project value and verifying --project=value + --grep + spec order. Real browser/parser evidence lives in BrowserTestKit
gh Host-real binary + Construct-only/stub gh --version uses the real binary. Exact argv for API subcommands, repeated headers, --jq, --paginate --slurp, --cache 1h, and typed -F / raw -f fields recorded as past patterns. Live auth/network not measured
Flutter / Dart Construct-only Representative argv for flutter build web -t ... --dart-define=... and dart run ... --output ... recorded as past patterns. Bootstrap/build is measured in the Flutter repo
FFmpeg Container-real FFmpeg is installed explicitly in the test-only image and processes a tiny lavfi source with -f lavfi -i ... -f null -. Multiple options/values and the special - output are measured
uv Construct-only Nested argv uv run --no-project --python ... --with ... python -m pytest used by the org's Python matrix recorded as a past pattern

For AI coding, prefer existing machine-readable / NUL-safe / fail-fast / structured-field CLI contracts over parsing human-oriented output. "Unglamorous but sturdy" arguments such as Git porcelain v2, -z/%00, curl diagnostics, and gh API pagination are actively added to regression evidence. Bidirectional interactive batch protocols are not implemented, and finite stdin input working is not stretched into a guarantee for resident processing.

The Python 3.15 host lane runs independently of Docker and systemd. The Docker integration lane builds the test-only image in tests/docker/Dockerfile (python:3.14.0-slim-bookworm) and verifies Git, curl, ls, journalctl and FFmpeg inside it with the same cli_args.py and pytest harness. The image sets CLI_ARGS_CONTAINER_REAL=1, uses the latest uv and resolves test dependencies from the same uv.lock. CI saves the container-real suite as its own JUnit report.

If Git/curl/ls, etc. are missing from the host, host-real testcases skip with a reason. The Docker integration lane installs the needed binaries explicitly into the image, so skips are not allowed there. No external website or live auth is needed.

The default branch sends 11 reports to the shared collector. They cover Python 3.15 on Ubuntu, six Node bridge combinations, Docker transport and Compose, the container CLI suite and two Python 3.15 core runs on Windows and macOS. The published reports distinguish argument construction, stub execution, real CLI execution and service integration.

uv sync --locked
python -S -c "import cli_args"
uv run --locked pytest -q -ra --tb=short --junitxml=test-results/pytest-local.xml
node --test tests/cli_args.test.cjs

pyproject.toml and uv.lock are the source of truth for the development/test environment. The module running under python -S and external tools being present are separate conditions. pytest runs with uv run --locked pytest. The development dependency pytest has a lower bound only (pytest>=8), with no exact pin or upper bound. node --test remains the Node test runner.

This records "which usage patterns were reproduced and verified", not speed benchmarks. Failure cases are added to regression tests, and shared features grow from real examples. HTML fetching and structure extraction belong in separate tools, which are expected to use this file's argument, execution, and output features.

Runtime and development Python policy

The standard-library runtime, including standalone and vendored copies, supports Python 3.11+. Python 3.15 runs the main assertions and the python -S stdlib suite. The parity workflow compares deterministic parser and renderer outputs on Python 3.11–3.14 to Python 3.15 using temporary files and SHA-256. The main pytest lane selects mapped test files on pull requests and runs the full suite for source, shared setup, unknown changes and pushes to main.

uv sync --locked --python 3.15
uv run --locked pytest
node --test tests/cli_args.test.cjs
python -S -m unittest discover -s tests -p test_runtime_compatibility.py -v

pyproject.toml is the canonical Python requirement and development dependency definition: project.requires-python = ">=3.11" applies to source runtime support as well as development. Legacy requirements files remain for downstream callers. A copied .py file does not read project metadata; consumers must select Python 3.11+ themselves. This policy does not update consumers' vendored files or interpreter settings.

The Node entry uses a pinned shared launcher that still probes for Python 3.9+; that discovery threshold is not this tool's support policy. Set CLI_ARGS_PYTHON or opts.python to a Python 3.11+ executable for supported use. The launcher and its vendor pin are unchanged.

CI pins setup-uv v10.4.1 by full commit SHA, gets Python from it (actions/setup-python is not used), and uses the latest uv. Setup actions using Node 24 require runner 2.327.1 or newer (GitHub-hosted runners are used).

CI exercises the stdlib and dependency-bearing portability tests on Python 3.15 on Windows and macOS; raw JUnit reports from both operating systems are required by the identity collector.

Focused test discovery

See standard-library discovery commands for validated python -S -m unittest discover recipes, filename/name filtering, and the boundary with the full pytest suite.

uv: install the single-file command

Runtime dependencies remain empty. From this repository root:

uv tool install .
cli_args --help

Or use it once without registering a persistent command:

uvx --from 'git+https://github.com/myon-bioinformatics/cli_args.git@main' cli_args --help

For Python imports in another project, use uv add /absolute/path/to/cli_args and uv run python -c 'import cli_args'. uv tool install uses a separate environment. Use a reviewed full commit SHA instead of main when reproducibility is required. Only cli_args.py is installed; development dependency groups are not runtime requirements.

About

A single-file, standard-library-only Python toolkit for reusable CLI argument definitions, validation, command construction, execution, and JSON/text output, backed by practical CLI tests.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages