claude-wrapper 0.13.3

A type-safe Claude Code CLI wrapper for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Authentication subcommand builders.
//!
//! Builders for the `claude` auth surface: [`AuthStatusCommand`],
//! [`AuthLoginCommand`], [`AuthLogoutCommand`], and
//! [`SetupTokenCommand`]. For detecting which auth strategy the CLI
//! will use without invoking it, see [`crate::auth`].

use crate::Claude;
use crate::command::ClaudeCommand;
use crate::error::Result;
use crate::exec::{self, CommandOutput};

/// Check authentication status.
///
/// # Example
///
/// ```no_run
/// use claude_wrapper::{Claude, ClaudeCommand, AuthStatusCommand};
///
/// # async fn example() -> claude_wrapper::Result<()> {
/// let claude = Claude::builder().build()?;
/// let status = AuthStatusCommand::new().execute_json(&claude).await?;
/// println!("logged in: {}", status.logged_in);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct AuthStatusCommand {
    json: bool,
}

impl AuthStatusCommand {
    /// Create a new auth status command.
    #[must_use]
    pub fn new() -> Self {
        Self { json: true }
    }

    /// Request text output instead of JSON.
    #[must_use]
    pub fn text(mut self) -> Self {
        self.json = false;
        self
    }

    /// Execute and parse the JSON result into an [`AuthStatus`](crate::types::AuthStatus).
    #[cfg(all(feature = "json", feature = "async"))]
    pub async fn execute_json(&self, claude: &Claude) -> Result<crate::types::AuthStatus> {
        let mut cmd = self.clone();
        cmd.json = true;

        let output = exec::run_claude(claude, cmd.args()).await?;

        serde_json::from_str(&output.stdout).map_err(|e| crate::error::Error::Json {
            message: format!("failed to parse auth status: {e}"),
            source: e,
        })
    }

    /// Blocking mirror of [`AuthStatusCommand::execute_json`].
    #[cfg(all(feature = "sync", feature = "json"))]
    pub fn execute_json_sync(&self, claude: &Claude) -> Result<crate::types::AuthStatus> {
        let mut cmd = self.clone();
        cmd.json = true;

        let output = exec::run_claude_sync(claude, cmd.args())?;

        serde_json::from_str(&output.stdout).map_err(|e| crate::error::Error::Json {
            message: format!("failed to parse auth status: {e}"),
            source: e,
        })
    }
}

impl ClaudeCommand for AuthStatusCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["auth".to_string(), "status".to_string()];
        if self.json {
            args.push("--json".to_string());
        } else {
            args.push("--text".to_string());
        }
        args
    }

    #[cfg(feature = "async")]
    async fn execute(&self, claude: &Claude) -> Result<CommandOutput> {
        exec::run_claude(claude, self.args()).await
    }
}

/// Which billing path the CLI should authenticate against.
/// Maps to `--claudeai` (subscription) or `--console` (Anthropic
/// Console / API usage billing) on `claude auth login`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoginMode {
    /// Claude subscription account (the CLI's default if neither
    /// flag is passed; passing this is explicit-form). Maps to
    /// `--claudeai`.
    Claudeai,
    /// Anthropic Console account, billed via API usage. Maps to
    /// `--console`. Required for teams on Console billing -- the
    /// default subscription path will sign them into the wrong
    /// account.
    Console,
}

impl LoginMode {
    fn as_arg(self) -> &'static str {
        match self {
            Self::Claudeai => "--claudeai",
            Self::Console => "--console",
        }
    }
}

/// Authenticate with Claude.
///
/// # Billing mode
///
/// As of Claude Code 2.1.x the CLI supports two billing paths:
/// Claude subscription (`--claudeai`, the default) and Anthropic
/// Console / API usage (`--console`). Use [`Self::mode`] to pin
/// the path explicitly -- Console-billed teams need to, or
/// they'll land in the wrong account on first auth.
///
/// # Example
///
/// ```no_run
/// use claude_wrapper::{Claude, ClaudeCommand, AuthLoginCommand};
/// use claude_wrapper::command::auth::LoginMode;
///
/// # async fn example() -> claude_wrapper::Result<()> {
/// let claude = Claude::builder().build()?;
/// AuthLoginCommand::new()
///     .mode(LoginMode::Console)
///     .email("user@example.com")
///     .execute(&claude)
///     .await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct AuthLoginCommand {
    email: Option<String>,
    mode: Option<LoginMode>,
    force_sso: bool,
    #[deprecated(
        since = "0.10.0",
        note = "the `--sso` flag is a boolean since at least Claude Code 2.1.x; \
                the value passed via the deprecated `.sso(provider)` was being \
                emitted as an extra positional and silently doing the wrong thing. \
                Use `force_sso()` to set the boolean flag instead."
    )]
    legacy_sso_value: Option<String>,
}

impl AuthLoginCommand {
    /// Create a new auth login command.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the email address for authentication.
    #[must_use]
    pub fn email(mut self, email: impl Into<String>) -> Self {
        self.email = Some(email.into());
        self
    }

    /// Pin the billing path (`--claudeai` or `--console`). The CLI
    /// defaults to `Claudeai` when neither flag is passed; setting
    /// this explicitly is the only way Console-billed teams reach
    /// their account.
    #[must_use]
    pub fn mode(mut self, mode: LoginMode) -> Self {
        self.mode = Some(mode);
        self
    }

    /// Force the SSO login flow (`--sso`). Boolean flag with no
    /// value -- replaces the historical [`Self::sso`] which took a
    /// provider name and emitted invalid args (the CLI's `--sso`
    /// has been boolean since at least 2.1.x).
    #[must_use]
    pub fn force_sso(mut self) -> Self {
        self.force_sso = true;
        self
    }

    /// **Deprecated.** Set the SSO provider for authentication.
    ///
    /// The CLI's `--sso` is a boolean flag with no value (since at
    /// least Claude Code 2.1.x). Passing a `provider` string caused
    /// the wrapper to emit `--sso <provider>`, which the CLI parsed
    /// as `--sso` plus an extra positional that was silently
    /// ignored or mishandled. Use [`Self::force_sso`] for the
    /// correct boolean form.
    ///
    /// Kept as a compile-error-and-deprecation-warning bridge so
    /// callers see the change. The value is intentionally ignored
    /// at args() emit time -- only the boolean intent is preserved.
    #[deprecated(
        since = "0.10.0",
        note = "the `--sso` flag is a boolean since at least Claude Code 2.1.x. \
                Use `force_sso()` instead. The value passed here is ignored at \
                emit time; the boolean intent is preserved."
    )]
    #[must_use]
    pub fn sso(mut self, provider: impl Into<String>) -> Self {
        // Honor the boolean intent (caller clearly wanted SSO);
        // record the legacy value purely so the deprecation
        // warning's "this used to break stuff" claim is reproducible
        // by anyone reading the field.
        self.force_sso = true;
        #[allow(deprecated)]
        {
            self.legacy_sso_value = Some(provider.into());
        }
        self
    }
}

impl ClaudeCommand for AuthLoginCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["auth".to_string(), "login".to_string()];
        if let Some(mode) = self.mode {
            args.push(mode.as_arg().to_string());
        }
        if let Some(ref email) = self.email {
            args.push("--email".to_string());
            args.push(email.clone());
        }
        if self.force_sso {
            args.push("--sso".to_string());
        }
        args
    }

    #[cfg(feature = "async")]
    async fn execute(&self, claude: &Claude) -> Result<CommandOutput> {
        exec::run_claude(claude, self.args()).await
    }
}

/// Deauthenticate from Claude.
///
/// # Example
///
/// ```no_run
/// use claude_wrapper::{Claude, ClaudeCommand, AuthLogoutCommand};
///
/// # async fn example() -> claude_wrapper::Result<()> {
/// let claude = Claude::builder().build()?;
/// AuthLogoutCommand::new().execute(&claude).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct AuthLogoutCommand;

impl AuthLogoutCommand {
    /// Create a new auth logout command.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl ClaudeCommand for AuthLogoutCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        vec!["auth".to_string(), "logout".to_string()]
    }

    #[cfg(feature = "async")]
    async fn execute(&self, claude: &Claude) -> Result<CommandOutput> {
        exec::run_claude(claude, self.args()).await
    }
}

/// Set up a long-lived authentication token.
///
/// # Example
///
/// ```no_run
/// use claude_wrapper::{Claude, ClaudeCommand, SetupTokenCommand};
///
/// # async fn example() -> claude_wrapper::Result<()> {
/// let claude = Claude::builder().build()?;
/// SetupTokenCommand::new().execute(&claude).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct SetupTokenCommand;

impl SetupTokenCommand {
    /// Create a new setup-token command.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl ClaudeCommand for SetupTokenCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        vec!["setup-token".to_string()]
    }

    #[cfg(feature = "async")]
    async fn execute(&self, claude: &Claude) -> Result<CommandOutput> {
        exec::run_claude(claude, self.args()).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_auth_status_args() {
        let cmd = AuthStatusCommand::new();
        assert_eq!(cmd.args(), vec!["auth", "status", "--json"]);
    }

    #[test]
    fn test_auth_status_text() {
        let cmd = AuthStatusCommand::new().text();
        assert_eq!(cmd.args(), vec!["auth", "status", "--text"]);
    }

    #[test]
    fn test_auth_login_default() {
        let cmd = AuthLoginCommand::new();
        assert_eq!(cmd.args(), vec!["auth", "login"]);
    }

    #[test]
    fn test_auth_login_with_email() {
        let cmd = AuthLoginCommand::new().email("user@example.com");
        assert_eq!(
            cmd.args(),
            vec!["auth", "login", "--email", "user@example.com"]
        );
    }

    #[test]
    fn test_auth_login_with_force_sso() {
        let cmd = AuthLoginCommand::new().force_sso();
        assert_eq!(cmd.args(), vec!["auth", "login", "--sso"]);
    }

    #[test]
    #[allow(deprecated)]
    fn test_auth_login_deprecated_sso_emits_boolean_only() {
        // Bug fix: the historical `.sso(provider)` would emit
        // `--sso <provider>` but the CLI's `--sso` is boolean. The
        // deprecated method now honors the boolean intent (calls
        // `force_sso` internally) and drops the value at emit time.
        let cmd = AuthLoginCommand::new().sso("okta");
        assert_eq!(cmd.args(), vec!["auth", "login", "--sso"]);
    }

    #[test]
    fn test_auth_login_with_mode_claudeai() {
        let cmd = AuthLoginCommand::new().mode(LoginMode::Claudeai);
        assert_eq!(cmd.args(), vec!["auth", "login", "--claudeai"]);
    }

    #[test]
    fn test_auth_login_with_mode_console() {
        let cmd = AuthLoginCommand::new().mode(LoginMode::Console);
        assert_eq!(cmd.args(), vec!["auth", "login", "--console"]);
    }

    #[test]
    fn test_auth_login_console_with_email() {
        let cmd = AuthLoginCommand::new()
            .mode(LoginMode::Console)
            .email("ops@example.com");
        assert_eq!(
            cmd.args(),
            vec!["auth", "login", "--console", "--email", "ops@example.com"]
        );
    }

    #[test]
    fn test_auth_logout() {
        let cmd = AuthLogoutCommand::new();
        assert_eq!(cmd.args(), vec!["auth", "logout"]);
    }

    #[test]
    fn test_setup_token() {
        let cmd = SetupTokenCommand::new();
        assert_eq!(cmd.args(), vec!["setup-token"]);
    }
}