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,
62        short = 'r',
63        default_value_t = false,
64        help = "Relative numbers (default: enabled)."
65    )]
66    pub relative_numbers: bool,
67
68    #[arg(
69        long = "wait",
70        short = 'W',
71        default_value_t = 5,
72        help = "Set wait time in seconds (default: 5)."
73    )]
74    pub wait: u64,
75
76    #[arg(help = "Specify an explicit example to run.")]
77    pub explicit_example: Option<String>,
78
79    #[arg(last = true, help = "Additional arguments passed to the command.")]
80    pub extra: Vec<String>,
81}
82
83/// Print the version and the JSON array of feature flags.
84pub fn print_version_and_features() {
85    // Print the version string.
86    let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
87
88    // Build a list of feature flags. Enabled features are printed normally,
89    // while disabled features are prefixed with an exclamation mark.
90    let mut features = Vec::new();
91
92    if cfg!(feature = "tui") {
93        features.push("tui");
94    } else {
95        features.push("!tui");
96    }
97    if cfg!(feature = "concurrent") {
98        features.push("concurrent");
99    } else {
100        features.push("!concurrent");
101    }
102    if cfg!(feature = "windows") {
103        features.push("windows");
104    } else {
105        features.push("!windows");
106    }
107    if cfg!(feature = "equivalent") {
108        features.push("equivalent");
109    } else {
110        features.push("!equivalent");
111    }
112
113    let json_features = format!(
114        "[{}]",
115        features
116            .iter()
117            .map(|f| format!("\"{}\"", f))
118            .collect::<Vec<String>>()
119            .join(", ")
120    );
121    println!("cargo-e {}", version);
122    println!("{}", json_features);
123    std::process::exit(0);
124}
125
126/// Returns a vector of feature flag strings.  
127/// Enabled features are listed as-is while disabled ones are prefixed with "!".
128pub fn get_feature_flags() -> Vec<&'static str> {
129    let mut flags = Vec::new();
130    if cfg!(feature = "tui") {
131        flags.push("tui");
132    } else {
133        flags.push("!tui");
134    }
135    if cfg!(feature = "concurrent") {
136        flags.push("concurrent");
137    } else {
138        flags.push("!concurrent");
139    }
140    if cfg!(feature = "windows") {
141        flags.push("windows");
142    } else {
143        flags.push("!windows");
144    }
145    if cfg!(feature = "equivalent") {
146        flags.push("equivalent");
147    } else {
148        flags.push("!equivalent");
149    }
150    flags
151}