Skip to main content

sonos_cli/cli/
mod.rs

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