openweather_cli/
cli.rs

1use crate::config::{Config, GeometryMode};
2use clap::{Args, Parser, Subcommand};
3
4/// A command line dictionary
5#[derive(Parser, Debug)]
6#[clap(version, about)]
7pub struct Cli {
8    #[clap(subcommand)]
9    pub commands: Commands,
10}
11
12impl Cli {
13    pub fn new() -> Commands {
14        Self::parse().commands
15    }
16}
17
18#[derive(Subcommand, Debug)]
19pub enum Commands {
20    /// Query weather
21    #[clap(aliases = &["q"])]
22    Query(QueryArgs),
23    // Query(QueryArgs),
24    /// Edit the configuration file
25    #[clap(aliases = &["e", "config", "init"])]
26    Edit,
27}
28
29#[derive(Args, Debug)]
30pub struct QueryArgs {
31    /// Api key (should be set in config file)
32    #[clap(value_parser, long)]
33    pub api_key: Option<String>,
34    /// Geometry mode (should be set in config file)
35    #[clap(value_parser, long)]
36    pub mode: Option<GeometryMode>,
37    /// Include minutely
38    #[clap(value_parser, long)]
39    pub minutely: Option<bool>,
40    /// Include hourly
41    #[clap(value_parser, long)]
42    pub hourly: Option<bool>,
43    /// Include daily
44    #[clap(value_parser, long)]
45    pub daily: Option<bool>,
46}
47
48impl QueryArgs {
49    pub fn update_config(self, config: &mut Config) {
50        if let Some(key) = self.api_key {
51            log::warn!("Api key should be set in config file");
52            config.api_key = key;
53        }
54        if let Some(mode) = self.mode {
55            log::warn!("Geometry mode should be set in config file");
56            config.geometry_mode = mode;
57        }
58        if let Some(b) = self.minutely {
59            config.minutely = b;
60        }
61        if let Some(b) = self.hourly {
62            config.hourly = b;
63        }
64        if let Some(b) = self.daily {
65            config.daily = b;
66        }
67    }
68}