cargo_e/
e_cli.rs

1use clap::Parser;
2
3#[derive(Parser, Debug)]
4#[command(author,version, about = "cargo-e is for Example.", long_about = None)]
5#[command(disable_version_flag = true)]
6pub struct Cli {
7    /// Print version and feature flags in JSON format.
8    #[arg(
9        long,
10        short = 'v',
11        help = "Print version and feature flags in JSON format."
12    )]
13    pub version: bool,
14
15    #[arg(
16        long,
17        short = 't',
18        help = "Launch the text-based user interface (TUI)."
19    )]
20    pub tui: bool,
21
22    #[arg(long, short = 'w', help = "Operate on the entire workspace.")]
23    pub workspace: bool,
24
25    /// Print the exit code of the process when run.
26    #[arg(
27        long = "pX",
28        default_value_t = false,
29        value_parser = clap::value_parser!(bool),
30        help = "Print the exit code of the process when run. (default: false)"
31    )]
32    pub print_exit_code: bool,
33
34    /// Print the program name before execution.
35    #[arg(
36        long = "pN",
37        default_value_t = false,
38        value_parser = clap::value_parser!(bool),
39        help = "Print the program name before execution. (default: false)"
40    )]
41    pub print_program_name: bool,
42
43    /// Print the program name before execution.
44    #[arg(
45        long = "pI",
46        default_value_t = true,
47        value_parser = clap::value_parser!(bool),
48        help = "Print the user instruction. (default: true)"
49    )]
50    pub print_instruction: bool,
51
52    #[arg(
53        long,
54        short = 'p',
55        default_value_t = true,
56        help = "Enable or disable paging (default: enabled)."
57    )]
58    pub paging: bool,
59
60    #[arg(
61        long = "wait",
62        short = 'W',
63        default_value_t = 5,
64        help = "Set wait time in seconds (default: 5)."
65    )]
66    pub wait: u64,
67
68    #[arg(help = "Specify an explicit example to run.")]
69    pub explicit_example: Option<String>,
70
71    #[arg(last = true, help = "Additional arguments passed to the command.")]
72    pub extra: Vec<String>,
73}
74
75/// Print the version and the JSON array of feature flags.
76pub fn print_version_and_features() {
77    // Print the version string.
78    let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
79
80    // Build a list of feature flags. Enabled features are printed normally,
81    // while disabled features are prefixed with an exclamation mark.
82    let mut features = Vec::new();
83
84    if cfg!(feature = "tui") {
85        features.push("tui");
86    } else {
87        features.push("!tui");
88    }
89    if cfg!(feature = "concurrent") {
90        features.push("concurrent");
91    } else {
92        features.push("!concurrent");
93    }
94    if cfg!(feature = "windows") {
95        features.push("windows");
96    } else {
97        features.push("!windows");
98    }
99    if cfg!(feature = "equivalent") {
100        features.push("equivalent");
101    } else {
102        features.push("!equivalent");
103    }
104
105    let json_features = format!(
106        "[{}]",
107        features
108            .iter()
109            .map(|f| format!("\"{}\"", f))
110            .collect::<Vec<String>>()
111            .join(", ")
112    );
113    println!("cargo-e {}", version);
114    println!("{}", json_features);
115    std::process::exit(0);
116}
117
118/// Returns a vector of feature flag strings.  
119/// Enabled features are listed as-is while disabled ones are prefixed with "!".
120pub fn get_feature_flags() -> Vec<&'static str> {
121    let mut flags = Vec::new();
122    if cfg!(feature = "tui") {
123        flags.push("tui");
124    } else {
125        flags.push("!tui");
126    }
127    if cfg!(feature = "concurrent") {
128        flags.push("concurrent");
129    } else {
130        flags.push("!concurrent");
131    }
132    if cfg!(feature = "windows") {
133        flags.push("windows");
134    } else {
135        flags.push("!windows");
136    }
137    if cfg!(feature = "equivalent") {
138        flags.push("equivalent");
139    } else {
140        flags.push("!equivalent");
141    }
142    flags
143}