Skip to main content

claude_wrapper/command/
agents.rs

1use crate::Claude;
2use crate::command::ClaudeCommand;
3use crate::error::Result;
4use crate::exec::{self, CommandOutput};
5
6/// List configured agents.
7///
8/// # Example
9///
10/// ```no_run
11/// use claude_wrapper::{Claude, ClaudeCommand, AgentsCommand};
12///
13/// # async fn example() -> claude_wrapper::Result<()> {
14/// let claude = Claude::builder().build()?;
15/// let output = AgentsCommand::new()
16///     .setting_sources("user,project")
17///     .execute(&claude)
18///     .await?;
19/// println!("{}", output.stdout);
20/// # Ok(())
21/// # }
22/// ```
23#[derive(Debug, Clone, Default)]
24pub struct AgentsCommand {
25    setting_sources: Option<String>,
26    json: bool,
27}
28
29impl AgentsCommand {
30    #[must_use]
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Set which setting sources to load (comma-separated: user, project, local).
36    #[must_use]
37    pub fn setting_sources(mut self, sources: impl Into<String>) -> Self {
38        self.setting_sources = Some(sources.into());
39        self
40    }
41
42    /// Output as JSON.
43    #[must_use]
44    pub fn json(mut self) -> Self {
45        self.json = true;
46        self
47    }
48}
49
50impl ClaudeCommand for AgentsCommand {
51    type Output = CommandOutput;
52
53    fn args(&self) -> Vec<String> {
54        let mut args = vec!["agents".to_string()];
55        if self.json {
56            args.push("--json".to_string());
57        }
58        if let Some(ref sources) = self.setting_sources {
59            args.push("--setting-sources".to_string());
60            args.push(sources.clone());
61        }
62        args
63    }
64
65    async fn execute(&self, claude: &Claude) -> Result<CommandOutput> {
66        exec::run_claude(claude, self.args()).await
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use crate::command::ClaudeCommand;
74
75    #[test]
76    fn test_agents_default() {
77        let cmd = AgentsCommand::new();
78        assert_eq!(ClaudeCommand::args(&cmd), vec!["agents"]);
79    }
80
81    #[test]
82    fn test_agents_with_sources() {
83        let cmd = AgentsCommand::new().setting_sources("user,project");
84        assert_eq!(
85            ClaudeCommand::args(&cmd),
86            vec!["agents", "--setting-sources", "user,project"]
87        );
88    }
89
90    #[test]
91    fn test_agents_json_flag() {
92        let cmd = AgentsCommand::new().json();
93        assert_eq!(ClaudeCommand::args(&cmd), vec!["agents", "--json"]);
94    }
95}