with_examples/
main.rs

1mod examples;
2
3use {
4    clap::{CommandFactory, Parser, ValueEnum},
5    examples::*,
6};
7
8static INTRO_TEMPLATE: &str = "
9Compute `height x width`
10";
11
12static EXAMPLES_TEMPLATE: &str = "
13**Examples:**
14
15${examples
16**${example-number})** ${example-title}: `${example-cmd}`
17${example-comments}
18}
19";
20
21/// Application launch arguments
22#[derive(Parser, Debug)]
23#[command(name = "withex", author, version, about, disable_help_flag = true)]
24struct Args {
25    /// Print help
26    #[arg(long)]
27    help: bool,
28
29    /// Only use ASCII characters
30    #[arg(long)]
31    ascii: bool,
32
33    /// Height, that is the distance between bottom and top
34    #[arg(short, long, default_value = "9")]
35    height: u16,
36
37    /// Width, from there, to there, eg `4` or `5`
38    #[arg(short, long, default_value = "3")]
39    width: u16,
40
41    /// Computation strategy
42    #[arg(long, default_value = "fast")]
43    strategy: Strategy,
44
45    /// Root Directory
46    pub root: Option<std::path::PathBuf>,
47}
48
49#[derive(ValueEnum, Clone, Copy, Debug)]
50enum Strategy {
51    Fast,
52    Precise,
53}
54
55pub fn print_help() {
56    let args = Args::parse();
57    let mut printer = clap_help::Printer::new(Args::command())
58        .with("introduction", INTRO_TEMPLATE)
59        .without("author");
60    if args.ascii {
61        printer.skin_mut().limit_to_ascii();
62    }
63    printer.template_keys_mut().push("examples");
64    printer.set_template("examples", EXAMPLES_TEMPLATE);
65    for (i, example) in EXAMPLES.iter().enumerate() {
66        printer
67            .expander_mut()
68            .sub("examples")
69            .set("example-number", i + 1)
70            .set("example-title", example.title)
71            .set("example-cmd", example.cmd)
72            .set_md("example-comments", example.comments);
73    }
74    printer.print_help();
75}
76
77fn main() {
78    let args = Args::parse();
79
80    if args.help {
81        print_help();
82        return;
83    }
84
85    let (w, h) = (args.width, args.height);
86    println!("Computation strategy: {:?}", args.strategy);
87    println!("{w} x {h} = {}", w * h);
88}