1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Clap surface for docsrs-cli.
use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Debug, Clone, Parser)]
#[command(
name = "docsrs-cli",
version,
about = "One-shot CLI for crates.io and docs.rs documentation",
long_about = "One-shot BORN→EXECUTE→DIE CLI for crates.io and docs.rs.
\
Stdout is the data contract (JSON or markdown). Stderr is diagnostics only.
\
JSON is automatic when stdout is not a TTY; force human with --format markdown|text.
\
First Ctrl-C / SIGINT cancels cooperatively (exit 130). Second Ctrl-C within 5s force-exits 130.
\
SIGTERM and SIGHUP cancel as terminate (exit 143). On Windows, Ctrl+Break and console close also terminate (143).
\
Broken pipe on stdout is exit 141 (pipeline-safe; no stack trace).",
after_help = "Examples:
\
docsrs-cli search-crates serde --json
\
docsrs-cli readme tokio --json
\
docsrs-cli get-item clap trait clap::Parser --json
\
docsrs-cli search-in-crate reqwest Client --json
\
docsrs-cli doctor --json
\
docsrs-cli config path --json
\
docsrs-cli config show --json
\
docsrs-cli config init --json
\
docsrs-cli commands --json
\
docsrs-cli completions bash
\
docsrs-cli --dry-run readme serde
\
# Agents: JSON is auto when stdout is not a TTY; force markdown with --format markdown",
disable_help_subcommand = false
)]
/// Top-level clap parser for the `docsrs-cli` binary.
pub struct Cli {
/// Emit JSON envelope on stdout
#[arg(long, global = true)]
pub json: bool,
/// Output format (json is alias of --json)
#[arg(long, global = true, value_enum)]
pub format: Option<OutputFormat>,
/// Wall-clock timeout in seconds
#[arg(long, global = true)]
pub timeout: Option<u64>,
/// Connect timeout in seconds
#[arg(long, global = true)]
pub connect_timeout: Option<u64>,
/// Override User-Agent
#[arg(long, global = true)]
pub user_agent: Option<String>,
/// Force human stderr locale: en or pt-BR (fail-closed; JSON stays English)
#[arg(long, global = true)]
pub lang: Option<String>,
/// Override XDG config directory
#[arg(long, global = true)]
pub config_dir: Option<PathBuf>,
/// Override XDG cache directory for HTTP body cache
#[arg(long, global = true)]
pub cache_dir: Option<PathBuf>,
/// Disable disk cache (always hit network)
#[arg(long, global = true)]
pub no_cache: bool,
/// Disk cache TTL in seconds (default 86400 = 24h)
#[arg(long, global = true)]
pub cache_ttl_secs: Option<u64>,
/// Soft cap on disk cache size in bytes (default 268435456 = 256 MiB; 0 = unlimited)
#[arg(long, global = true)]
pub max_cache_bytes: Option<u64>,
/// Plan URLs without opening network sockets
#[arg(long, global = true)]
pub dry_run: bool,
/// Cap downloaded body size in bytes (hard max 10485760 = 10 MiB; cannot raise above)
#[arg(long, global = true)]
pub max_body_bytes: Option<u64>,
/// Cap emitted payload size in bytes (hard max 2097152 = 2 MiB; cannot raise above)
#[arg(long, global = true)]
pub max_output_bytes: Option<u64>,
/// Minimum delay between requests to the same host (ms)
#[arg(long, global = true)]
pub rate_limit_delay_ms: Option<u64>,
/// Max concurrent CPU parse workers (`0` = auto from CPUs and free RAM)
#[arg(long, global = true)]
pub max_concurrency: Option<u32>,
/// Max HTTP retries for transient errors (after the first attempt)
#[arg(long, global = true)]
pub max_retries: Option<u32>,
/// Base backoff delay in milliseconds for HTTP retries
#[arg(long, global = true)]
pub retry_base_ms: Option<u64>,
/// Maximum single HTTP retry sleep in milliseconds
#[arg(long, global = true)]
pub retry_max_delay_ms: Option<u64>,
/// Disable HTTP retries (incident kill switch / debug)
#[arg(long, global = true)]
pub disable_retry: bool,
/// Increase stderr verbosity
#[arg(short = 'v', long, global = true, action = clap::ArgAction::Count)]
pub verbose: u8,
/// Suppress non-error stderr prose
#[arg(short = 'q', long, global = true)]
pub quiet: bool,
/// Disable ANSI colors on stderr
#[arg(long, global = true)]
pub no_color: bool,
#[command(subcommand)]
/// Subcommand to execute for this invocation.
pub command: Commands,
}
/// Human or machine output format selection.
#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)]
pub enum OutputFormat {
/// Markdown rendered for humans.
Markdown,
/// JSON envelope for agents.
Json,
/// Plain-text rendering (alias of markdown for most commands).
Text,
}
/// Product subcommands exposed on the CLI surface.
#[derive(Debug, Clone, Subcommand)]
pub enum Commands {
/// Search crates on crates.io
SearchCrates {
/// Search keywords (English). Optional when `--page-token` carries a full query string.
#[arg(default_value = "")]
query: String,
/// Results per page (max 100)
#[arg(long, default_value_t = 10)]
per_page: u32,
/// Sort order
#[arg(long, value_enum, default_value_t = SortKind::Relevance)]
sort: SortKind,
/// Page number (1-based). Conflicts with `--page-token`.
#[arg(long, default_value_t = 1, conflicts_with = "page_token")]
page: u32,
/// Opaque pagination token from `meta.next_page` / `meta.prev_page` (full query or seek).
#[arg(long, conflicts_with = "page")]
page_token: Option<String>,
},
/// Fetch crate overview docblock from docs.rs (not git README)
Readme {
/// Crate name on crates.io or a stdlib crate.
crate_name: String,
/// Optional crate version (`latest` when omitted).
#[arg(long)]
crate_version: Option<String>,
},
/// Fetch documentation for a typed item
GetItem {
/// Crate name on crates.io or a stdlib crate.
crate_name: String,
/// Kind: module|struct|trait|enum|union|fn|function|method|type|const|constant|static|macro|attr|attribute|derive
item_type: String,
/// Path with `::` or `/` (e.g. `tokio::runtime::Runtime`, `runtime/Runtime`, `async_trait`)
item_path: String,
/// Optional crate version (`latest` when omitted).
#[arg(long)]
crate_version: Option<String>,
/// On 404, suggest nearby symbols from all.html (extra request).
#[arg(long)]
suggest: bool,
},
/// Search symbols in crate all.html index
SearchInCrate {
/// Crate name on crates.io or a stdlib crate.
crate_name: String,
/// Text filter; empty string lists all classified items
#[arg(default_value = "")]
query: String,
/// Optional crate version (`latest` when omitted).
#[arg(long)]
crate_version: Option<String>,
/// Optional item-kind filter (`struct`, `fn`, `method`, …).
#[arg(long)]
item_type: Option<String>,
/// Maximum hits to emit (clamped to 1000).
#[arg(long, default_value_t = 100)]
limit: u32,
/// Match policy: exact | prefix (default) | substring
#[arg(long, default_value = "prefix")]
r#match: String,
},
/// Print binary version
Version,
/// Validate local TLS/config readiness
Doctor {
/// Probe crates.io / docs.rs over the network (opt-in)
#[arg(long)]
online: bool,
},
/// List the full command tree for agent discovery
Commands,
/// Emit JSON Schema for a command
Schema {
/// Command name whose schema should be printed.
#[arg(long)]
cmd: String,
},
/// Generate shell completions
Completions {
/// Target shell for completion scripts.
#[arg(value_enum)]
shell: Shell,
},
/// Inspect or clear the XDG HTTP disk cache
Cache {
/// Cache maintenance action.
#[command(subcommand)]
action: CacheAction,
},
/// Manage XDG config paths and the optional config.toml (no secrets / no .env)
Config {
/// Config lifecycle action.
#[command(subcommand)]
action: ConfigAction,
},
}
/// Cache maintenance subcommands.
#[derive(Debug, Clone, Subcommand)]
pub enum CacheAction {
/// Delete all cached HTTP bodies under the cache dir
Clear,
/// Report entry count, total bytes, and budget
Stats,
}
/// XDG config lifecycle subcommands (no API keys; product is public HTTP only).
#[derive(Debug, Clone, Subcommand)]
pub enum ConfigAction {
/// Print resolved config/cache directories and which layer won
Path,
/// Print effective runtime configuration (merged defaults + TOML + env + CLI)
Show,
/// Create a default config.toml under the resolved config directory
Init {
/// Overwrite an existing config.toml
#[arg(long)]
force: bool,
},
}
/// crates.io search sort order.
#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)]
pub enum SortKind {
/// Rank by search relevance.
Relevance,
/// Rank by total downloads.
Downloads,
/// Rank by recent downloads.
#[value(name = "recent-downloads")]
RecentDownloads,
/// Rank by recent crate updates.
#[value(name = "recent-updates")]
RecentUpdates,
/// Rank newest crates first.
New,
/// Rank alphabetically by crate name.
Alphabetical,
}
impl SortKind {
/// crates.io API `sort=` query value.
pub fn as_api_str(self) -> &'static str {
match self {
Self::Relevance => "relevance",
Self::Downloads => "downloads",
Self::RecentDownloads => "recent-downloads",
Self::RecentUpdates => "recent-updates",
Self::New => "new",
Self::Alphabetical => "alphabetical",
}
}
}
/// Shells supported by the `completions` subcommand.
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Shell {
/// Bash completions.
Bash,
/// Zsh completions.
Zsh,
/// Fish completions.
Fish,
/// Elvish completions.
Elvish,
/// Canonical CLI value is `power-shell`; `powershell` is accepted as alias.
#[value(name = "power-shell", alias = "powershell")]
PowerShell,
}
impl Shell {
/// Stable CLI / JSON name for this shell.
pub fn as_str(self) -> &'static str {
match self {
Self::Bash => "bash",
Self::Zsh => "zsh",
Self::Fish => "fish",
Self::Elvish => "elvish",
Self::PowerShell => "power-shell",
}
}
/// Convert to the `clap_complete` shell enum.
pub fn to_clap_shell(self) -> clap_complete::Shell {
match self {
Self::Bash => clap_complete::Shell::Bash,
Self::Zsh => clap_complete::Shell::Zsh,
Self::Fish => clap_complete::Shell::Fish,
Self::Elvish => clap_complete::Shell::Elvish,
Self::PowerShell => clap_complete::Shell::PowerShell,
}
}
}
impl Cli {
/// Whether JSON envelope is requested.
///
/// Agent-first rules (stdin/stdout LLM contract):
/// - `--json` or `--format json` → JSON
/// - `--format markdown|text` → human (never auto-JSON)
/// - otherwise → JSON when stdout is not a TTY (pipe/agent), markdown on TTY
pub fn wants_json(&self, stdout_is_terminal: bool) -> bool {
if self.json || matches!(self.format, Some(OutputFormat::Json)) {
return true;
}
if matches!(
self.format,
Some(OutputFormat::Markdown | OutputFormat::Text)
) {
return false;
}
!stdout_is_terminal
}
/// Reject incompatible `--json` + `--format text|markdown`.
///
/// # Errors
///
/// Returns [`crate::error::ErrorKind::Usage`] when `--json` is combined with
/// `--format text` or `--format markdown`.
pub fn validate_format_conflict(&self) -> Result<(), crate::error::AppError> {
use crate::error::{AppError, ErrorKind};
if self.json
&& matches!(
self.format,
Some(OutputFormat::Text | OutputFormat::Markdown)
)
{
return Err(AppError::new(
ErrorKind::Usage,
"cannot combine --json with --format text or --format markdown",
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
#[test]
fn sort_api_strings() {
assert_eq!(SortKind::Relevance.as_api_str(), "relevance");
assert_eq!(SortKind::Downloads.as_api_str(), "downloads");
assert_eq!(SortKind::RecentDownloads.as_api_str(), "recent-downloads");
assert_eq!(SortKind::RecentUpdates.as_api_str(), "recent-updates");
assert_eq!(SortKind::New.as_api_str(), "new");
assert_eq!(SortKind::Alphabetical.as_api_str(), "alphabetical");
}
#[test]
fn wants_json_from_flag_or_format() {
let a = Cli::try_parse_from(["docsrs-cli", "version", "--json"]).unwrap();
assert!(a.wants_json(true));
assert!(a.wants_json(false));
let b = Cli::try_parse_from(["docsrs-cli", "version", "--format", "json"]).unwrap();
assert!(b.wants_json(true));
let c = Cli::try_parse_from(["docsrs-cli", "version"]).unwrap();
assert!(!c.wants_json(true), "TTY defaults to human markdown");
assert!(c.wants_json(false), "non-TTY auto-JSON for agents");
let d = Cli::try_parse_from(["docsrs-cli", "version", "--format", "markdown"]).unwrap();
assert!(!d.wants_json(false), "explicit format overrides auto-JSON");
let e = Cli::try_parse_from(["docsrs-cli", "version", "--format", "text"]).unwrap();
assert!(!e.wants_json(false));
}
#[test]
fn parse_commands_subcommand() {
let cli = Cli::try_parse_from(["docsrs-cli", "commands"]).unwrap();
assert!(matches!(cli.command, Commands::Commands));
}
#[test]
fn format_conflict_detected() {
let cli =
Cli::try_parse_from(["docsrs-cli", "version", "--json", "--format", "text"]).unwrap();
assert!(cli.validate_format_conflict().is_err());
let ok = Cli::try_parse_from(["docsrs-cli", "version", "--json"]).unwrap();
assert!(ok.validate_format_conflict().is_ok());
}
#[test]
fn shell_mapping() {
assert!(matches!(
Shell::Bash.to_clap_shell(),
clap_complete::Shell::Bash
));
assert!(matches!(
Shell::Zsh.to_clap_shell(),
clap_complete::Shell::Zsh
));
assert!(matches!(
Shell::Fish.to_clap_shell(),
clap_complete::Shell::Fish
));
assert!(matches!(
Shell::Elvish.to_clap_shell(),
clap_complete::Shell::Elvish
));
assert!(matches!(
Shell::PowerShell.to_clap_shell(),
clap_complete::Shell::PowerShell
));
}
#[test]
fn powershell_alias_and_canonical_parse() {
let a = Cli::try_parse_from(["docsrs-cli", "completions", "powershell"]).unwrap();
let b = Cli::try_parse_from(["docsrs-cli", "completions", "power-shell"]).unwrap();
match (a.command, b.command) {
(Commands::Completions { shell: sa }, Commands::Completions { shell: sb }) => {
assert!(matches!(sa, Shell::PowerShell));
assert!(matches!(sb, Shell::PowerShell));
}
_ => panic!("expected completions commands"),
}
}
#[test]
fn parse_search_crates_defaults() {
let cli = Cli::try_parse_from(["docsrs-cli", "search-crates", "serde"]).unwrap();
match cli.command {
Commands::SearchCrates {
query,
per_page,
sort,
page,
page_token,
} => {
assert_eq!(query, "serde");
assert_eq!(per_page, 10);
assert_eq!(sort, SortKind::Relevance);
assert_eq!(page, 1);
assert!(page_token.is_none());
}
_ => panic!("wrong command"),
}
}
}