stern4rust/settings/args.rs
1// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the MIT License
3// SPDX-License-Identifier: MIT
4
5use crate::reporting::output_format::OutputFormat;
6use clap::Parser;
7use std::path::PathBuf;
8
9#[derive(Clone, Debug, Parser)]
10#[command(name = "cargo-stern4rust")]
11#[command(bin_name = "cargo stern4rust")]
12#[command(version)]
13#[command(about = "Check Rust packages and fail the build when the rule is broken")]
14pub struct Args {
15 #[arg(long)]
16 pub manifest_path: Option<PathBuf>,
17
18 #[arg(long = "package")]
19 pub packages: Vec<String>,
20
21 /// File holding the header every .rs file must open with. It is data rather
22 /// than a built-in constant because it is never the same twice: MIT here,
23 /// Apache 2.0 in a sibling repository, and a different year again next year.
24 #[arg(long)]
25 pub header_file: Option<PathBuf>,
26
27 /// How to report. The table is for a person; `json` is the same run as a
28 /// document, for a gate script or an agent that would otherwise have to
29 /// guess where one column of the table ends and the next begins.
30 #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
31 pub format: OutputFormat,
32
33 /// How many offences the report prints. A first run against a large
34 /// codebase can find a thousand, and a thousand rows is a wall rather than
35 /// a report. The cap is on what is shown and never on what is counted:
36 /// the summary, the omitted count and the exit code all see every offence.
37 /// Use 0 for no limit.
38 /// Option rather than a defaulted value so that "not passed" is
39 /// distinguishable from "passed the default": without that, a
40 /// stern4rust.toml could never set the threshold, because every run would
41 /// look like the reader had asked for 100 on the command line.
42 #[arg(long)]
43 pub offence_threshold: Option<usize>,
44
45 /// Apply only these rules; repeatable. Omit to apply every rule. Naming one
46 /// makes the selection a whitelist, which is what lets a codebase facing
47 /// hundreds of offences gate on one rule today and the rest as it goes.
48 #[arg(long = "rule")]
49 pub rules: Vec<String>,
50
51 /// Repair what can be repaired mechanically, then report what is left.
52 /// Only test-file-structure offences are fixable today: item order, section
53 /// order and blank lines. Everything else is reported unchanged, and the
54 /// report says how many offences were fixed and how many were not.
55 #[arg(long)]
56 pub fix: bool,
57
58 /// Offences recorded here are not reported and do not fail the run. What
59 /// lets a codebase with hundreds of existing offences enforce every rule
60 /// against new code without first fixing the old. The count of suppressed
61 /// offences is always in the summary, never hidden.
62 #[arg(long)]
63 pub baseline: Option<PathBuf>,
64
65 /// Record the current offences as the baseline and exit clean, instead of
66 /// judging against one. Writes to --baseline, or to stern4rust-baseline.json
67 /// beside the manifest.
68 #[arg(long)]
69 pub write_baseline: bool,
70
71 /// Keep these paths out of the run; repeatable, matched as a glob against
72 /// the package-relative path. For a tree the repository cannot move --
73 /// vendored source, generated output. Every pattern is named in the report
74 /// with how many files it removed, including zero, so an exclusion is
75 /// something the reader can see rather than a silence.
76 #[arg(long = "exclude")]
77 pub excludes: Vec<String>,
78
79 /// Do not apply these rules; repeatable. Subtracted from whatever --rule
80 /// selected, so skipping wins over selecting.
81 #[arg(long = "skip")]
82 pub skipped_rules: Vec<String>,
83}
84
85impl Args {
86 /// Cargo invokes `cargo stern4rust` as `cargo-stern4rust stern4rust ...`, so
87 /// the subcommand name arrives as an extra leading argument that clap would
88 /// otherwise reject. Running the binary directly does not repeat it, which
89 /// is why the strip is conditional rather than unconditional.
90 pub fn without_cargo_subcommand<I>(args: I) -> Vec<String>
91 where
92 I: IntoIterator<Item = String>,
93 {
94 let args: Vec<String> = args.into_iter().collect();
95 if args.get(1).map(String::as_str) != Some("stern4rust") {
96 return args;
97 }
98 let mut forwarded = Vec::with_capacity(args.len() - 1);
99 forwarded.extend(args.iter().take(1).cloned());
100 forwarded.extend(args.into_iter().skip(2));
101 forwarded
102 }
103}