Skip to main content

weatherapi_rs/files/
cli.rs

1use std::fs::File;
2
3use clap::{crate_version, Args, Command, Parser, ValueEnum};
4
5#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
6pub enum Format {
7    Json,
8    Compact,
9}
10
11#[derive(Parser, Debug)]
12#[command(version = crate_version!(), about = "Weather CLI tool", long_about = None)]
13pub struct Cli {
14    #[arg(
15        short,
16        required_unless_present("usage"),
17        long_help = "City name e.g. London, Paris"
18    )]
19    pub q: Option<String>,
20    #[arg(value_enum, short, long, default_value_t = Format::Compact, long_help = "Output format")]
21    pub format: Format,
22    #[arg(long, long_help = "Include air quality index")]
23    pub aqi: bool,
24    #[arg(
25        long,
26        long_help = "Generate usage file in KDL format (usefull to create auto-completion)"
27    )]
28    pub usage: bool,
29}
30
31impl Cli {
32    pub fn generate_usage() {
33        let cmd = Command::new("weatherapi-rs");
34        let mut cmd = Cli::augment_args(cmd);
35
36        if cfg!(debug_assertions) {
37            let mut file = File::create("docs/usage.spec.kdl").unwrap();
38            clap_usage::generate(&mut cmd, "weatherapi-rs", &mut file);
39        } else {
40            clap_usage::generate(&mut cmd, "weatherapi-rs", &mut std::io::stdout());
41        }
42    }
43}
44
45#[test]
46fn verify_cmd() {
47    let cmd = Command::new("weatherapi-rs");
48    let cmd = Cli::augment_args(cmd);
49
50    cmd.debug_assert();
51}