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(long = "nW", default_value_t = false, help = "Disable window popups.")]
179 pub no_window: bool,
180
181 #[arg(last = true, help = "Additional arguments passed to the command.")]
182 pub extra: Vec<String>,
183}
184
185pub fn print_version_and_features() {
187 let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
189
190 let mut features = Vec::new();
193
194 if cfg!(feature = "tui") {
195 features.push("tui");
196 } else {
197 features.push("!tui");
198 }
199 if cfg!(feature = "concurrent") {
200 features.push("concurrent");
201 } else {
202 features.push("!concurrent");
203 }
204 if cfg!(target_os = "windows") {
205 features.push("windows");
206 } else {
207 features.push("!windows");
208 }
209 if cfg!(feature = "equivalent") {
210 features.push("equivalent");
211 } else {
212 features.push("!equivalent");
213 }
214
215 let json_features = format!(
216 "[{}]",
217 features
218 .iter()
219 .map(|f| format!("\"{}\"", f))
220 .collect::<Vec<String>>()
221 .join(", ")
222 );
223 println!("cargo-e {}", version);
224 println!("{}", json_features);
225 std::process::exit(0);
226}
227
228pub fn get_feature_flags() -> Vec<&'static str> {
231 let mut flags = Vec::new();
232 if cfg!(feature = "tui") {
233 flags.push("tui");
234 } else {
235 flags.push("!tui");
236 }
237 if cfg!(feature = "concurrent") {
238 flags.push("concurrent");
239 } else {
240 flags.push("!concurrent");
241 }
242 if cfg!(target_os = "windows") {
243 flags.push("windows");
244 } else {
245 flags.push("!windows");
246 }
247 if cfg!(feature = "equivalent") {
248 flags.push("equivalent");
249 } else {
250 flags.push("!equivalent");
251 }
252 flags
253}
254
255use std::str::FromStr;
256
257#[derive(Debug, Clone, PartialEq, Default)]
259pub enum RunAll {
260 #[default]
262 NotSpecified,
263 Forever,
265 Timeout(u64),
267}
268
269impl FromStr for RunAll {
270 type Err = String;
271 fn from_str(s: &str) -> Result<Self, Self::Err> {
272 if s.is_empty() {
274 Ok(RunAll::Forever)
275 } else if s.eq_ignore_ascii_case("not_specified") {
276 Ok(RunAll::NotSpecified)
277 } else {
278 s.parse::<u64>()
280 .map(RunAll::Timeout)
281 .map_err(|e| e.to_string())
282 }
283 }
284}
285
286impl std::fmt::Display for RunAll {
287 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
288 match self {
289 RunAll::NotSpecified => write!(f, "not_specified"),
290 RunAll::Forever => write!(f, "forever"),
291 RunAll::Timeout(secs) => write!(f, "{}", secs),
292 }
293 }
294}
295
296pub fn custom_cli(args: &mut Vec<String>) -> (Option<usize>, Vec<&String>) {
297 if args.len() > 1 && args[1].as_str() == "e" {
299 args.remove(1);
300 }
301 let mut run_at_a_time: Option<usize> = None;
302 let mut filtered_args = vec![];
304 for arg in &*args {
305 if let Some(num) = arg
306 .strip_prefix("--run-")
307 .and_then(|s| s.strip_suffix("-at-a-time"))
308 {
309 if let Ok(n) = num.parse() {
310 println!("run-at-a-time: {}", n);
311 run_at_a_time = Some(n);
312 }
313 continue;
315 }
316 filtered_args.push(arg);
317 }
318 (run_at_a_time, filtered_args)
319}