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`; staleness is
138/// mtime-based against the command's config AND, for a `compile: true`
139/// command, the sources its schema is compiled from.
140pub mod schema_cache;
141
142/// First-run and reconfiguration wizard (`fdl setup`).
143pub mod setup;
144
145/// Daily update check against crates.io for `flodl-cli` and
146/// project-pinned `flodl` / `flodl-hf`. Opt out via
147/// `FDL_NO_UPDATE_CHECK=1` or the global config file.
148pub mod update_check;
149
150/// AI-skill bundles: packaging and installing the `/port` skill and
151/// similar assistant integrations.
152pub mod skill;
153
154/// ANSI styling primitives and the `--ansi` / `--no-ansi` / `NO_COLOR`
155/// resolution chain used by the help renderer and CLI output.
156pub mod style;
157
158/// Miscellaneous helpers shared by the other modules.
159pub mod util;
160
161/// Print a red-prefixed `error: <formatted>` line to stderr.
162///
163/// Takes standard `format!` arguments. Coloring follows the `--ansi` /
164/// `--no-ansi` / `NO_COLOR` / `FORCE_COLOR` chain via
165/// [`style::color_enabled`], so pipes stay plain automatically.
166#[macro_export]
167macro_rules! cli_error {
168 ($($arg:tt)*) => {
169 $crate::style::print_cli_error(format_args!($($arg)*))
170 };
171}
172
173// ── Public API for binary authors ──────────────────────────────────────
174
175/// Parse argv into `T`, intercepting `--fdl-schema` and `--help`.
176pub use args::parse_or_schema;
177
178/// Slice-based variant of [`parse_or_schema`] — parses from an explicit
179/// `&[String]` rather than `std::env::args()`. Used by the `fdl` driver to
180/// dispatch per-sub-command arg tails.
181pub use args::parse_or_schema_from;
182
183/// Trait implemented by `#[derive(FdlArgs)]` structs. Binary authors do
184/// not typically implement this manually — the derive emits it.
185pub use args::FdlArgsTrait;
186
187/// Derive macro for `FdlArgs`. Generates argv parsing, `--fdl-schema`
188/// emission, and `--help` rendering from a single struct definition.
189pub use flodl_cli_macros::FdlArgs;
190
191/// Schema types — mirror the JSON shape emitted by `--fdl-schema` and
192/// consumed by the fdl driver.
193pub use config::{ArgSpec, OptionSpec, Schema};
194
195/// Re-exported dependencies the derive macro needs to reference by path.
196/// Users should not depend on these directly — they are only stable as
197/// an implementation detail of the derive.
198#[doc(hidden)]
199pub use serde_json;