flodl_cli/lib.rs
1//! flodl-cli — library side of the `fdl` binary.
2//!
3//! This crate is both a library and a binary. The binary (`fdl`) is the
4//! user-facing driver; the library exposes the pieces that other crates
5//! (e.g. a flodl-based training binary) need to integrate with the
6//! `fdl` ecosystem:
7//!
8//! - [`FdlArgs`] — derive macro + trait for argv parsing and schema emission
9//! - [`parse_or_schema`] — intercepts `--fdl-schema` / `--help` and dispatches
10//! - [`Schema`], [`OptionSpec`], [`ArgSpec`] — the canonical schema shape
11//!
12//! # Example
13//!
14//! ```no_run
15//! use flodl_cli::{FdlArgs, parse_or_schema};
16//!
17//! /// My training binary.
18//! #[derive(FdlArgs, Debug)]
19//! struct Cli {
20//! /// Model to run.
21//! #[option(short = 'm', default = "all")]
22//! model: String,
23//!
24//! /// Write a report instead of training.
25//! #[option(default = "runs/report.md")]
26//! report: Option<String>,
27//! }
28//!
29//! fn main() {
30//! let cli: Cli = parse_or_schema();
31//! // ... use cli.model, cli.report, etc.
32//! }
33//! ```
34
35// Self-alias: the `#[derive(FdlArgs)]` macro emits `::flodl_cli::...`
36// paths. That resolves automatically when the derive is used from a
37// downstream crate (or from `main.rs`, which sees the lib as an external
38// dep), but inside the library itself the compiler only knows the
39// crate by its `crate`-root name. The alias makes `::flodl_cli::...`
40// resolve to ourselves so `builtins.rs` can derive the same trait.
41extern crate self as flodl_cli;
42
43// Internal modules — shared by lib consumers and the fdl binary.
44
45/// Structured API reference for flodl itself (`fdl api-ref`), used by
46/// AI porting tools and as a machine-readable surface index.
47pub mod api_ref;
48
49/// Argv parsing primitives and the [`FdlArgsTrait`] contract that
50/// `#[derive(FdlArgs)]` implements.
51pub mod args;
52
53/// Built-in `fdl` sub-commands (setup, install, completions, schema,
54/// config, libtorch, diagnose, init, skill, ...).
55pub mod builtins;
56
57/// Shell completion script generation and per-project completion
58/// enrichment driven by cached schemas.
59pub mod completions;
60
61/// `fdl.yml` manifest loading, validation, and resolved-command types.
62pub mod config;
63
64/// Cluster-mode env preparation. fdl-cli sets `FLODL_INTERNAL_FULL_CLUSTER_JSON`
65/// + `FLODL_INTERNAL_FDL_CMD` + `FDL_ENV` on its process env so the user binary
66/// inherits them and detects launcher role via
67/// `flodl::distributed::launcher::dispatch`. Fan-out, log fan-in, and
68/// ClusterController all live on the flodl side.
69/// Entry point [`cluster::prepare_cluster_env`]; recursion guard via
70/// [`cluster::should_dispatch`].
71pub mod cluster;
72
73/// `--gpus` flag parsing + single-host cluster envelope synthesis (loopback,
74/// one host, N ranks). Used when `--gpus` is set on a cluster-aware command
75/// and no `cluster:` block is configured in YAML.
76pub mod gpus;
77
78/// Cross-cutting context passed to sub-command handlers (resolved config,
79/// verbosity, overlay selection, working directory, ...).
80pub mod context;
81
82/// Top-level command dispatch: routing argv to built-ins vs. manifest
83/// entries, resolving the three command kinds (run / path / preset).
84pub mod dispatch;
85
86/// Hardware and compatibility diagnostics (`fdl diagnose`).
87pub mod diagnose;
88
89/// Cluster readiness probe (`fdl probe`): GPU + libtorch arch +
90/// shared-data path + NCCL discovery. Pre-training gate; the
91/// foundation for `fdl deploy` and the transparent launcher dispatch.
92pub mod probe;
93
94/// Live run status (`fdl status`): fetches the controller's
95/// `state.json` (membership + lifecycle phase) and pretty-prints it.
96pub mod status;
97
98/// Project scaffolding (`fdl init`): generates Dockerfile, `fdl.yml`,
99/// training template, `.gitignore`.
100pub mod init;
101
102/// Ecosystem-crate scaffolding (`fdl add <target>`): drops a
103/// configured sub-project inside a flodl project for hands-on
104/// discovery. Currently supports `flodl-hf`.
105pub mod add;
106
107/// libtorch variant management (download, build, list, activate, remove,
108/// info) used by both `fdl libtorch` and the standalone-manager flow.
109pub mod libtorch;
110
111/// NCCL source builds (`fdl nccl build`). Drops a standalone libnccl.so
112/// into `libtorch/nccl/builds/<ver>-<archs>/` for the LD_PRELOAD bridge
113/// pattern used by cross-host heterogeneous-arch clusters.
114pub mod nccl;
115
116/// Environment overlay loader (`@env`, `--env`, `FDL_ENV`) with
117/// per-field origin annotations for `fdl config show`.
118pub mod overlay;
119
120/// Runtime: invoking resolved commands, streaming their output, and
121/// mapping exit codes through `fdl`.
122pub mod run;
123
124/// Pre-flight build for cluster commands. Builds the target binary
125/// locally (in Docker on the controller) for each remote host's
126/// libtorch ABI before fan-out, delivering it via the shared
127/// project-root mount so the remote can exec it directly without a
128/// cargo / rustc toolchain.
129pub mod prebuild;
130
131/// `fdl schema` sub-command: discover every cache under the project,
132/// report fresh / stale / orphan states, and clear or refresh on
133/// demand. The [`Schema`] type itself lives in [`config`].
134pub mod schema;
135
136/// `--fdl-schema` binary contract and per-command cache mechanics.
137/// Caches live at `<cmd_dir>/.fdl/schema-cache/<cmd>.json` with
138/// mtime + binary hash metadata for staleness detection.
139pub mod schema_cache;
140
141/// First-run and reconfiguration wizard (`fdl setup`).
142pub mod setup;
143
144/// Daily update check against crates.io for `flodl-cli` and
145/// project-pinned `flodl` / `flodl-hf`. Opt out via
146/// `FDL_NO_UPDATE_CHECK=1` or the global config file.
147pub mod update_check;
148
149/// AI-skill bundles: packaging and installing the `/port` skill and
150/// similar assistant integrations.
151pub mod skill;
152
153/// ANSI styling primitives and the `--ansi` / `--no-ansi` / `NO_COLOR`
154/// resolution chain used by the help renderer and CLI output.
155pub mod style;
156
157/// Miscellaneous helpers shared by the other modules.
158pub mod util;
159
160/// Print a red-prefixed `error: <formatted>` line to stderr.
161///
162/// Takes standard `format!` arguments. Coloring follows the `--ansi` /
163/// `--no-ansi` / `NO_COLOR` / `FORCE_COLOR` chain via
164/// [`style::color_enabled`], so pipes stay plain automatically.
165#[macro_export]
166macro_rules! cli_error {
167 ($($arg:tt)*) => {
168 $crate::style::print_cli_error(format_args!($($arg)*))
169 };
170}
171
172// ── Public API for binary authors ──────────────────────────────────────
173
174/// Parse argv into `T`, intercepting `--fdl-schema` and `--help`.
175pub use args::parse_or_schema;
176
177/// Slice-based variant of [`parse_or_schema`] — parses from an explicit
178/// `&[String]` rather than `std::env::args()`. Used by the `fdl` driver to
179/// dispatch per-sub-command arg tails.
180pub use args::parse_or_schema_from;
181
182/// Trait implemented by `#[derive(FdlArgs)]` structs. Binary authors do
183/// not typically implement this manually — the derive emits it.
184pub use args::FdlArgsTrait;
185
186/// Derive macro for `FdlArgs`. Generates argv parsing, `--fdl-schema`
187/// emission, and `--help` rendering from a single struct definition.
188pub use flodl_cli_macros::FdlArgs;
189
190/// Schema types — mirror the JSON shape emitted by `--fdl-schema` and
191/// consumed by the fdl driver.
192pub use config::{ArgSpec, OptionSpec, Schema};
193
194/// Re-exported dependencies the derive macro needs to reference by path.
195/// Users should not depend on these directly — they are only stable as
196/// an implementation detail of the derive.
197#[doc(hidden)]
198pub use serde_json;