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
66 #[arg(
68 long = "filter",
69 short = 'f',
70 help = "Enable passthrough mode. No cargo output is filtered, and stdout is captured."
71 )]
72 pub filter: bool,
73 #[arg(
75 long,
76 short = 'v',
77 help = "Print version and feature flags in JSON format."
78 )]
79 pub version: bool,
80
81 #[arg(
82 long,
83 short = 't',
84 help = "Launch the text-based user interface (TUI)."
85 )]
86 pub tui: bool,
87
88 #[arg(long, short = 'w', help = "Operate on the entire workspace.")]
89 pub workspace: bool,
90
91 #[arg(
93 long = "pX",
94 default_value_t = false,
95 value_parser = clap::value_parser!(bool),
96 help = "Print the exit code of the process when run. (default: false)"
97 )]
98 pub print_exit_code: bool,
99
100 #[arg(
102 long = "pN",
103 default_value_t = false,
104 value_parser = clap::value_parser!(bool),
105 help = "Print the program name before execution. (default: false)"
106 )]
107 pub print_program_name: bool,
108
109 #[arg(
111 long = "pI",
112 default_value_t = true,
113 value_parser = clap::value_parser!(bool),
114 help = "Print the user instruction. (default: true)"
115 )]
116 pub print_instruction: bool,
117
118 #[arg(
119 long,
120 short = 'p',
121 default_value_t = true,
122 help = "Enable or disable paging (default: enabled)."
123 )]
124 pub paging: bool,
125
126 #[arg(
127 long,
128 short = 'r',
129 default_value_t = false,
130 help = "Relative numbers (default: enabled)."
131 )]
132 pub relative_numbers: bool,
133
134 #[arg(
135 long = "wait",
136 short = 'W',
137 default_value_t = 15,
138 help = "Set wait time in seconds (default: 15)."
139 )]
140 pub wait: u64,
141
142 #[arg(
144 long = "subcommand",
145 short = 's',
146 value_parser,
147 default_value = "run",
148 help = "Specify subcommands (e.g., `build|b`, `test|t`)."
149 )]
150 pub subcommand: String,
151
152 #[arg(help = "Specify an explicit target to run.")]
153 pub explicit_example: Option<String>,
154
155 #[arg(
156 long = "run-at-a-time",
157 short = 'J',
158 default_value_t = 1,
159 value_parser = clap::value_parser!(usize),
160 help = "Number of targets to run at a time in --run-all mode (--run-at-a-time)"
161 )]
162 pub run_at_a_time: usize,
163
164 #[arg(
165 long = "nS",
166 default_value_t = false,
167 help = "Disable status lines during runtime loop output."
168 )]
169 pub no_status_lines: bool,
170
171 #[arg(
172 long = "nT",
173 default_value_t = false,
174 help = "Disable text-to-speech output."
175 )]
176 pub no_tts: bool,
177
178 #[arg(
180 long = "parse-available",
181 help = "Parse available targets from stdin (one per line)."
182 )]
183 pub parse_available: bool,
184
185 #[arg(
186 long = "default-binary-is-runner",
187 default_value_t = false,
188 help = "If enabled, treat the default binary as the runner for targets."
189 )]
190 pub default_binary_is_runner: bool,
191
192 #[arg(long = "nW", default_value_t = false, help = "Disable window popups.")]
193 pub no_window: bool,
194
195 #[arg(last = true, help = "Additional arguments passed to the command.")]
196 pub extra: Vec<String>,
197}
198
199pub fn print_version_and_features() {
201 let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
203
204 let mut features = Vec::new();
207
208 if cfg!(feature = "tui") {
209 features.push("tui");
210 } else {
211 features.push("!tui");
212 }
213 if cfg!(feature = "concurrent") {
214 features.push("concurrent");
215 } else {
216 features.push("!concurrent");
217 }
218 if cfg!(target_os = "windows") {
219 features.push("windows");
220 } else {
221 features.push("!windows");
222 }
223 if cfg!(feature = "equivalent") {
224 features.push("equivalent");
225 } else {
226 features.push("!equivalent");
227 }
228
229 let json_features = format!(
230 "[{}]",
231 features
232 .iter()
233 .map(|f| format!("\"{}\"", f))
234 .collect::<Vec<String>>()
235 .join(", ")
236 );
237 println!("cargo-e {}", version);
238 println!("{}", json_features);
239 std::process::exit(0);
240}
241
242pub fn get_feature_flags() -> Vec<&'static str> {
245 let mut flags = Vec::new();
246 if cfg!(feature = "tui") {
247 flags.push("tui");
248 } else {
249 flags.push("!tui");
250 }
251 if cfg!(feature = "concurrent") {
252 flags.push("concurrent");
253 } else {
254 flags.push("!concurrent");
255 }
256 if cfg!(target_os = "windows") {
257 flags.push("windows");
258 } else {
259 flags.push("!windows");
260 }
261 if cfg!(feature = "equivalent") {
262 flags.push("equivalent");
263 } else {
264 flags.push("!equivalent");
265 }
266 flags
267}
268
269use std::str::FromStr;
270
271#[derive(Debug, Clone, PartialEq, Default)]
273pub enum RunAll {
274 #[default]
276 NotSpecified,
277 Forever,
279 Timeout(u64),
281}
282
283impl FromStr for RunAll {
284 type Err = String;
285 fn from_str(s: &str) -> Result<Self, Self::Err> {
286 if s.is_empty() {
288 Ok(RunAll::Forever)
289 } else if s.eq_ignore_ascii_case("not_specified") {
290 Ok(RunAll::NotSpecified)
291 } else {
292 s.parse::<u64>()
294 .map(RunAll::Timeout)
295 .map_err(|e| e.to_string())
296 }
297 }
298}
299
300impl std::fmt::Display for RunAll {
301 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
302 match self {
303 RunAll::NotSpecified => write!(f, "not_specified"),
304 RunAll::Forever => write!(f, "forever"),
305 RunAll::Timeout(secs) => write!(f, "{}", secs),
306 }
307 }
308}
309
310pub fn custom_cli(args: &mut Vec<String>) -> (Option<usize>, Vec<&String>) {
311 if args.len() > 1 && args[1].as_str() == "e" {
313 args.remove(1);
314 }
315 let mut run_at_a_time: Option<usize> = None;
316 let mut filtered_args = vec![];
318 for arg in &*args {
319 if let Some(num) = arg
320 .strip_prefix("--run-")
321 .and_then(|s| s.strip_suffix("-at-a-time"))
322 {
323 if let Ok(n) = num.parse() {
324 println!("run-at-a-time: {}", n);
325 run_at_a_time = Some(n);
326 }
327 continue;
329 }
330 filtered_args.push(arg);
331 }
332 (run_at_a_time, filtered_args)
333}