rippy-cli 0.1.2

A shell command safety hook for AI coding tools (Claude Code, Cursor, Gemini CLI) — Rust rewrite of Dippy
Documentation
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Flag alias discovery — parse `--help` output to find short/long flag pairs.
//!
//! Enables auto-expansion: a rule with `flags = ["--force"]` also matches `-f`
//! if the flag cache knows `--force` aliases to `-f`.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use crate::cli::DiscoverArgs;
use crate::config;
use crate::error::RippyError;

/// A short ↔ long flag pair discovered from help output.
#[derive(Debug, Clone, PartialEq, Eq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct FlagAlias {
    pub short: String,
    pub long: String,
}

/// Current cache format version. Bump to force re-discovery on upgrade.
const CACHE_VERSION: u32 = 1;

/// Cached flag aliases keyed by command (e.g. "git push", "curl").
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct FlagCache {
    /// Cache format version — mismatched versions are discarded.
    pub version: u32,
    /// Map from command key to list of flag aliases.
    pub entries: BTreeMap<String, Vec<FlagAlias>>,
}

impl Default for FlagCache {
    fn default() -> Self {
        Self {
            version: CACHE_VERSION,
            entries: BTreeMap::new(),
        }
    }
}

// ── Help output parser ─────────────────────────────────────────────────

/// Parse help output text and extract short/long flag pairs.
///
/// Recognizes common CLI framework formats:
/// - `  -f, --force          description`  (clap, cobra, argparse)
/// - `  --force, -f          description`  (reverse order)
/// - `       -n, --dry-run`                (git man pages)
#[must_use]
pub fn parse_help_output(output: &str) -> Vec<FlagAlias> {
    let mut aliases = Vec::new();

    for line in output.lines() {
        if let Some(alias) = parse_flag_line(line) {
            aliases.push(alias);
        }
    }

    // Deduplicate by long flag (keep first occurrence).
    let mut seen = std::collections::HashSet::new();
    aliases.retain(|a| seen.insert(a.long.clone()));
    aliases
}

/// Try to extract a flag alias from a single line.
fn parse_flag_line(line: &str) -> Option<FlagAlias> {
    let trimmed = line.trim();

    // Find positions of short flag (-X) and long flag (--word).
    // Pattern 1: -X, --long or -X --long
    // Pattern 2: --long, -X or --long -X
    let tokens: Vec<&str> = trimmed.split_whitespace().collect();

    for window in tokens.windows(2) {
        let a = window[0].trim_end_matches(',');
        let b = window[1].trim_end_matches(',');

        if let Some(alias) = match_flag_pair(a, b) {
            return Some(alias);
        }
        if let Some(alias) = match_flag_pair(b, a) {
            return Some(alias);
        }
    }

    None
}

/// Check if two tokens form a short/long flag pair.
fn match_flag_pair(a: &str, b: &str) -> Option<FlagAlias> {
    let is_short = a.starts_with('-')
        && !a.starts_with("--")
        && a.len() == 2
        && a.as_bytes().get(1).is_some_and(u8::is_ascii_alphabetic);

    let is_long = b.starts_with("--") && b.len() > 2 && b.as_bytes()[2].is_ascii_alphabetic();

    if is_short && is_long {
        Some(FlagAlias {
            short: a.to_string(),
            long: b.to_string(),
        })
    } else {
        None
    }
}

// ── Flag cache ─────────────────────────────────────────────────────────

fn cache_path() -> Option<PathBuf> {
    config::home_dir().map(|h| h.join(".rippy/flag-cache.bin"))
}

/// Load the flag cache from `~/.rippy/flag-cache.bin`.
#[must_use]
pub fn load_cache() -> FlagCache {
    let Some(path) = cache_path() else {
        return FlagCache::default();
    };
    load_cache_from(&path).unwrap_or_default()
}

fn load_cache_from(path: &Path) -> Option<FlagCache> {
    let bytes = std::fs::read(path).ok()?;
    let cache = rkyv::from_bytes::<FlagCache, rkyv::rancor::Error>(&bytes).ok()?;
    // Discard cache if version doesn't match (forces re-discovery on upgrade).
    if cache.version != CACHE_VERSION {
        return None;
    }
    Some(cache)
}

/// Save the flag cache to `~/.rippy/flag-cache.bin`.
///
/// # Errors
///
/// Returns `RippyError::Setup` if the file cannot be written.
pub fn save_cache(cache: &FlagCache) -> Result<(), RippyError> {
    let Some(path) = cache_path() else {
        return Err(RippyError::Setup(
            "could not determine home directory".into(),
        ));
    };

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| {
            RippyError::Setup(format!("could not create {}: {e}", parent.display()))
        })?;
    }

    let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(cache)
        .map_err(|e| RippyError::Setup(format!("could not serialize flag cache: {e}")))?;
    std::fs::write(&path, &bytes)
        .map_err(|e| RippyError::Setup(format!("could not write {}: {e}", path.display())))?;
    Ok(())
}

#[cfg(test)]
fn save_cache_to(cache: &FlagCache, path: &Path) -> Result<(), RippyError> {
    let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(cache)
        .map_err(|e| RippyError::Setup(format!("could not serialize flag cache: {e}")))?;
    std::fs::write(path, &bytes)
        .map_err(|e| RippyError::Setup(format!("could not write {}: {e}", path.display())))?;
    Ok(())
}

// ── Discovery ──────────────────────────────────────────────────────────

/// Run a command with `--help` and parse the output for flag aliases.
///
/// # Errors
///
/// Returns `RippyError::Setup` if the command cannot be executed.
pub fn discover_flags(
    command: &str,
    subcommand: Option<&str>,
) -> Result<Vec<FlagAlias>, RippyError> {
    let mut cmd = std::process::Command::new(command);
    if let Some(sub) = subcommand {
        cmd.arg(sub);
    }
    cmd.arg("--help");
    cmd.stdout(std::process::Stdio::piped());
    cmd.stderr(std::process::Stdio::piped());

    let output = cmd
        .output()
        .map_err(|e| RippyError::Setup(format!("could not run `{command} --help`: {e}")))?;

    // Some tools print help to stdout, others to stderr.
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{stdout}\n{stderr}");

    Ok(parse_help_output(&combined))
}

/// Expand a list of flags with their aliases from the cache.
///
/// Given `["--force"]` and a cache with `--force → -f`, returns `["--force", "-f"]`.
#[must_use]
pub fn expand_flags(flags: &[String], cache: &FlagCache, command: Option<&str>) -> Vec<String> {
    let mut expanded: Vec<String> = flags.to_vec();

    let Some(cmd) = command else {
        return expanded;
    };

    // Try exact command key and command-only key.
    let aliases = cache.entries.get(cmd);

    if let Some(alias_list) = aliases {
        for flag in flags {
            for alias in alias_list {
                if flag == &alias.long && !expanded.contains(&alias.short) {
                    expanded.push(alias.short.clone());
                } else if flag == &alias.short && !expanded.contains(&alias.long) {
                    expanded.push(alias.long.clone());
                }
            }
        }
    }

    expanded
}

// ── CLI entry point ────────────────────────────────────────────────────

/// Run the `rippy discover` command.
///
/// # Errors
///
/// Returns `RippyError::Setup` if discovery or cache writing fails.
pub fn run(args: &DiscoverArgs) -> Result<ExitCode, RippyError> {
    if args.all {
        return rediscover_all(args.json);
    }

    let Some(command) = args.args.first() else {
        return Err(RippyError::Setup(
            "usage: rippy discover <command> [subcommand]".into(),
        ));
    };

    let subcommand = args.args.get(1).map(String::as_str);
    let aliases = discover_flags(command, subcommand)?;

    if args.json {
        print_json(&aliases);
    } else {
        print_text(command, subcommand, &aliases);
    }

    // Update cache.
    let mut cache = load_cache();
    let key = cache_key(command, subcommand);
    cache.entries.insert(key, aliases);
    save_cache(&cache)?;

    Ok(ExitCode::SUCCESS)
}

fn cache_key(command: &str, subcommand: Option<&str>) -> String {
    subcommand.map_or_else(|| command.to_string(), |sub| format!("{command} {sub}"))
}

fn rediscover_all(json: bool) -> Result<ExitCode, RippyError> {
    let cache = load_cache();
    let mut new_cache = FlagCache::default();

    for key in cache.entries.keys() {
        let mut parts = key.split_whitespace();
        let Some(cmd) = parts.next() else { continue };
        let sub = parts.next();
        match discover_flags(cmd, sub) {
            Ok(aliases) => {
                if !json {
                    eprintln!("[rippy] discovered {} flags for {key}", aliases.len());
                }
                new_cache.entries.insert(key.clone(), aliases);
            }
            Err(e) => {
                eprintln!("[rippy] warning: {key}: {e}");
            }
        }
    }

    save_cache(&new_cache)?;
    if json {
        println!("{{\"refreshed\": {}}}", new_cache.entries.len());
    } else {
        eprintln!("[rippy] Refreshed {} commands", new_cache.entries.len());
    }
    Ok(ExitCode::SUCCESS)
}

fn print_text(command: &str, subcommand: Option<&str>, aliases: &[FlagAlias]) {
    let label = subcommand.map_or_else(|| command.to_string(), |sub| format!("{command} {sub}"));
    if aliases.is_empty() {
        eprintln!("[rippy] No flag aliases discovered for {label}");
        return;
    }
    println!("Flag aliases for {label}:\n");
    for alias in aliases {
        println!("  {:<6} {}", alias.short, alias.long);
    }
    println!("\n{} alias(es) cached.", aliases.len());
}

fn print_json(aliases: &[FlagAlias]) {
    let pairs: Vec<serde_json::Value> = aliases
        .iter()
        .map(|a| {
            serde_json::json!({
                "short": a.short,
                "long": a.long,
            })
        })
        .collect();
    let json = serde_json::to_string_pretty(&serde_json::Value::Array(pairs));
    if let Ok(j) = json {
        println!("{j}");
    }
}

// ── Tests ──────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn parse_clap_style() {
        let help = "  -f, --force          Force operation\n  -v, --verbose        Be verbose\n";
        let aliases = parse_help_output(help);
        assert_eq!(aliases.len(), 2);
        assert_eq!(aliases[0].short, "-f");
        assert_eq!(aliases[0].long, "--force");
        assert_eq!(aliases[1].short, "-v");
        assert_eq!(aliases[1].long, "--verbose");
    }

    #[test]
    fn parse_git_manpage_style() {
        let help = "       -n, --dry-run\n       -d, --delete\n";
        let aliases = parse_help_output(help);
        assert_eq!(aliases.len(), 2);
        assert_eq!(aliases[0].short, "-n");
        assert_eq!(aliases[0].long, "--dry-run");
    }

    #[test]
    fn parse_reverse_order() {
        let help = "  --force, -f          Force operation\n";
        let aliases = parse_help_output(help);
        assert_eq!(aliases.len(), 1);
        assert_eq!(aliases[0].short, "-f");
        assert_eq!(aliases[0].long, "--force");
    }

    #[test]
    fn parse_no_comma() {
        let help = "  -q --quiet           Suppress output\n";
        let aliases = parse_help_output(help);
        assert_eq!(aliases.len(), 1);
        assert_eq!(aliases[0].short, "-q");
        assert_eq!(aliases[0].long, "--quiet");
    }

    #[test]
    fn parse_with_value_placeholder() {
        let help = "  -o, --output <file>  Write to file\n";
        let aliases = parse_help_output(help);
        assert_eq!(aliases.len(), 1);
        assert_eq!(aliases[0].short, "-o");
        assert_eq!(aliases[0].long, "--output");
    }

    #[test]
    fn parse_ignores_long_only() {
        let help = "  --verbose            Be verbose\n  --quiet              Quiet\n";
        let aliases = parse_help_output(help);
        assert!(aliases.is_empty());
    }

    #[test]
    fn parse_ignores_noise() {
        let help = "Usage: git push [options]\n\nOptions:\n  This is a description.\n";
        let aliases = parse_help_output(help);
        assert!(aliases.is_empty());
    }

    #[test]
    fn parse_deduplicates() {
        let help = "  -f, --force   Force\n  -f, --force   Force again\n";
        let aliases = parse_help_output(help);
        assert_eq!(aliases.len(), 1);
    }

    #[test]
    fn parse_curl_real_output() {
        let help = "\
 -d, --data <data>           HTTP POST data
 -f, --fail                  Fail fast with no output on HTTP errors
 -h, --help <category>       Get help for commands
 -i, --include               Include response headers in output
 -o, --output <file>         Write to file instead of stdout
 -s, --silent                Silent mode
 -u, --user <user:password>  Server user and password";
        let aliases = parse_help_output(help);
        assert_eq!(aliases.len(), 7);
        assert!(
            aliases
                .iter()
                .any(|a| a.short == "-f" && a.long == "--fail")
        );
        assert!(
            aliases
                .iter()
                .any(|a| a.short == "-s" && a.long == "--silent")
        );
    }

    #[test]
    fn expand_flags_with_cache() {
        let mut cache = FlagCache::default();
        cache.entries.insert(
            "git push".into(),
            vec![FlagAlias {
                short: "-f".into(),
                long: "--force".into(),
            }],
        );

        let expanded = expand_flags(&["--force".into()], &cache, Some("git push"));
        assert!(expanded.contains(&"--force".to_string()));
        assert!(expanded.contains(&"-f".to_string()));
    }

    #[test]
    fn expand_flags_reverse() {
        let mut cache = FlagCache::default();
        cache.entries.insert(
            "curl".into(),
            vec![FlagAlias {
                short: "-s".into(),
                long: "--silent".into(),
            }],
        );

        let expanded = expand_flags(&["-s".into()], &cache, Some("curl"));
        assert!(expanded.contains(&"-s".to_string()));
        assert!(expanded.contains(&"--silent".to_string()));
    }

    #[test]
    fn expand_flags_no_cache_entry() {
        let cache = FlagCache::default();
        let expanded = expand_flags(&["--force".into()], &cache, Some("unknown"));
        assert_eq!(expanded, vec!["--force".to_string()]);
    }

    #[test]
    fn expand_flags_no_command() {
        let cache = FlagCache::default();
        let expanded = expand_flags(&["--force".into()], &cache, None);
        assert_eq!(expanded, vec!["--force".to_string()]);
    }

    #[test]
    fn cache_round_trip() {
        let dir = tempfile::TempDir::new().unwrap();
        let path = dir.path().join("flag-cache.bin");

        let mut cache = FlagCache::default();
        cache.entries.insert(
            "git push".into(),
            vec![
                FlagAlias {
                    short: "-f".into(),
                    long: "--force".into(),
                },
                FlagAlias {
                    short: "-n".into(),
                    long: "--dry-run".into(),
                },
            ],
        );

        // Save via save_cache_to (same serialization as save_cache)
        save_cache_to(&cache, &path).unwrap();

        // Load
        let loaded = load_cache_from(&path).unwrap();
        assert!(loaded.entries.contains_key("git push"));
        let aliases = &loaded.entries["git push"];
        assert_eq!(aliases.len(), 2);
        assert!(
            aliases
                .iter()
                .any(|a| a.short == "-f" && a.long == "--force")
        );
    }

    #[test]
    fn cache_version_mismatch_returns_none() {
        let dir = tempfile::TempDir::new().unwrap();
        let path = dir.path().join("flag-cache.bin");

        let cache = FlagCache {
            version: 999, // Wrong version
            ..FlagCache::default()
        };
        save_cache_to(&cache, &path).unwrap();

        assert!(load_cache_from(&path).is_none());
    }

    #[test]
    fn cache_key_format() {
        assert_eq!(cache_key("git", Some("push")), "git push");
        assert_eq!(cache_key("curl", None), "curl");
    }
}