safe_chains/cli.rs
1use clap::Parser;
2
3const EXAMPLES: &str = "\
4EXAMPLES:
5 # Check whether a command would auto-approve (exit 0 = allowed):
6 safe-chains \"git status\"
7
8 # See a per-segment breakdown of why a command does or doesn't approve:
9 safe-chains --explain \"grep foo . && ./deploy.sh\"
10
11 # Offer to support a command safe-chains doesn't recognize. This writes or
12 # upgrades the local .safe-chains.toml, then prints the pin to hand-add to
13 # ~/.config/safe-chains.toml so the definition takes effect:
14 safe-chains --suggest \"mytool sync --dry-run\"
15";
16
17#[derive(Parser)]
18#[command(name = "safe-chains")]
19#[command(about = "Auto-allow safe bash commands in agentic coding tools")]
20#[command(version, disable_version_flag = true)]
21#[command(after_help = EXAMPLES)]
22#[allow(clippy::struct_excessive_bools)]
23pub struct Cli {
24 /// Command string to check (omit for Claude hook mode via stdin)
25 pub command: Option<String>,
26
27 /// Print version information.
28 #[arg(short = 'v', short_alias = 'V', long, action = clap::ArgAction::Version)]
29 pub version: Option<bool>,
30
31 /// Safety level threshold; only commands at or below it auto-approve. Levels, locked → open:
32 /// paranoid, reader, editor, developer, local-admin, network-admin, yolo. The legacy names
33 /// inert / safe-read / safe-write still work (mapped to paranoid / reader / developer, with a
34 /// notice). Default: developer.
35 #[arg(long)]
36 pub level: Option<String>,
37
38 /// Working directory to resolve relative paths against (as a harness hook would pass).
39 /// Pair with --root so e.g. `cd`-relative writes classify against the real directory.
40 #[arg(long)]
41 pub cwd: Option<String>,
42
43 /// Project root, so a relative path under it is worktree-local and one outside it (the
44 /// cwd having escaped the project) is scored as its real absolute target.
45 #[arg(long)]
46 pub root: Option<String>,
47
48 /// Print a per-segment breakdown of why a command would or would not auto-approve.
49 #[arg(long)]
50 pub explain: bool,
51
52 /// Offer to support a command safe-chains doesn't recognize yet. Pass the command as the
53 /// argument: `safe-chains --suggest "<command>"`. It writes (or upgrades) the project's local
54 /// `.safe-chains.toml` with a definition for the unrecognized command, then prints the
55 /// `[[trusted]]` pin for you to add to ~/.config/safe-chains.toml so that file takes effect.
56 #[arg(long)]
57 pub suggest: bool,
58
59 /// List all supported commands in Markdown format
60 #[arg(long)]
61 pub list_commands: bool,
62
63 /// Generate mdBook command reference pages in docs/src/commands/
64 #[arg(long)]
65 pub generate_book: bool,
66
67 /// Configure the hook for the named tool (default: claude). Use --auto-detect for every installed tool.
68 #[arg(long)]
69 pub setup: bool,
70
71 /// Pair with --setup to select the target tool by name. See --list-tools.
72 #[arg(long, value_name = "NAME")]
73 pub tool: Option<String>,
74
75 /// Pair with --setup to install for every installed tool detected on this machine.
76 #[arg(long)]
77 pub auto_detect: bool,
78
79 /// Print the names of every supported integration target.
80 #[arg(long)]
81 pub list_tools: bool,
82
83 /// Hook subcommand: read this tool's stdin envelope, validate the command, write the response.
84 #[command(subcommand)]
85 pub subcommand: Option<Subcommand>,
86}
87
88#[derive(clap::Subcommand)]
89pub enum Subcommand {
90 /// Run as a runtime hook for the named tool.
91 Hook {
92 /// Tool to read/write the hook envelope for. See --list-tools.
93 #[arg(value_name = "TOOL")]
94 tool: String,
95 },
96}