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
//! Clap-backed command-line declaration for the `forbidden-strings` binary.
//!
//! This module owns flags, positionals, help text, version text, and validation
//! for argv-shaped input. Environment-variable fallback stays in `lib.rs` because
//! it is not itself command-line syntax.
/// Imports clap's parser trait and derive macro for this CLI declaration.
// What: `use clap::Parser;` brings TWO clap tools into scope under one name:
// the `Parser` trait, which supplies `Cli::try_parse_from(...)`, and
// the `#[derive(Parser)]` macro, which generates the argv scanner for
// the struct below. `::` is Rust's namespace separator, like `.` on an
// imported object in TypeScript.
// Why: Delegate option parsing, help, version, and validation to clap
// instead of hand-scanning raw strings.
// Gotcha: The derive macro writes Rust code during compilation. This file keeps
// that code generation at the CLI boundary so the scanner logic stays
// ordinary functions.
//
// In TS you'd write (pseudocode):
// ```ts
// import { parseArgs } from "some-cli-parser";
// ```
use Parser;
/// Custom clap help layout with the historical uppercase usage heading.
// What: `const HELP_TEMPLATE: &str = "..."` declares a borrowed static string.
// `&str` is a read-only view into UTF-8 bytes baked into the binary;
// sibling type `String` would allocate owned bytes at runtime. Clap
// replaces placeholders like `{usage}` and `{all-args}` when rendering
// `--help`.
// Why: Keep the visible `USAGE:` heading already documented by tests while
// letting clap generate the actual flag and positional sections.
//
// In TS you'd write (pseudocode):
// ```ts
// const HELP_TEMPLATE = `${name} ${version}\n${about}\n...`;
// ```
const HELP_TEMPLATE: &str = "\
{name} {version}\n\
{about}\n\
\n\
USAGE:\n\
{usage}\n\
\n\
{all-args}{after-help}\
";
/// Extra help text that describes runtime fallback and rule semantics.
// What: `const AFTER_HELP: &str = "..."` is another static string slice. The
// sibling `String` is unnecessary because the text never changes.
// Why: Clap owns syntax help, while this block carries domain notes that are
// not attached to one flag: environment fallback, exit codes, rule
// grammar, the dialect's flag policy, and output format.
//
// In TS you'd write (pseudocode):
// ```ts
// const AFTER_HELP = `ENV:\n FORBIDDEN_STRINGS_RULES ...`;
// ```
const AFTER_HELP: &str = "\
ENV:\n\
FORBIDDEN_STRINGS_RULES Default rules path; --rules wins if both are set.\n\
If unset, falls back to ./forbidden-strings.local.txt\n\
\n\
BUILT-IN BASELINE:\n\
--builtin-rules appends the embedded betterleaks-ported baseline after\n\
the resolved rules file (user rule numbering is unchanged). When the\n\
implicit default rules file is absent, the baseline alone is used; an\n\
explicitly named missing file (--rules or env) still errors. Without\n\
the flag, the baseline is never read.\n\
\n\
EXIT CODES:\n\
0 No violations.\n\
1 One or more violations (printed to stderr, redacted).\n\
2 Usage error or rule-file error.\n\
\n\
EXAMPLES:\n\
# Scan a few files\n\
forbidden-strings --rules ./rules.txt src/main.ts README.md\n\
\n\
# Scan the whole working tree\n\
FORBIDDEN_STRINGS_RULES=./rules.txt forbidden-strings --all\n\
\n\
RULE FORMAT:\n\
Bare line -> case-sensitive literal substring\n\
/PATTERN/FLAGS -> regex in the forbidden-regex dialect\n\
# ... -> comment\n\
Empty line -> skipped\n\
\n\
DIALECT:\n\
Supported: literals, classes [a-z] and \\d \\w \\s, '.', (?:a|b),\n\
bounded repetition a{3,6}, anchors ^ $ \\b, and set algebra A & B\n\
and ~(A). Flags 'm' and 'x' are accepted no-ops (multiline and\n\
verbose are always on); any other flag letter is a hard load error.\n\
Rejected at compile time (fail-closed): '*', '+', unbounded {n,},\n\
capturing '(', lookaround and inline flags, backreferences, and any\n\
pattern matching the empty string.\n\
\n\
OUTPUT:\n\
PATH:LINE rule=N (columnless; matched substring is NEVER printed)\n\
\n\
See README.md for the full dialect, set-algebra examples, and CI integration.\n\
";
/// Parsed command-line options for `forbidden-strings`.
// What: `#[derive(Parser, Debug, PartialEq)]` asks Rust to generate three
// implementations for `Cli`: clap's parser, debug formatting, and
// equality. `#[command(...)]` configures clap's program metadata for
// `--help` and `--version`. `pub struct Cli { ... }` is an exported
// record type with owned fields. Siblings a TS reader might expect:
// a plain object type with a handwritten parser, or a builder API.
// Why: Declaring the CLI as data lets clap validate arguments and generate
// user-facing help while the scanner run loop receives typed options.
//
// In TS you'd write (pseudocode):
// ```ts
// // @cli({ name: "forbidden-strings", version, about: "..." })
// export type Cli = { rulesPath?: string; all: boolean; files: string[] };
// ```