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