degenbot_cli/lib.rs
1//! `degenbot-cli` - the degenbot console binary (ADR-051 D2).
2//!
3//! One argv facade, one command model. This crate owns exactly the things
4//! [`degenbot_cli_core`] cannot own by charter:
5//!
6//! - **argv declaration** ([`argv`]): the clap v4 derive tree for the whole
7//! command vocabulary, and the argv -> [`Command`](degenbot_cli_core::Command)
8//! mapping. See `argv`'s module docs for where clap's shape differs from the
9//! retired Python click tree.
10//! - **rendering** ([`render`]): typed [`CommandReport`](degenbot_cli_core::CommandReport)
11//! lines to stdout, typed [`CliError`](degenbot_cli_core::CliError) to stderr
12//! with its `ExitCode`.
13//! - **interaction** ([`prompt`]): the stdin/stdout [`Prompter`](degenbot_cli_core::Prompter)
14//! the arms ask; the prompt *policy* stays declared data in cli-core (D4).
15//! - **sinks** ([`sinks`]): the tracing registry + console fmt layer, booted in
16//! the same order as the Python driver (typed config first, then the
17//! subscriber), and **progress** ([`progress`]): the `indicatif` bar, which
18//! exists only here (D9).
19//! - **SIGINT ownership** ([`signal`], D7): first Ctrl+C feeds cli-core's
20//! [`CancelHandle`](degenbot_cli_core::CancelHandle); a second aborts.
21//!
22//! cli-core stays clap-free and indicatif-free (asserted by
23//! `just check-cli-core-purity`); this crate stays free of every domain-engine
24//! dependency (asserted by `just check-cli-shell-purity`).
25
26pub mod argv;
27pub mod progress;
28pub mod prompt;
29pub mod render;
30pub mod signal;
31pub mod sinks;
32
33pub use argv::{Cli, Commands};
34
35use std::io::Write as _;
36
37use degenbot_cli_core::CancelHandle;
38use degenbot_config::ProcessEnv;
39
40/// The banner `--version` prints: the workspace version (ADR-009 lockstep).
41pub const VERSION_LINE: &str = env!("CARGO_PKG_VERSION");
42
43/// Parse `args` (argv WITHOUT the program name), boot the sinks, resolve the
44/// argv into a [`Command`](degenbot_cli_core::Command), run it and render the
45/// result. This is the ONE console composition root: the `degenbot` binary and
46/// the Python `cli_main` passthrough both land here, so their sequences cannot
47/// drift apart.
48///
49/// Returns the process exit code and never exits the process, so an embedded
50/// host can drive the console directly: clap owns `--help`/`--version`/usage
51/// errors (its own exit codes apply), a refused typed config is `2` (the same
52/// refusal exit the Python module init uses), and every command outcome maps
53/// through cli-core's single `CliError -> ExitCode` site.
54///
55/// There is deliberately no host parameter here. The native and Python hosts
56/// are not abstracted behind a flag: their genuine difference lives upstream
57/// of this function — the Python `#[pymodule]` init has already installed the
58/// typed config and the driver's log forwarder by the time this runs, while
59/// the native path gets both from `sinks::boot()` below — and `boot`'s
60/// first-wins installs keep that difference in place. A switch would hide the
61/// difference it cannot actually remove.
62#[must_use]
63pub fn run_args<T>(args: &[T]) -> i32
64where
65 T: Into<std::ffi::OsString> + Clone,
66{
67 let parsed = <Cli as clap::Parser>::try_parse_from(
68 std::iter::once(std::ffi::OsString::from("degenbot"))
69 .chain(args.iter().map(|arg| arg.clone().into())),
70 );
71 let cli = match parsed {
72 Ok(cli) => cli,
73 Err(error) => {
74 // clap's `Error::exit` is print-then-exit-code with the print
75 // result discarded; a broken pipe on `--help` is swallowed the
76 // same way here.
77 let code = error.exit_code();
78 let _ = error.print();
79 return code;
80 }
81 };
82
83 let env = ProcessEnv;
84 if cli.command.is_none() {
85 argv::write_missing_subcommand_error();
86 return 2;
87 }
88
89 // Sinks before anything that emits, exactly like the Python module init.
90 let _telemetry = match sinks::boot() {
91 Ok(telemetry) => telemetry,
92 Err(refusal) => {
93 let _ = writeln!(std::io::stderr().lock(), "{refusal}");
94 return 2;
95 }
96 };
97
98 let ctx = argv::context(&cli, &env);
99 let command = match argv::resolve_with_env(&cli, &env) {
100 Ok(command) => command,
101 Err(error) => return render::error(&error),
102 };
103
104 let prompter = prompt::ConsolePrompter::new();
105 let cancel = CancelHandle::new();
106 let _sigint = signal::install(cancel.clone());
107
108 let outcome = degenbot_cli_core::run_with_cancel(&command, &ctx, &prompter, &cancel);
109 render::outcome(&outcome)
110}
111
112/// The binary entry: collect the process argv (minus the program name) and
113/// delegate to the console composition root.
114#[must_use]
115pub fn run() -> i32 {
116 let args: Vec<std::ffi::OsString> = std::env::args_os().skip(1).collect();
117 run_args(&args)
118}