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(long, short = 'v')]
9    pub version: bool,
10
11    #[arg(long, short = 't')]
12    pub tui: bool,
13
14    #[arg(long, short = 'w')]
15    pub workspace: bool,
16
17    #[arg(long = "wait", short = 'W', default_value_t = 2)]
18    pub wait: u64,
19
20    pub explicit_example: Option<String>,
21
22    #[arg(last = true)]
23    pub extra: Vec<String>,
24}
25
26/// Print the version and the JSON array of feature flags.
27pub fn print_version_and_features() {
28    // Print the version string.
29    let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown");
30
31    // Build a list of feature flags. Enabled features are printed normally,
32    // while disabled features are prefixed with an exclamation mark.
33    let mut features = Vec::new();
34
35    if cfg!(feature = "tui") {
36        features.push("tui");
37    } else {
38        features.push("!tui");
39    }
40    if cfg!(feature = "concurrent") {
41        features.push("concurrent");
42    } else {
43        features.push("!concurrent");
44    }
45    if cfg!(feature = "windows") {
46        features.push("windows");
47    } else {
48        features.push("!windows");
49    }
50    if cfg!(feature = "equivalent") {
51        features.push("equivalent");
52    } else {
53        features.push("!equivalent");
54    }
55
56    let json_features = format!(
57        "[{}]",
58        features
59            .iter()
60            .map(|f| format!("\"{}\"", f))
61            .collect::<Vec<String>>()
62            .join(", ")
63    );
64    println!("cargo-e {}", version);
65    println!("{}", json_features);
66    std::process::exit(0);
67}
68
69/// Returns a vector of feature flag strings.  
70/// Enabled features are listed as-is while disabled ones are prefixed with "!".
71pub fn get_feature_flags() -> Vec<&'static str> {
72    let mut flags = Vec::new();
73    if cfg!(feature = "tui") {
74        flags.push("tui");
75    } else {
76        flags.push("!tui");
77    }
78    if cfg!(feature = "concurrent") {
79        flags.push("concurrent");
80    } else {
81        flags.push("!concurrent");
82    }
83    if cfg!(feature = "windows") {
84        flags.push("windows");
85    } else {
86        flags.push("!windows");
87    }
88    if cfg!(feature = "equivalent") {
89        flags.push("equivalent");
90    } else {
91        flags.push("!equivalent");
92    }
93    flags
94}