darwin-cli
A declaration-driven CLI engine: parameters are not passive slots waiting for a value. They can compute values, infer other parameters, declare new parameters at runtime, and enforce relationships with each other — all from a config file if you want.
- Zero third-party dependencies in the core — only the Python standard library
- One DSL for everything: static params, Actions, dynamic params, dependency ordering, validation, and cross-parameter constraints
- Config-file driven:
CliApp.from_config()readscli_args/groups/constraintsfrom JSON, a dict, or any loader callback - Declarative groups: nest parameter groups and declare relations in config; they compile into ordinary constraints at load time
- Optional darwin-config adapter for keeping params in a layered config file (see section 6)
- A small, stable API:
CliApp,Param,ActionResult,CliResult,register_action, plus a handful of helpers
pip install darwin-cli
Python >= 3.10.
1. At a glance: what an app looks like
The same parameter declaration in three equivalent forms.
fetch.json — everything in a config file:
{
"prog": "fetch.py",
"help": "Fetch market data",
"cli_args": {
"type": {
"type": "str",
"required": true,
"action": "load_dynamic_params",
"depends_on": ["source"],
"help": "Data type"
},
"source": { "type": "str", "action": "guess_source" },
"date": { "type": "str" },
"year": { "type": "str" }
},
"constraints": [
{ "rule": "mutually_exclusive", "params": ["date", "year"] }
]
}
Python dict — callables allowed:
from darwin_cli import CliApp
app = CliApp.from_config({
"cli_args": {
"type": {"required": True, "action": load_dynamic_params, "depends_on": ["source"]},
"source": {"action": guess_source},
"date": {},
"year": {},
},
"constraints": [{"rule": "mutually_exclusive", "params": ["date", "year"]}],
})
Param objects — full control:
from darwin_cli import CliApp, Param
app = CliApp(
params=[
Param(name="type", required=True, action=load_dynamic_params, depends_on=["source"]),
Param(name="source", action=guess_source),
Param(name="date"),
Param(name="year"),
],
constraints=[{"rule": "mutually_exclusive", "params": ["date", "year"]}],
prog="fetch.py",
)
What the engine does for you:
@register_action("guess_source")
def guess_source(value, ctx):
if value is not None: # user supplied --source: do not interfere
return {}
return {"source": "tushare"} # otherwise infer it
@register_action("load_dynamic_params")
def load_dynamic_params(value, ctx):
return ActionResult(params=[ # declare params that did not exist at startup
Param(name="symbol", type=ValidationRule(pattern=r"^\d{6}\.(SH|SZ)$"), required=True),
Param(name="fields", default="close"),
])
result = app.run(["--type", "stock_daily", "--symbol", "600000.SH"])
print(result.outcome, result.data)
# ok {'type': 'stock_daily', 'source': 'tushare', 'symbol': '600000.SH', 'fields': 'close'}
--source was inferred, symbol / fields did not exist when parsing started,
and --date / --year cannot both be used — none of that required an
if/else in main().
examples/contains 9 runnable scenarios with a demo script. Runpython examples/demo.pyfrom the repository root.
2. Syntax quick reference
Param fields
| Field | Type | Meaning |
|---|---|---|
name |
str |
CLI name (--name); also the key in cli_args |
type |
"str" | "int" | "float" | "bool" or ValidationRule |
A rule object implies str |
required |
bool |
Must come from the CLI or an Action — not from a default |
default |
value or Callable(ctx) |
Static value, or a function evaluated during execution |
help |
str or Callable(ctx) |
Static text, or help generated from the current context |
action |
name or Callable(value, ctx) |
Registered Action name, or a function directly |
validation |
ValidationRule |
Extra rules when type is a string |
depends_on |
list[str] |
Parameters that must be processed before this one |
mutually_exclusive / together / at_least_one / exclusive_with / requires |
str (optional) |
One-to-one inline relation naming a single other parameter; compiled at load time (see section 4.9) |
ValidationRule fields
| Field | Applies to | Check |
|---|---|---|
choices |
any | value must be in the list |
min / max |
numbers | inclusive numeric bounds |
min_length / max_length |
strings | inclusive length bounds |
pattern |
strings | re.match (prefix match — anchor with $ for exact match) |
custom |
any | name of a validator registered via custom_validators=... |
Type coercion happens before rules: str casts with str(), int / float
with their constructors, and bool accepts true/1/yes/on/t and
false/0/no/off/f (lowercase strings only). None is returned unchanged for
optional params — required checks live in the executor, not the validator.
Constraint rules (config top-level constraints)
| Rule | Config | Meaning |
|---|---|---|
mutually_exclusive |
{"rule":"mutually_exclusive","params":["date","year"]} |
at most one |
together |
{"rule":"together","params":["user","token"]} |
all or none |
requires |
{"rule":"requires","param":"type","requires":"source"} |
A present implies B present |
exclusive_with |
{"rule":"exclusive_with","param":"date","group":["start_date","end_date"]} |
A excludes a whole group |
at_least_one |
{"rule":"at_least_one","params":["source","file"]} |
at least one must be present |
Optional keys on any rule: "message" (override the default English error) and
"include_generated": true (also count Action-produced values, not just CLI
args). By default only explicitly supplied CLI arguments are checked, so an
Action expanding date → start_date/end_date never triggers a false conflict.
ActionResult and CliResult
| Type | Fields | Purpose |
|---|---|---|
ActionResult |
values: dict, params: list[Param] |
backfill values and/or declare new parameters |
CliResult |
success, data, error, exit_code, outcome |
result of run(); outcome is ok / help / error |
ParamInfo.from_source is the decision key throughout: "cli" (explicit user
argument), "default" (static/callable default), or "action" (produced by an
Action).
3. Core APIs in practice
CliApp(params, ..., constraints=..., registry=...)
The facade. params accepts list[Param] or a dict of declarations.
Other constructor arguments:
app = CliApp(
params=[...],
prog="fetch.py", # program name in help
help="Fetch market data", # also accepts description=
max_iterations=100, # safety net for the deferred loop
action_modules=["myapp.actions"], # import modules that register Actions
custom_validators={"even": lambda v: ...},
available_sources=["tushare", "yahoo"], # listed in global help
constraints=[...],
groups={...}, # optional group declarations (section 4.9)
registry=ActionRegistry(), # omit for the global registry
)
Useful attributes after a run: app.params (static + dynamic),
app.static_params, app.dynamic_params, app.get_param(name).
CliApp.from_config(config, prog=None, help=None, loader=None, mount=None, **kwargs)
app = CliApp.from_config("config/fetch.json") # JSON, stdlib only
app = CliApp.from_config({"cli_args": {...}}) # dict
app = CliApp.from_config("fetcher.cli", loader=my_loader) # custom source
app = CliApp.from_config("settings.json", mount="fetcher.cli") # nested section
mount walks a dotted path into the loaded dict before extracting cli_args
(or params), groups, constraints, prog and help. loader is any callable that
turns the string into a dict; without one, the string is treated as a JSON path.
run(args=None) and run_or_exit(args=None)
result = app.run(["--type", "stock_daily"])
if result.outcome == "ok":
handle(result.data)
elif result.outcome == "help":
pass # help text already printed; exit_code == 0
else:
log(result.error) # exit_code == 1
data = app.run_or_exit() # reads sys.argv[1:]; exits on any non-ok outcome
run() never exits the process — it returns a CliResult, which makes the
app easy to embed and test. CliResult.ok(data), CliResult.help() and
CliResult.fail(error, exit_code=1) are the factory helpers.
register_action(name, *, registry=None) and ActionRegistry
from darwin_cli import ActionRegistry, register_action
@register_action("guess_source") # global registry
def guess_source(value, ctx): ...
reg = ActionRegistry() # isolated registry for one app
@register_action("guess_source", registry=reg)
def isolated(value, ctx): ...
app = CliApp(params=[...], registry=reg)
has_action / get_action / list_actions / clear_actions all accept the
same optional registry. ACTION_REGISTRY and GLOBAL_ACTIONS both point to
the process-wide registry. load_actions_from_module(path) imports a module so
its decorators run; validate_before(fn) wraps an Action with a pre-check.
ActionResult — the return value of an Action
@register_action("expand_year")
def expand_year(value, ctx):
if value is None:
return {}
return ActionResult(
values={"start_date": f"{value}-01-01", "end_date": f"{value}-12-31"},
params=[Param(name="start_date"), Param(name="end_date")],
)
Returning a plain dict is shorthand for ActionResult(values=that_dict).
values fills or overwrites values; params gives the new names a formal
definition so they take part in validation, help, and further processing.
Validation helpers
from darwin_cli import Validator, create_validator, parse_args_with_meta, build_parser
validator = create_validator(custom_validators={"even": lambda v: v if v % 2 == 0 else None})
validator.validate_one(Param(name="n", type="int"), "4") # -> 4
meta = parse_args_with_meta(params, ["--type=stock_daily", "--count", "3"])
meta["type"].from_source # 'cli'
meta["count"].from_source # 'cli'
parser = build_parser(params, prog="fetch.py")
parse_params_from_config, parse_constraints, compile_constraints,
resolve_groups, validate_param_definitions, merge_params,
params_to_dict, get_param_by_name, check_constraint and
check_constraints are also exported for building custom front-ends.
Exceptions
ActionError, ConflictError (subclass), ConstraintError (subclass, with
.violations), CycleDetected (subclass), HelpRequested,
MaxIterationsExceeded, RequiredError. All error messages are English;
CliApp.run() converts them into a CliResult and prints contextual help.
4. Features in detail
4.1 Exact source tracking
--key value, --key=value and bare --flag are all recorded by custom
argparse.Action subclasses, not by scanning raw argv. So
from_source == "cli" is reliable — the earlier O(n²) argv scan used to
mislabel --key=value. This matters for three decisions: required checks,
constraint checks, and whether an Action may overwrite a value.
4.2 Dynamic parameters
An Action can return ActionResult(params=[...]). The executor registers the
new definitions in the middle of a run. A value supplied on the command line for
a not-yet-declared name is captured as an unknown argument, then formally
validated once the definition appears. This is the capability argparse cannot
offer: its parameter set is frozen before parsing starts.
r = app.run(["--type", "stock_daily", "--symbol", "600000.SH"])
# tushare branch declares symbol+fields; yahoo branch declares symbol+interval
4.3 depends_on and real cycle detection
When a parameter's dependencies are not yet processed, it is put back into the
queue behind them. Declaration order does not matter. Cycles are found by a DFS
over the depends_on graph (three-color marking) and reported with the actual
path, e.g. Dependency cycle detected: a -> b -> c -> a → CycleDetected.
max_iterations remains as a safety net for runaway chains.
4.4 Value expansion and chaining
Values written by an Action are marked dirty and re-queued, so A producing B
producing C converges automatically. Write Actions idempotently
(if value is not None: return {}) because a parameter may be revisited.
4.5 Validation
Coercion → ValidationRule checks → custom validator, in that order. A rule
object used as type implies str coercion first. pattern uses re.match,
so "^\\d{6}\\.SH$" matches exactly while "^\\d{6}\\.SH" also allows a
suffix. Custom validators are registered with
CliApp(custom_validators={...}) and referenced by name from ValidationRule.custom.
4.6 Cross-parameter constraints
Relationships between parameters are declared in config rather than written as
Actions. Constraints run after the deferred loop (so all generated values
exist) but before required checks, and by default inspect only cli-sourced
values:
r = app.run(["--date", "2026-01-01", "--year", "2026"])
# CliResult(outcome='error', error="Parameters ['date', 'year'] are mutually exclusive; ...")
Use "include_generated": true on a rule when Action-produced values must
count. Complex, business-specific conditions (lookups, multi-step computation)
still belong in Actions; the two mechanisms coexist.
4.7 Context-aware help
--help is moved to the end of the queue. With --help alone the engine
shows global help immediately; combined with other arguments it first runs
inference and dynamic-param discovery, then shows scenario help:
$ python fetch.py --help
Usage: fetch.py [global options]
Global options:
--type <str> [required]
--source <str>
--help Show this help message
$ python fetch.py --type stock_daily --help
Usage: fetch.py --source tushare [options]
Source: tushare
Options:
--type <str> [required]
(dynamic) --symbol <str> [required] pattern: ^\d{6}\.(SH|SZ)$
--help Show this help message
A help value may be a Callable(ctx); if it raises, the line degrades to
(help generation failed: ...) instead of breaking the whole run. Note that a
help request returns success=False with exit_code=0 — use outcome == "help"
to distinguish it from an error.
4.8 Isolated Action registries
The global registry is convenient but process-wide. Pass registry=ActionRegistry()
to CliApp and register_action(..., registry=reg) to keep multiple apps in one
process from colliding on Action names. Omitting it keeps the classic
@register_action("name") behaviour.
4.9 Groups: declarative relations at scale
@name is a macro for a flat list of parameter names. Groups are resolved at
load time and compiled into ordinary Constraint objects — the runtime has no
concept of a group. Three declaration layers, deliberately kept separate:
| Layer | Where | Expresses |
|---|---|---|
| one-to-one inline | inside cli_args |
a relation between exactly two parameters |
| (a) group-internal | groups with true |
a relation among several params — a group is required |
| (b) group ↔ third party | groups with a list |
a group vs another group and/or a parameter |
top-level constraints |
constraints |
unchanged; keeps custom message / include_generated |
{
"cli_args": {
"date": { "type": "str" },
"start_date": { "type": "str", "together": "end_date" },
"end_date": { "type": "str" },
"year": { "type": "str" },
"window": { "type": "str" }
},
"groups": {
"@sedate": ["start_date", "end_date"],
"@instant": ["date", "window"],
"@date_range": {
"members": ["@sedate", "year"],
"exclusive_with": ["@instant"]
},
"@dates": {
"members": ["@instant", "@date_range"],
"at_least_one": true
}
}
}
Syntax rules:
- Inline relations are one-to-one: the value is a single parameter name
(
"together": "end_date"), never a list and never an@group. Unknown keys, list values, agroupkey or any@reference inside a param definition are load-time errors — a typo can never silently drop a relation. groupsis the single source of truth for membership and group-level relations. A group is a list (pure members) or a dict (membersplus rules).membersmay nest other@groups; a parameter may belong to several groups. Group cycles (including self-cycles), undefined groups and empty groups are reported with the actual path.true= (a): one N-ary rule over the group's own members.- a list = (b): the group's relation to a third party. Targets may mix
@groupnames and parameter names; the compiler expands them, drops the subject itself, and emits one rule per member.exclusive_with/requiresare the natural fit. @expands everywhere:members, relation targets and top-levelconstraints.params/group. After expansion the result is always a flat list of parameter names.at_least_oneis evaluated last: it is a fallback hint, and the engine reports only the first violation.
CliApp(..., groups=...) accepts the same groups dict when building from
Param objects; from_config() reads it from the (optionally mounted) config.
Common design mistake: relating a parent group to its own child is contradictory after expansion — the members end up excluding each other. The library faithfully executes what you declare; how to partition groups and where to place a relation is the config author's decision. Put relations at a level where the two sides are disjoint. Conditions that depend on values, runtime-only parameters or external state still belong in an Action.
5. Execution pipeline and precedence
Executor.run() is a deferred loop, not a one-pass parse.
| # | Phase | What happens |
|---|---|---|
| 1 | Help short-circuit | --help with no other actionable input → global help immediately |
| 2 | Defaults | apply static / callable defaults; source becomes default |
| 3 | Pending queue | every declared parameter is queued |
| 4 | Help goes last | --help is moved to the tail so inference runs first |
| 5 | Deferred loop | pop → dependency check (requeue or raise CycleDetected) → validate → Action → merge ActionResult (new params are queued too) → mark processed |
| 6 | Unknown args | CLI args that never received a definition → ActionError: Unknown parameter: --x |
| 7 | Re-validation | re-check only values touched by Actions |
| 8 | Constraints | evaluate cross-parameter rules (explicit CLI args by default) |
| 9 | Required | value must exist and come from cli or action |
| — | Return | {name: value} wrapped in CliResult |
Value precedence (high → low): an existing CLI or Action-produced value is never overwritten by a later Action → a new Action value → static / callable default. When an Action declares a parameter that already holds a CLI value, the value is kept and the definition is attached.
Gotchas worth knowing
required=Truetogether with a staticdefaultstill fails: defaults are not a valid source for required values ("must come from CLI or an Action").- A plain dict returned by an Action is treated as
values; onlyActionResult(params=...)declares new parameters. patternis a prefix match (re.match); anchor with$.- Bool flags:
default=Truemakes--flagsetFalse, otherwise--flagsetsTrue. CliApp.run()prints help / error help to stdout; capture it if you embed the app in another program.constraintsare checked beforerequired, so a missing required param can also surface as anat_least_oneviolation.
6. Ecosystem integration
darwin-config (optional adapter)
Keep parameter declarations in a JSON / YAML / TOML config managed by
darwin-config. The adapter lives
in darwin_cli.integrations.darwin_config and is not imported by the core:
importing it is what pulls in darwin-config, so darwin-cli itself stays
dependency-free.
from darwin_cli.integrations.darwin_config import from_config, from_file, get_loader
app = from_config("fetcher.cli", prog="fetch.py") # dotted key
app = from_file("config/settings.yaml", mount="fetcher", prog="fetch.py")
app = CliApp.from_config("cli", loader=get_loader("fetcher"), prog="fetch.py")
from_config reads a dotted key via get_config, from_file initialises
darwin-config from a file first, and get_loader returns a loader callback for
CliApp.from_config. Any of these can be combined with mount to select a
sub-tree.
Other consumers
darwin-fetch consumes this library to build data-fetching CLIs. When
migrating from darwin_common.cli, the API names are unchanged — only the
package name differs. New capabilities (constraints, from_config,
outcome, instance registries) require importing darwin_cli.
When not to use it
This is not a general-purpose CLI framework competitor. For a script with a
few fixed options, argparse or click is simpler; for subcommands, shell
completion and coloured help, use click / typer. darwin-cli targets tools
whose parameter set changes with context: swappable data sources, dynamic
parameter discovery, inference and chained dependencies between parameters.
See docs/by-example.md for a scenario-by-scenario comparison.
7. Tests
pip install -e '.[test]'
pytest tests -q
# or: uv run --extra test python -m pytest tests -q
Tests are split into tests/unit/ (one file per module: base, actions, loader,
parser, validator, executor, help, app, constraints, registry, source tracking,
cycle detection, config integration, groups) and tests/integration/ (end-to-end fetch
workflows, complex chains, and test_examples.py, which executes every config
under examples/). All error-message assertions are in English.
Samples in examples/ are covered by integration tests, so they stay
executable documentation.
Publishing a new version: see the release guide.
8. License
MIT
Metadata
Release files for darwin-cli 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| darwin_cli-0.3.0.tar.gz | 30.3 kB | Without provenance | Details for darwin_cli-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| darwin_cli-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for darwin_cli-0.3.0-py3-none-any.whl |
Total release size: 65.0 kB
Release files / darwin_cli-0.3.0.tar.gz
| Download URL | darwin_cli-0.3.0.tar.gz |
|---|---|
| Size | 30.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a9a3444cc46192fb527866ae81ad5578321e680c9592c4f0b1cdcddcfdf80e4f
|
|
BLAKE2b-256 checksum How to use checksums |
356852e3b4ee8e19b30409c9bf9f422acf7943c0744022e881e2d3cd463ad330
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / darwin_cli-0.3.0-py3-none-any.whl
| Download URL | darwin_cli-0.3.0-py3-none-any.whl |
|---|---|
| Size | 34.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bd25c68dcad1fef69a6305f56eafd2e9bddfa74766a008fa0917fac7aa7756f0
|
|
BLAKE2b-256 checksum How to use checksums |
4957f48815d2af10f3fa90a4cc0d0d5a8310f8f83a341a731e07194e416dfdf4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for darwin-cli 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.