1use clap::Parser;
2
3#[derive(Parser, Debug, Clone)]
4#[command(author, version, about = "cargo-e is for Example.", long_about = None)]
5#[command(disable_version_flag = true)]
6pub struct Cli {
7 #[arg(
11 long,
12 value_name = "PATH",
13 help = "Path to read/write the stdout of the executed command."
14 )]
15 pub stdout: Option<std::path::PathBuf>,
16
17 #[arg(
19 long,
20 value_name = "PATH",
21 help = "Path to read/write the stderr of the executed command."
22 )]
23 pub stderr: Option<std::path::PathBuf>,
24 #[arg(
29 long,
30 num_args = 0..=1,
31 default_value_t = RunAll::NotSpecified,
32 default_missing_value ="",
33 value_parser,
34 help = "Run all optionally specifying run time (in seconds) per target. \
35 If the flag is present without a value, run forever."
36 )]
37 pub run_all: RunAll,
38
39 #[arg(long, help = "Create GIST run_report.md on exit.")]
40 pub gist: bool,
41 #[arg(long, help = "Build and run in release mode.")]
42 pub release: bool,
43 #[arg(
44 long,
45 short = 'q',
46 help = "Suppress cargo output when running the sample."
47 )]
48 pub quiet: bool,
49 #[clap(
54 long,
55 help = "If enabled, pre-build the examples before executing them."
56 )]
57 pub pre_build: bool,
58
59 #[clap(
60 long,
61 default_value_t = false,
62 help = "If enabled, execute the existing target directly."
63 )]
64 pub cached: bool,
65 #[arg(
67 long = "scan-dir",
68 value_name = "DIR",
69 help = "Scan the given directory for targets to run."
70 )]
71 pub scan_dir: Option<std::path::PathBuf>,
72 #[arg(
74 long = "filter",
75 short = 'f',
76 help = "Enable filter mode. cargo output is filtered and captured."
77 )]
78 pub filter: bool,
79 #[arg(
81 long,
82 short = 'v',
83 help = "Print version and feature flags in JSON format."
84 )]
85 pub version: bool,
86
87 #[arg(
88 long,
89 short = 't',
90 help = "Launch the text-based user interface (TUI)."
91 )]
92 pub tui: bool,
93
94 #[arg(long, short = 'w', help = "Operate on the entire workspace.")]
95 pub workspace: bool,
96
97 #[arg(
99 long = "pX",
100 default_value_t = false,
101 value_parser = clap::value_parser!(bool),
102 help = "Print the exit code of the process when run. (default: false)"
103 )]
104 pub print_exit_code: bool,
105
106 #[arg(
108 long = "pN",
109 default_value_t = false,
110 value_parser = clap::value_parser!(bool),
111 help = "Print the program name before execution. (default: false)"
112 )]
113 pub print_program_name: bool,
114
115 #[arg(
117 long = "pI",
118 default_value_t = true,
119 value_parser = clap::value_parser!(bool),
120 help = "Print the user instruction. (default: true)"
121 )]
122 pub print_instruction: bool,
123
124 #[arg(
125 long,
126 short = 'p',
127 default_value_t = true,
128 help = "Enable or disable paging (default: enabled)."
129 )]
130 pub paging: bool,
131
132 #[arg(
133 long,
134 short = 'r',
135 default_value_t = false,
136 help = "Relative numbers (default: enabled)."
137 )]
138 pub relative_numbers: bool,
139
140 #[arg(
141 long = "wait",
142 short = 'W',
143 default_value_t = 15,
144 help = "Set wait time in seconds (default: 15)."
145 )]
146 pub wait: u64,
147
148 #[arg(
150 long = "subcommand",
151 short = 's',
152 value_parser,
153 default_value = "run",
154 help = "Specify subcommands (e.g., `build|b`, `test|t`)."
155 )]
156 pub subcommand: String,
157
158 #[arg(help = "Specify an explicit target to run.")]
159 pub explicit_example: Option<String>,
160
161 #[arg(
162 long = "run-at-a-time",
163 short = 'J',
164 default_value_t = 1,
165 value_parser = clap::value_parser!(usize),
166 help = "Number of targets to run at a time in --run-all mode (--run-at-a-time)"
167 )]
168 pub run_at_a_time: usize,
169
170 #[arg(
171 long = "nS",
172 default_value_t = false,
173 help = "Disable status lines during runtime loop output."
174 )]
175 pub no_status_lines: bool,
176
177 #[arg(
178 long = "nT",
179 default_value_t = false,
180 help = "Disable text-to-speech output."
181 )]
182 pub no_tts: bool,
183
184 #[arg(
186 long = "parse-available",
187 help = "Parse available targets from stdin (one per line)."
188 )]
189 pub parse_available: bool,
190
191 #[arg(
192 long = "default-binary-is-runner",
193 default_value_t = false,
194 help = "If enabled, treat the default binary as the runner for targets."
195 )]
196 pub default_binary_is_runner: bool,
197
198 #[arg(long = "nW", default_value_t = false, help = "Disable window popups.")]
199 pub no_window: bool,
200
201 #[arg(
203 long = "log",
204 value_name = "PATH",
205 help = "Enable logging to a file at the given path, or to stdout if not specified."
206 )]
207 pub log: Option<std::path::PathBuf>,
208
209 #[arg(
210 long = "manifest-path",
211 value_name = "PATH",
212 help = "Specify the path to the Cargo.toml manifest file."
213 )]
214 pub manifest_path: Option<std::path::PathBuf>,
215
216 #[arg(
217 long = "target",
218 value_name = "TARGET",
219 help = "Specify the target triple for the build."
220 )]
221 pub target: Option<String>,
222
223 #[arg(
225 long = "json-all-targets",
226 help = "Output the list of all targets as JSON."
227 )]
228 pub json_all_targets: bool,
229
230 #[arg(last = true, help = "Additional arguments passed to the command.")]
231 pub extra: Vec<String>,
232}
233
234pub fn print_version_and_features() {
236 let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
238
239 let mut features = Vec::new();
242
243 if cfg!(feature = "tui") {
244 features.push("tui");
245 } else {
246 features.push("!tui");
247 }
248 if cfg!(feature = "concurrent") {
249 features.push("concurrent");
250 } else {
251 features.push("!concurrent");
252 }
253 if cfg!(target_os = "windows") {
254 features.push("windows");
255 } else {
256 features.push("!windows");
257 }
258 if cfg!(feature = "equivalent") {
259 features.push("equivalent");
260 } else {
261 features.push("!equivalent");
262 }
263
264 let json_features = format!(
265 "[{}]",
266 features
267 .iter()
268 .map(|f| format!("\"{}\"", f))
269 .collect::<Vec<String>>()
270 .join(", ")
271 );
272 println!("cargo-e {}", version);
273 println!("{}", json_features);
274 std::process::exit(0);
275}
276
277pub fn get_feature_flags() -> Vec<&'static str> {
280 let mut flags = Vec::new();
281 if cfg!(feature = "tui") {
282 flags.push("tui");
283 } else {
284 flags.push("!tui");
285 }
286 if cfg!(feature = "concurrent") {
287 flags.push("concurrent");
288 } else {
289 flags.push("!concurrent");
290 }
291 if cfg!(target_os = "windows") {
292 flags.push("windows");
293 } else {
294 flags.push("!windows");
295 }
296 if cfg!(feature = "equivalent") {
297 flags.push("equivalent");
298 } else {
299 flags.push("!equivalent");
300 }
301 flags
302}
303
304use std::str::FromStr;
305
306#[derive(Debug, Clone, PartialEq, Default)]
308pub enum RunAll {
309 #[default]
311 NotSpecified,
312 Forever,
314 Timeout(u64),
316}
317
318impl FromStr for RunAll {
319 type Err = String;
320 fn from_str(s: &str) -> Result<Self, Self::Err> {
321 if s.is_empty() {
323 Ok(RunAll::Forever)
324 } else if s.eq_ignore_ascii_case("not_specified") {
325 Ok(RunAll::NotSpecified)
326 } else {
327 s.parse::<u64>()
329 .map(RunAll::Timeout)
330 .map_err(|e| e.to_string())
331 }
332 }
333}
334
335impl std::fmt::Display for RunAll {
336 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
337 match self {
338 RunAll::NotSpecified => write!(f, "not_specified"),
339 RunAll::Forever => write!(f, "forever"),
340 RunAll::Timeout(secs) => write!(f, "{}", secs),
341 }
342 }
343}
344
345pub fn custom_cli(args: &mut Vec<String>) -> (Option<usize>, Vec<&String>) {
346 if args.len() > 1 && args[1].as_str() == "e" {
348 args.remove(1);
349 }
350 let mut run_at_a_time: Option<usize> = None;
351 let mut filtered_args = vec![];
353 for arg in &*args {
354 if let Some(num) = arg
355 .strip_prefix("--run-")
356 .and_then(|s| s.strip_suffix("-at-a-time"))
357 {
358 if let Ok(n) = num.parse() {
359 println!("run-at-a-time: {}", n);
360 run_at_a_time = Some(n);
361 }
362 continue;
364 }
365 filtered_args.push(arg);
366 }
367 (run_at_a_time, filtered_args)
368}