Skip to main content

cargo_fixit/util/
cli.rs

1use clap::Parser;
2
3#[derive(Debug, Parser)]
4pub struct CheckFlags {
5    /// Package(s) to fix
6    #[arg(short, long, value_name = "SPEC", help_heading = "Package Selection")]
7    package: Vec<String>,
8
9    /// Fix all packages in the workspace
10    #[arg(long, help_heading = "Package Selection")]
11    workspace: bool,
12
13    /// Exclude packages from the fixes
14    #[arg(long, value_name = "SPEC", help_heading = "Package Selection")]
15    exclude: Vec<String>,
16
17    /// Alias for --workspace (deprecated)
18    #[arg(long, help_heading = "Package Selection")]
19    all: bool,
20
21    /// Fix only this package's library
22    #[arg(long, help_heading = "Target Selection")]
23    lib: bool,
24
25    /// Fix all binaries
26    #[arg(long, help_heading = "Target Selection")]
27    bins: bool,
28
29    /// Fix only the specified binary
30    #[arg(long, value_name = "NAME", help_heading = "Target Selection")]
31    bin: Option<String>,
32
33    /// Fix all examples
34    #[arg(long, help_heading = "Target Selection")]
35    examples: bool,
36
37    /// Fix only the specified binary
38    #[arg(long, value_name = "NAME", help_heading = "Target Selection")]
39    example: Option<String>,
40
41    /// Fix all tests
42    #[arg(long, help_heading = "Target Selection")]
43    tests: bool,
44
45    /// Fix only the specified test
46    #[arg(long, value_name = "NAME", help_heading = "Target Selection")]
47    test: Option<String>,
48
49    /// Fix all benches
50    #[arg(long, help_heading = "Target Selection")]
51    benches: bool,
52
53    /// Fix only the specified bench
54    #[arg(long, value_name = "NAME", help_heading = "Target Selection")]
55    bench: Option<String>,
56
57    /// Fix all targets
58    #[arg(long, help_heading = "Target Selection")]
59    all_targets: bool,
60
61    /// Space or comma separated list of features to activate
62    #[arg(
63        short = 'F',
64        long,
65        value_name = "FEATURES",
66        help_heading = "Feature Selection"
67    )]
68    features: Vec<String>,
69
70    /// Activate all available features
71    #[arg(long, help_heading = "Feature Selection")]
72    all_features: bool,
73
74    /// Do not activate the `default` feature
75    #[arg(long, help_heading = "Feature Selection")]
76    no_default_features: bool,
77
78    /// Unstable (nightly-only) flags
79    #[arg(short = 'Z', value_name = "FLAG")]
80    unstable_flags: Vec<String>,
81
82    /// Number of parallel jobs, defaults to # of CPUs.
83    #[arg(long, value_name = "N", help_heading = "Compilation Options")]
84    jobs: Option<usize>,
85
86    /// Fix artifacts in release mode, with optimizations
87    #[arg(long, help_heading = "Compilation Options")]
88    release: bool,
89
90    /// Build artifacts with the specified profile
91    #[arg(
92        long,
93        value_name = "PROFILE-NAME",
94        help_heading = "Compilation Options"
95    )]
96    profile: Option<String>,
97
98    /// Fix for the target triple
99    #[arg(long, value_name = "TRIPLE", help_heading = "Compilation Options")]
100    target: Vec<String>,
101
102    /// Directory for all generated artifacts
103    #[arg(long, value_name = "DIRECTORY", help_heading = "Compilation Options")]
104    target_dir: Option<String>,
105
106    /// Path to Cargo.toml
107    #[arg(long, value_name = "PATH", help_heading = "Manifest Options")]
108    manifest_path: Option<String>,
109
110    /// Path to Cargo.lock (unstable)
111    #[arg(long, value_name = "PATH", help_heading = "Manifest Options")]
112    lockfile_path: Option<String>,
113
114    /// Ignore `rust-version` specification in packages
115    #[arg(long, help_heading = "Manifest Options")]
116    ignore_rust_version: bool,
117
118    /// Assert that `Cargo.lock` will remain unchanged
119    #[arg(long, help_heading = "Manifest Options")]
120    locked: bool,
121
122    /// Run without accessing the network
123    #[arg(long, help_heading = "Manifest Options")]
124    offline: bool,
125
126    /// Equivalent to specifying both --locked and --offline
127    #[arg(long, help_heading = "Manifest Options")]
128    frozen: bool,
129}
130
131impl CheckFlags {
132    pub fn to_flags(&self) -> Vec<String> {
133        let mut out = Vec::new();
134
135        for spec in self.package.clone() {
136            out.push("--package".to_owned());
137            out.push(spec);
138        }
139        if self.workspace {
140            out.push("--workspace".to_owned());
141        }
142        for spec in self.exclude.clone() {
143            out.push("--exclude".to_owned());
144            out.push(spec);
145        }
146        if self.all {
147            out.push("--all".to_owned());
148        }
149
150        if self.lib {
151            out.push("--lib".to_owned());
152        }
153
154        if self.bins {
155            out.push("--bins".to_owned());
156        }
157        if let Some(b) = self.bin.clone() {
158            out.push("--bin".to_owned());
159            out.push(b);
160        }
161
162        if self.examples {
163            out.push("--examples".to_owned());
164        }
165        if let Some(b) = self.example.clone() {
166            out.push("--example".to_owned());
167            out.push(b);
168        }
169
170        if self.tests {
171            out.push("--tests".to_owned());
172        }
173        if let Some(b) = self.test.clone() {
174            out.push("--test".to_owned());
175            out.push(b);
176        }
177
178        if self.benches {
179            out.push("--benches".to_owned());
180        }
181        if let Some(b) = self.bench.clone() {
182            out.push("--bench".to_owned());
183            out.push(b);
184        }
185
186        if self.all_targets {
187            out.push("--all-targets".to_owned());
188        }
189
190        for i in self.features.clone() {
191            out.push("--features".to_owned());
192            out.push(i);
193        }
194        if self.all_features {
195            out.push("--all-features".to_owned());
196        }
197        if self.no_default_features {
198            out.push("--no-default-features".to_owned());
199        }
200
201        for i in self.unstable_flags.clone() {
202            out.push("-Z".to_owned());
203            out.push(i);
204        }
205
206        if let Some(b) = self.jobs {
207            out.push("--jobs".to_owned());
208            out.push(b.to_string());
209        }
210        if self.release {
211            out.push("--release".to_owned());
212        }
213        if let Some(b) = self.profile.clone() {
214            out.push("--profile".to_owned());
215            out.push(b);
216        }
217
218        for spec in self.target.clone() {
219            out.push("--target".to_owned());
220            out.push(spec);
221        }
222        if let Some(b) = self.target_dir.clone() {
223            out.push("--target-dir".to_owned());
224            out.push(b);
225        }
226
227        if let Some(b) = self.manifest_path.clone() {
228            out.push("--manifest-path".to_owned());
229            out.push(b);
230        }
231        if let Some(b) = self.lockfile_path.clone() {
232            out.push("--lockfile-path".to_owned());
233            out.push(b);
234        }
235        if self.ignore_rust_version {
236            out.push("--ignore-rust-version".to_owned());
237        }
238        if self.locked {
239            out.push("--locked".to_owned());
240        }
241        if self.offline {
242            out.push("--offline".to_owned());
243        }
244        if self.frozen {
245            out.push("--frozen".to_owned());
246        }
247        out
248    }
249}