utf8_forge/cli.rs
1//! clap definitions for the CLI surface (`DESIGN.md §9`).
2//!
3//! All flags carry `///` doc comments in English (AGENT.md §5 policy).
4//! clap parse failures exit with code 2, matching the documented fatal-code
5//! contract; semantic conflicts are validated in [`TreeArgs::validate`].
6
7use std::path::PathBuf;
8
9use clap::{Args, Parser, Subcommand};
10
11use crate::transcoder::TargetEncoding;
12
13/// utf8-forge — a focused, idempotent, bidirectional charset transcoder.
14#[derive(Debug, Parser)]
15#[command(
16 name = "utf8-forge",
17 version,
18 about = "Idempotent charset transcoder for source trees (default target: UTF-8 without BOM)"
19)]
20pub struct Cli {
21 /// Selected subcommand.
22 #[command(subcommand)]
23 pub command: Commands,
24}
25
26/// Subcommands (`DESIGN.md §9`). Named `Commands` to avoid colliding with
27/// the imported `clap::Subcommand` derive trait.
28#[derive(Debug, Subcommand)]
29pub enum Commands {
30 /// Detect only; report distribution (no writes).
31 Scan(TreeArgs),
32 /// Detect + transcode + atomic write (supports --dry-run, --url).
33 Convert(ConvertArgs),
34 /// Assert all whitelisted files are UTF-8 without BOM.
35 Verify(TreeArgs),
36}
37
38/// Options shared by `scan` / `convert` / `verify` (tree mode).
39#[derive(Debug, Args, Clone, Default)]
40pub struct TreeArgs {
41 /// Root directory to process recursively.
42 #[arg(short = 'r', long = "root", default_value = ".")]
43 pub root: PathBuf,
44
45 /// Override the default extension whitelist (comma-separated).
46 #[arg(short = 'e', long = "ext", value_delimiter = ',')]
47 pub ext: Vec<String>,
48
49 /// Extra excluded directory names (comma-separated, appended to defaults).
50 #[arg(short = 'x', long = "exclude", value_delimiter = ',')]
51 pub exclude: Vec<String>,
52
53 /// Extra excluded extensions (comma-separated, appended).
54 #[arg(short = 'X', long = "exclude-ext", value_delimiter = ',')]
55 pub exclude_ext: Vec<String>,
56
57 /// Extra glob pattern excluding root-relative paths (repeatable).
58 #[arg(long = "exclude-glob")]
59 pub exclude_glob: Vec<String>,
60
61 /// Include .rc/.rc2/.dlg (rc.exe has limited UTF-8 support).
62 #[arg(long = "include-rc")]
63 pub include_rc: bool,
64
65 /// Include .sln/.vcxproj/... (VS re-saves these with a BOM).
66 #[arg(long = "include-msbuild")]
67 pub include_msbuild: bool,
68
69 /// Process dot-prefixed files and directories.
70 #[arg(long = "include-hidden")]
71 pub include_hidden: bool,
72
73 /// Target encoding: utf-8 | utf-8-bom | gb18030 | utf-16le | utf-16be.
74 #[arg(short = 't', long = "target", default_value = "utf-8")]
75 pub target: TargetEncoding,
76
77 /// Report what would change without writing anything.
78 #[arg(long = "dry-run")]
79 pub dry_run: bool,
80
81 /// Mirror original bytes under this directory before overwriting.
82 #[arg(long = "backup-dir")]
83 pub backup_dir: Option<PathBuf>,
84
85 /// Skip files above this size, recorded as too_large (bytes).
86 #[arg(long = "max-file-size", default_value_t = 209_715_200)]
87 pub max_file_size: u64,
88
89 /// Rayon worker thread count (default: CPU count).
90 #[arg(long = "threads")]
91 pub threads: Option<usize>,
92
93 /// Write a JSON report to this path.
94 #[arg(long = "report")]
95 pub report: Option<PathBuf>,
96
97 /// Print errors only.
98 #[arg(short = 'q', long = "quiet")]
99 pub quiet: bool,
100
101 /// Print one line per file.
102 #[arg(short = 'v', long = "verbose")]
103 pub verbose: bool,
104}
105
106/// `convert`-only additions.
107#[derive(Debug, Args, Clone, Default)]
108pub struct ConvertArgs {
109 /// Shared tree-mode options (root ignored when --url is given).
110 #[command(flatten)]
111 pub common: TreeArgs,
112
113 /// Fetch this remote file and transcode it (convert only).
114 #[arg(long = "url")]
115 pub url: Option<String>,
116
117 /// Destination path for --url (derived from the URL when omitted).
118 #[arg(long = "output")]
119 pub output: Option<PathBuf>,
120}