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 why a command does or doesn't approve, and what would change that:
9 safe-chains --explain \"grep foo . && ./deploy.sh\"
10
11 # For a single command, --explain also shows the behavior it resolved to and
12 # says in plain words what it does and which limit refused it:
13 safe-chains --explain \"aws dynamodb put-item --table-name t --item {}\"
14
15 # List the rules safe-chains reads from your files, and where each came from:
16 safe-chains config list
17
18 # Change them. safe-chains never approves these itself, so a person does:
19 safe-chains config add \"mytool sync --dry-run\"
20 safe-chains config grant ~/notes --write
21 safe-chains config remove grant ~/notes
22
23 # Show the project .safe-chains.toml entry for a command safe-chains doesn't
24 # recognize, and the pin that trusts it. Writes nothing:
25 safe-chains --suggest \"mytool sync --dry-run\"
26";
27
28#[derive(Parser)]
29#[command(name = "safe-chains")]
30#[command(about = "Auto-allow safe bash commands in agentic coding tools")]
31#[command(version, disable_version_flag = true)]
32#[command(after_help = EXAMPLES)]
33#[allow(clippy::struct_excessive_bools)]
34pub struct Cli {
35 /// Command string to check (omit for Claude hook mode via stdin)
36 pub command: Option<String>,
37
38 /// Print version information.
39 #[arg(short = 'v', short_alias = 'V', long, action = clap::ArgAction::Version)]
40 pub version: Option<bool>,
41
42 /// Safety level threshold; only commands at or below it auto-approve. Levels, locked → open:
43 /// paranoid, reader, editor, developer, local-admin, network-admin, yolo. The legacy names
44 /// inert / safe-read / safe-write still work (mapped to paranoid / reader / developer, with a
45 /// notice). Default: developer.
46 #[arg(long)]
47 pub level: Option<String>,
48
49 /// Working directory to resolve relative paths against (as a harness hook would pass).
50 /// Pair with --root so e.g. `cd`-relative writes classify against the real directory.
51 #[arg(long)]
52 pub cwd: Option<String>,
53
54 /// Project root, so a relative path under it is worktree-local and one outside it (the
55 /// cwd having escaped the project) is scored as its real absolute target.
56 #[arg(long)]
57 pub root: Option<String>,
58
59 /// Check the command as a hook does when the tool does not say which folder it runs in, with
60 /// writes approved as far as LEVEL allows: reads, developer or workspace. --root is the
61 /// workspace; --cwd is ignored.
62 #[arg(long, value_name = "LEVEL", value_parser = ["reads", "developer", "workspace"])]
63 pub unknown_folder: Option<String>,
64
65 /// The harness session id (as a hook would pass), used to recognize this session's scratchpad
66 /// under a temp root as a trusted working area rather than anonymous `/tmp`.
67 #[arg(long, value_name = "ID")]
68 pub session_id: Option<String>,
69
70 /// Print a per-segment breakdown of why a command would or would not auto-approve. For a
71 /// single command it also prints the behavior it resolved to and, when it does not approve,
72 /// says which limit refused it.
73 #[arg(long)]
74 pub explain: bool,
75
76 /// Show how to support a command safe-chains doesn't recognize yet. Pass the command as the
77 /// argument: `safe-chains --suggest "<command>"`. It prints the `.safe-chains.toml` entry for
78 /// the unrecognized command, the path that file belongs at, and the `[[trusted]]` pin to add to
79 /// ~/.config/safe-chains.toml so it takes effect. Writes nothing — copy the parts you want.
80 #[arg(long)]
81 pub suggest: bool,
82
83 /// Record every command that did NOT auto-approve — with the directory it ran in and why it was
84 /// refused — as JSON Lines in ~/.local/state/safe-chains/log.jsonl. Off unless given. The file
85 /// holds commands verbatim, credentials included, and is created owner-only.
86 #[arg(long)]
87 pub log: bool,
88
89 /// Like --log, but records approvals too. Writes an entry per command rather than per refusal.
90 #[arg(long)]
91 pub log_everything: bool,
92
93 /// List all supported commands in Markdown format
94 #[arg(long)]
95 pub list_commands: bool,
96
97 /// Generate mdBook command reference pages in docs/src/commands/
98 #[arg(long)]
99 pub generate_book: bool,
100
101 /// Configure the hook for the named tool (default: claude). Use --auto-detect for every installed tool.
102 #[arg(long)]
103 pub setup: bool,
104
105 /// Pair with --setup to select the target tool by name. See --list-tools.
106 #[arg(long, value_name = "NAME")]
107 pub tool: Option<String>,
108
109 /// Pair with --setup to install for every installed tool detected on this machine.
110 #[arg(long)]
111 pub auto_detect: bool,
112
113 /// Print the names of every supported integration target.
114 #[arg(long)]
115 pub list_tools: bool,
116
117 /// Hook subcommand: read this tool's stdin envelope, validate the command, write the response.
118 #[command(subcommand)]
119 pub subcommand: Option<Subcommand>,
120}
121
122#[derive(clap::Subcommand)]
123pub enum Subcommand {
124 /// Run as a runtime hook for the named tool.
125 Hook {
126 /// Tool to read/write the hook envelope for. See --list-tools.
127 #[arg(value_name = "TOOL")]
128 tool: String,
129
130 /// How far to approve writes when this tool does not say which folder the command runs
131 /// in: reads, developer or workspace. Overrides `[unknown_folder] writes` in
132 /// ~/.config/safe-chains.toml. Default: developer.
133 #[arg(long, value_name = "LEVEL", value_parser = ["reads", "developer", "workspace"])]
134 unknown_folder: Option<String>,
135 },
136 /// List or change the rules in ~/.config/safe-chains.toml. safe-chains never approves `add`,
137 /// `remove` or `grant` by itself, so a person approves each change.
138 Config {
139 #[command(subcommand)]
140 action: ConfigAction,
141 },
142 /// Serve the same checks and config changes over MCP on stdin and stdout, for agents that call
143 /// tools rather than run commands.
144 Mcp,
145}
146
147#[derive(clap::Subcommand)]
148pub enum ConfigAction {
149 /// List every rule safe-chains reads from a file, under the file it came from.
150 List,
151 /// Add an entry for a command safe-chains has no entry for, approving it used the way it is
152 /// here: the same subcommand and flags, and up to as many other arguments.
153 Add {
154 /// The command, as one quoted string or as separate words.
155 #[arg(required = true, num_args = 1.., trailing_var_arg = true, allow_hyphen_values = true, value_name = "COMMAND")]
156 command: Vec<String>,
157 /// How much the command may do: inert, safe-read or safe-write.
158 #[arg(long, default_value = "safe-write")]
159 level: String,
160 },
161 /// Remove a command entry or a grant from ~/.config/safe-chains.toml.
162 Remove {
163 /// What to remove.
164 #[arg(value_enum)]
165 kind: RuleKind,
166 /// The command's name, or the grant's path.
167 #[arg(value_name = "NAME_OR_PATH")]
168 key: String,
169 },
170 /// Let commands read or write in a folder outside the places safe-chains approves by default.
171 Grant {
172 /// The folder or file. A relative path is taken from the current directory.
173 path: String,
174 /// Approve reading there.
175 #[arg(long)]
176 read: bool,
177 /// Approve writing there.
178 #[arg(long)]
179 write: bool,
180 },
181}
182
183/// The two kinds of rule `config remove` can remove.
184#[derive(clap::ValueEnum, Clone, Copy)]
185pub enum RuleKind {
186 Command,
187 Grant,
188}