Skip to main content

sonos_cli/cli/
mod.rs

1//! CLI command parsing, helpers, and command execution.
2
3mod commands;
4mod format;
5mod parse;
6mod resolve;
7mod run;
8
9pub use commands::*;
10pub use format::*;
11pub use parse::*;
12pub use resolve::*;
13pub use run::run_command;
14
15use clap::{Args, Parser};
16
17/// Top-level CLI parser.
18#[derive(Debug, Parser)]
19#[command(name = "sonos", about = "Control Sonos speakers", version)]
20pub struct Cli {
21    #[command(subcommand)]
22    pub command: Option<Commands>,
23    #[command(flatten)]
24    pub global: GlobalFlags,
25}
26
27/// Global flags accepted by every command.
28///
29/// Implements `Default` so tests can name only the flags they care about; the
30/// clap-facing defaults and the `Default` impl are the same values.
31#[derive(Debug, Default, Args)]
32pub struct GlobalFlags {
33    /// Target a specific speaker by friendly name
34    #[arg(long, short = 's', global = true)]
35    pub speaker: Option<String>,
36    /// Target a group by name
37    #[arg(long, short = 'g', global = true)]
38    pub group: Option<String>,
39    /// Suppress all non-error stdout output
40    #[arg(long, short, global = true)]
41    pub quiet: bool,
42    /// Increase log verbosity (-v info, -vv debug, -vvv trace)
43    #[arg(short = 'v', long = "verbose", action = clap::ArgAction::Count, global = true)]
44    pub verbose: u8,
45    /// Disable all interactive prompts
46    #[arg(long, global = true)]
47    pub no_input: bool,
48    /// Accept cached data when no speaker responds (exit 0, no warning banner)
49    #[arg(long, global = true, conflicts_with = "require_live")]
50    pub offline: bool,
51    /// Fail with no output unless a speaker was actually reached
52    #[arg(long, global = true)]
53    pub require_live: bool,
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use clap::Parser;
60
61    fn parse(args: &[&str]) -> Cli {
62        Cli::try_parse_from(args).expect("should parse")
63    }
64
65    #[test]
66    fn liveness_flags_default_to_off() {
67        let cli = parse(&["sonos", "speakers"]);
68        assert!(!cli.global.offline);
69        assert!(!cli.global.require_live);
70    }
71
72    #[test]
73    fn liveness_flags_are_global() {
74        // `global = true` means they may follow the subcommand, which is where
75        // people actually type them.
76        assert!(parse(&["sonos", "speakers", "--offline"]).global.offline);
77        assert!(parse(&["sonos", "--offline", "speakers"]).global.offline);
78        assert!(
79            parse(&["sonos", "status", "--require-live"])
80                .global
81                .require_live
82        );
83    }
84
85    /// Asking to accept unverified data and to refuse it is not a coherent
86    /// request; clap rejects it as a usage error (exit 2) rather than letting
87    /// one silently win.
88    #[test]
89    fn offline_and_require_live_conflict() {
90        let err = Cli::try_parse_from(["sonos", "speakers", "--offline", "--require-live"])
91            .expect_err("conflicting flags must be rejected");
92        assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
93    }
94
95    #[test]
96    fn global_flags_default_matches_clap_default() {
97        let parsed = parse(&["sonos", "speakers"]).global;
98        let defaulted = GlobalFlags::default();
99        assert_eq!(parsed.speaker, defaulted.speaker);
100        assert_eq!(parsed.group, defaulted.group);
101        assert_eq!(parsed.quiet, defaulted.quiet);
102        assert_eq!(parsed.verbose, defaulted.verbose);
103        assert_eq!(parsed.no_input, defaulted.no_input);
104        assert_eq!(parsed.offline, defaulted.offline);
105        assert_eq!(parsed.require_live, defaulted.require_live);
106    }
107}