claude-wrapper 0.11.0

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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#[cfg(feature = "async")]
use crate::Claude;
use crate::command::ClaudeCommand;
#[cfg(feature = "async")]
use crate::error::Result;
#[cfg(feature = "async")]
use crate::exec;
use crate::exec::CommandOutput;
use crate::types::Scope;

/// List installed plugins.
///
/// # Example
///
/// ```no_run
/// use claude_wrapper::{Claude, ClaudeCommand, PluginListCommand};
///
/// # async fn example() -> claude_wrapper::Result<()> {
/// let claude = Claude::builder().build()?;
/// let output = PluginListCommand::new().json().execute(&claude).await?;
/// println!("{}", output.stdout);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct PluginListCommand {
    json: bool,
    available: bool,
}

impl PluginListCommand {
    /// Creates a new plugin list command.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Output as JSON.
    #[must_use]
    pub fn json(mut self) -> Self {
        self.json = true;
        self
    }

    /// Include available plugins from marketplaces (requires `json()`).
    #[must_use]
    pub fn available(mut self) -> Self {
        self.available = true;
        self
    }
}

impl ClaudeCommand for PluginListCommand {
    type Output = CommandOutput;

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

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

/// Install a plugin.
///
/// # Example
///
/// ```no_run
/// use claude_wrapper::{Claude, ClaudeCommand, PluginInstallCommand, Scope};
///
/// # async fn example() -> claude_wrapper::Result<()> {
/// let claude = Claude::builder().build()?;
/// PluginInstallCommand::new("my-plugin")
///     .scope(Scope::User)
///     .execute(&claude)
///     .await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct PluginInstallCommand {
    plugin: String,
    scope: Option<Scope>,
}

impl PluginInstallCommand {
    /// Creates a command to install a plugin by name.
    #[must_use]
    pub fn new(plugin: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            scope: None,
        }
    }

    /// Set the installation scope.
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = Some(scope);
        self
    }
}

impl ClaudeCommand for PluginInstallCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["plugin".to_string(), "install".to_string()];
        if let Some(ref scope) = self.scope {
            args.push("--scope".to_string());
            args.push(scope.as_arg().to_string());
        }
        args.push(self.plugin.clone());
        args
    }

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

/// Uninstall a plugin.
///
/// **Headless callers should pass [`Self::yes`]** -- the underlying
/// CLI requires `-y` whenever stdin/stdout isn't a TTY and will
/// otherwise wait on a prompt that no one is around to answer.
#[derive(Debug, Clone)]
pub struct PluginUninstallCommand {
    plugin: String,
    scope: Option<Scope>,
    keep_data: bool,
    prune: bool,
    yes: bool,
}

impl PluginUninstallCommand {
    /// Creates a command to uninstall a plugin by name.
    #[must_use]
    pub fn new(plugin: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            scope: None,
            keep_data: false,
            prune: false,
            yes: false,
        }
    }

    /// Set the scope.
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = Some(scope);
        self
    }

    /// Preserve the plugin's persistent data directory
    /// (`~/.claude/plugins/data/{id}/`) on uninstall (`--keep-data`).
    /// Default: data is removed alongside the plugin.
    #[must_use]
    pub fn keep_data(mut self) -> Self {
        self.keep_data = true;
        self
    }

    /// Also remove auto-installed dependencies that are no longer
    /// needed (`--prune`). Requires [`Self::yes`] in non-interactive
    /// contexts (which the wrapper always is).
    #[must_use]
    pub fn prune(mut self) -> Self {
        self.prune = true;
        self
    }

    /// Skip the `--prune` confirmation prompt (`-y`). **Required for
    /// non-TTY callers** -- without it, the CLI will hang waiting on
    /// stdin. Every wrapper consumer running under `execute()` is
    /// non-TTY by definition, so you almost always want this on.
    #[must_use]
    pub fn yes(mut self) -> Self {
        self.yes = true;
        self
    }
}

impl ClaudeCommand for PluginUninstallCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["plugin".to_string(), "uninstall".to_string()];
        if let Some(ref scope) = self.scope {
            args.push("--scope".to_string());
            args.push(scope.as_arg().to_string());
        }
        if self.keep_data {
            args.push("--keep-data".to_string());
        }
        if self.prune {
            args.push("--prune".to_string());
        }
        if self.yes {
            args.push("--yes".to_string());
        }
        args.push(self.plugin.clone());
        args
    }

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

/// Enable a disabled plugin.
#[derive(Debug, Clone)]
pub struct PluginEnableCommand {
    plugin: String,
    scope: Option<Scope>,
}

impl PluginEnableCommand {
    /// Creates a command to enable a plugin by name.
    #[must_use]
    pub fn new(plugin: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            scope: None,
        }
    }

    /// Set the scope.
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = Some(scope);
        self
    }
}

impl ClaudeCommand for PluginEnableCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["plugin".to_string(), "enable".to_string()];
        if let Some(ref scope) = self.scope {
            args.push("--scope".to_string());
            args.push(scope.as_arg().to_string());
        }
        args.push(self.plugin.clone());
        args
    }

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

/// Disable an enabled plugin.
#[derive(Debug, Clone)]
pub struct PluginDisableCommand {
    plugin: Option<String>,
    scope: Option<Scope>,
    all: bool,
}

impl PluginDisableCommand {
    /// Creates a command to disable a plugin by name. To disable all plugins, use [`PluginDisableCommand::all`].
    #[must_use]
    pub fn new(plugin: impl Into<String>) -> Self {
        Self {
            plugin: Some(plugin.into()),
            scope: None,
            all: false,
        }
    }

    /// Disable all enabled plugins.
    #[must_use]
    pub fn all() -> Self {
        Self {
            plugin: None,
            scope: None,
            all: true,
        }
    }

    /// Set the scope.
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = Some(scope);
        self
    }
}

impl ClaudeCommand for PluginDisableCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["plugin".to_string(), "disable".to_string()];
        if self.all {
            args.push("--all".to_string());
        }
        if let Some(ref scope) = self.scope {
            args.push("--scope".to_string());
            args.push(scope.as_arg().to_string());
        }
        if let Some(ref plugin) = self.plugin {
            args.push(plugin.clone());
        }
        args
    }

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

/// Update a plugin to the latest version.
#[derive(Debug, Clone)]
pub struct PluginUpdateCommand {
    plugin: String,
    scope: Option<Scope>,
}

impl PluginUpdateCommand {
    /// Creates a command to update a plugin to the latest version.
    #[must_use]
    pub fn new(plugin: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            scope: None,
        }
    }

    /// Set the scope.
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = Some(scope);
        self
    }
}

impl ClaudeCommand for PluginUpdateCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["plugin".to_string(), "update".to_string()];
        if let Some(ref scope) = self.scope {
            args.push("--scope".to_string());
            args.push(scope.as_arg().to_string());
        }
        args.push(self.plugin.clone());
        args
    }

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

/// Validate a plugin or marketplace manifest.
#[derive(Debug, Clone)]
pub struct PluginValidateCommand {
    path: String,
}

impl PluginValidateCommand {
    /// Creates a command to validate a plugin manifest at the given path.
    #[must_use]
    pub fn new(path: impl Into<String>) -> Self {
        Self { path: path.into() }
    }
}

impl ClaudeCommand for PluginValidateCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        vec![
            "plugin".to_string(),
            "validate".to_string(),
            self.path.clone(),
        ]
    }

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

/// Create a `{name}--v{version}` git tag for a plugin release.
///
/// Runs `claude plugin tag [path]`, validating that the plugin's
/// `plugin.json` and any enclosing marketplace entry agree on the
/// version before tagging.
///
/// # Example
///
/// ```no_run
/// # #[cfg(feature = "async")] {
/// use claude_wrapper::{Claude, ClaudeCommand, PluginTagCommand};
///
/// # async fn example() -> claude_wrapper::Result<()> {
/// let claude = Claude::builder().build()?;
/// let out = PluginTagCommand::new()
///     .path("./my-plugin")
///     .message("release %s")
///     .push()
///     .execute(&claude)
///     .await?;
/// println!("{}", out.stdout);
/// # Ok(()) }
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct PluginTagCommand {
    path: Option<String>,
    dry_run: bool,
    force: bool,
    message: Option<String>,
    push: bool,
    remote: Option<String>,
}

impl PluginTagCommand {
    /// Create a new tag command. Without [`path`](Self::path), the CLI
    /// uses the current directory.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Path to the plugin directory.
    #[must_use]
    pub fn path(mut self, path: impl Into<String>) -> Self {
        self.path = Some(path.into());
        self
    }

    /// Print what would be tagged without creating anything.
    #[must_use]
    pub fn dry_run(mut self) -> Self {
        self.dry_run = true;
        self
    }

    /// Skip dirty-working-tree and tag-already-exists checks.
    #[must_use]
    pub fn force(mut self) -> Self {
        self.force = true;
        self
    }

    /// Tag annotation message; `%s` is substituted with the version.
    #[must_use]
    pub fn message(mut self, msg: impl Into<String>) -> Self {
        self.message = Some(msg.into());
        self
    }

    /// Push the tag after creating it.
    #[must_use]
    pub fn push(mut self) -> Self {
        self.push = true;
        self
    }

    /// Override the remote pushed to with [`push`](Self::push) (default `origin`).
    #[must_use]
    pub fn remote(mut self, remote: impl Into<String>) -> Self {
        self.remote = Some(remote.into());
        self
    }
}

impl ClaudeCommand for PluginTagCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["plugin".to_string(), "tag".to_string()];
        if self.dry_run {
            args.push("--dry-run".to_string());
        }
        if self.force {
            args.push("--force".to_string());
        }
        if let Some(ref msg) = self.message {
            args.push("--message".to_string());
            args.push(msg.clone());
        }
        if self.push {
            args.push("--push".to_string());
        }
        if let Some(ref remote) = self.remote {
            args.push("--remote".to_string());
            args.push(remote.clone());
        }
        if let Some(ref path) = self.path {
            args.push(path.clone());
        }
        args
    }

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

/// Show a plugin's component inventory and projected token cost
/// (`claude plugin details <name>`).
#[derive(Debug, Clone)]
pub struct PluginDetailsCommand {
    plugin: String,
}

impl PluginDetailsCommand {
    /// Create a details command for the given plugin name.
    #[must_use]
    pub fn new(plugin: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
        }
    }
}

impl ClaudeCommand for PluginDetailsCommand {
    type Output = CommandOutput;

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

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

/// Remove auto-installed dependencies that are no longer needed
/// (`claude plugin prune` -- alias `autoremove`).
///
/// Non-TTY callers should pass [`Self::yes`] -- the underlying CLI
/// requires `-y` whenever stdin/stdout isn't a TTY and will
/// otherwise wait on a confirmation prompt.
#[derive(Debug, Clone, Default)]
pub struct PluginPruneCommand {
    dry_run: bool,
    scope: Option<Scope>,
    yes: bool,
}

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

    /// Print what would be removed without removing anything
    /// (`--dry-run`).
    #[must_use]
    pub fn dry_run(mut self) -> Self {
        self.dry_run = true;
        self
    }

    /// Set the scope (`-s/--scope`). Default: `user`.
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = Some(scope);
        self
    }

    /// Skip the confirmation prompt (`-y`). **Required for non-TTY
    /// callers** -- without it the CLI will hang waiting on stdin.
    #[must_use]
    pub fn yes(mut self) -> Self {
        self.yes = true;
        self
    }
}

impl ClaudeCommand for PluginPruneCommand {
    type Output = CommandOutput;

    fn args(&self) -> Vec<String> {
        let mut args = vec!["plugin".to_string(), "prune".to_string()];
        if self.dry_run {
            args.push("--dry-run".to_string());
        }
        if let Some(ref scope) = self.scope {
            args.push("--scope".to_string());
            args.push(scope.as_arg().to_string());
        }
        if self.yes {
            args.push("--yes".to_string());
        }
        args
    }

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

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

    #[test]
    fn test_plugin_list() {
        let cmd = PluginListCommand::new().json().available();
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "list", "--json", "--available"]
        );
    }

    #[test]
    fn test_plugin_install() {
        let cmd = PluginInstallCommand::new("my-plugin").scope(Scope::User);
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "install", "--scope", "user", "my-plugin"]
        );
    }

    #[test]
    fn test_plugin_uninstall() {
        let cmd = PluginUninstallCommand::new("old-plugin");
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "uninstall", "old-plugin"]
        );
    }

    #[test]
    fn test_plugin_uninstall_with_all_flags() {
        let cmd = PluginUninstallCommand::new("old-plugin")
            .scope(Scope::User)
            .keep_data()
            .prune()
            .yes();
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec![
                "plugin",
                "uninstall",
                "--scope",
                "user",
                "--keep-data",
                "--prune",
                "--yes",
                "old-plugin"
            ]
        );
    }

    #[test]
    fn test_plugin_uninstall_yes_alone() {
        // Most common headless case: just need to skip the prompt.
        let cmd = PluginUninstallCommand::new("p").yes();
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "uninstall", "--yes", "p"]
        );
    }

    #[test]
    fn test_plugin_enable() {
        let cmd = PluginEnableCommand::new("my-plugin").scope(Scope::Project);
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "enable", "--scope", "project", "my-plugin"]
        );
    }

    #[test]
    fn test_plugin_disable_specific() {
        let cmd = PluginDisableCommand::new("my-plugin");
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "disable", "my-plugin"]
        );
    }

    #[test]
    fn test_plugin_disable_all() {
        let cmd = PluginDisableCommand::all();
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "disable", "--all"]
        );
    }

    #[test]
    fn test_plugin_update() {
        let cmd = PluginUpdateCommand::new("my-plugin").scope(Scope::Local);
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "update", "--scope", "local", "my-plugin"]
        );
    }

    #[test]
    fn test_plugin_validate() {
        let cmd = PluginValidateCommand::new("/path/to/manifest");
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "validate", "/path/to/manifest"]
        );
    }

    #[test]
    fn plugin_tag_defaults_to_just_subcommand() {
        let cmd = PluginTagCommand::new();
        assert_eq!(ClaudeCommand::args(&cmd), vec!["plugin", "tag"]);
    }

    #[test]
    fn plugin_tag_with_all_options() {
        let cmd = PluginTagCommand::new()
            .path("./plugin")
            .dry_run()
            .force()
            .message("release %s")
            .push()
            .remote("upstream");
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec![
                "plugin",
                "tag",
                "--dry-run",
                "--force",
                "--message",
                "release %s",
                "--push",
                "--remote",
                "upstream",
                "./plugin",
            ]
        );
    }

    #[test]
    fn test_plugin_details() {
        let cmd = PluginDetailsCommand::new("some-plugin");
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "details", "some-plugin"]
        );
    }

    #[test]
    fn test_plugin_prune_default() {
        let cmd = PluginPruneCommand::new();
        assert_eq!(ClaudeCommand::args(&cmd), vec!["plugin", "prune"]);
    }

    #[test]
    fn test_plugin_prune_all_flags() {
        let cmd = PluginPruneCommand::new().dry_run().scope(Scope::User).yes();
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "prune", "--dry-run", "--scope", "user", "--yes"]
        );
    }

    #[test]
    fn test_scope_managed_renders_as_arg() {
        // `claude plugin update --scope managed` added in 2.1.143.
        let cmd = PluginUpdateCommand::new("p").scope(Scope::Managed);
        assert_eq!(
            ClaudeCommand::args(&cmd),
            vec!["plugin", "update", "--scope", "managed", "p"]
        );
    }
}