Skip to main content

cc_switch/cli/
cli.rs

1use clap::{Parser, Subcommand};
2
3/// Command-line interface for managing Claude API configurations
4#[derive(Parser)]
5#[command(name = "cc-switch")]
6#[command(about = "A CLI tool for managing Claude API configurations")]
7#[command(version)]
8#[command(disable_help_subcommand = true)]
9#[command(
10    long_about = "cc-switch helps you manage multiple Claude API configurations and switch between them easily.
11
12EXAMPLES:
13    cc-switch add my-config sk-ant-xxx https://api.anthropic.com
14    cc-switch add my-config -t sk-ant-xxx -u https://api.anthropic.com
15    cc-switch add my-config -t sk-ant-xxx -u https://api.anthropic.com -m claude-3-5-sonnet-20241022
16    cc-switch add my-config -t sk-ant-xxx -u https://api.anthropic.com --small-fast-model claude-3-haiku-20240307
17    cc-switch add my-config -t sk-ant-xxx -u https://api.anthropic.com --max-thinking-tokens 8192
18    cc-switch add my-config -i                       # Interactive mode
19    cc-switch add my-config --from-file              # Import from ~/.claude/settings.json
20    cc-switch add my-config --from-file ./other.json # Import from an explicit JSON file
21    cc-switch add my-config --force  # Overwrite existing config
22    cc-switch list
23    cc-switch remove config1 config2 config3
24    cc-switch current  # Interactive mode to view and switch configurations
25    cc-switch  # Enter interactive mode (same as 'current' without arguments)
26
27CODEX CONFIGURATIONS:
28    cc-switch codex add work --from-file                       # Import from ~/.codex/auth.json
29    cc-switch codex add work --from-file ~/other/auth.json     # Import from an explicit path
30    cc-switch codex add personal -i  # Interactive mode
31    cc-switch codex list
32    cc-switch codex use work  # Switch and launch Codex
33OMP CONFIGURATIONS:
34    cc-switch omp add work --from-file                       # Import from ~/.omp/agent/.env
35    cc-switch omp add work --from-file ~/other/.env          # Import from an explicit path
36    cc-switch omp add personal -i                            # Interactive mode
37    cc-switch omp add gpt --api-key sk-... --slow-model openai/gpt-5.3-codex:high
38    cc-switch omp list
39    cc-switch omp use work                                   # Switch and launch OMP
40    cc-switch omp remove work
41
42SHELL COMPLETION AND ALIASES:
43    cc-switch completion fish  # Generates shell completions
44
45    Recommended aliases:
46    - cs='cc-switch'           # Quick access to cc-switch
47    - cx='cc-switch codex'     # Quick access to cc-switch codex
48    - co='cc-switch omp'       # Quick access to cc-switch omp
49
50    Install permanently:
51    cc-switch completion fish > ~/.config/fish/completions/cc-switch.fish
52    echo \"alias cs='cc-switch'\" >> ~/.config/fish/config.fish
53    echo \"alias cx='cc-switch codex'\" >> ~/.config/fish/config.fish
54    echo \"alias co='cc-switch omp'\" >> ~/.config/fish/config.fish
55
56    Then use:
57    cs current    # Instead of cc-switch current
58    cx use work   # Instead of cc-switch codex use work
59    co use work   # Instead of cc-switch omp use work"
60)]
61pub struct Cli {
62    #[command(subcommand)]
63    pub command: Option<Commands>,
64
65    /// List available configuration aliases (for shell completion)
66    #[arg(long = "list-aliases", hide = true)]
67    pub list_aliases: bool,
68
69    /// List available Codex configuration aliases (for shell completion)
70    #[arg(long = "list-codex-aliases", hide = true)]
71    pub list_codex_aliases: bool,
72    /// List available OMP configuration aliases (for shell completion)
73    #[arg(long = "list-omp-aliases", hide = true)]
74    pub list_omp_aliases: bool,
75
76    /// Migrate old config path (~/.cc_auto_switch/configurations.json) to new path
77    #[arg(
78        long = "migrate",
79        help = "Migrate old config path to new path and exit"
80    )]
81    pub migrate: bool,
82
83    /// Storage mode for writing configuration (env or config)
84    #[arg(
85        long = "store",
86        help = "Storage mode for writing configuration (env: write to env field, config: write to root with camelCase)",
87        global = true
88    )]
89    pub store: Option<String>,
90}
91
92/// Available subcommands for configuration management
93#[derive(Subcommand)]
94#[allow(clippy::large_enum_variant)]
95pub enum Commands {
96    /// Add a new Claude API configuration
97    ///
98    /// Stores a new configuration with alias, API token, base URL, and optional model settings
99    Add {
100        /// Configuration alias name (used to identify this config)
101        #[arg(help = "Configuration alias name (cannot be 'cc')")]
102        alias_name: String,
103
104        /// ANTHROPIC_AUTH_TOKEN value (your Claude API token)
105        #[arg(
106            long = "token",
107            short = 't',
108            help = "API token (ANTHROPIC_AUTH_TOKEN, mutually exclusive with --api-key)"
109        )]
110        token: Option<String>,
111
112        /// ANTHROPIC_API_KEY value (alternative to --token, mutually exclusive)
113        #[arg(
114            long = "api-key",
115            short = 'k',
116            help = "API key (ANTHROPIC_API_KEY, mutually exclusive with --token)"
117        )]
118        api_key: Option<String>,
119
120        /// ANTHROPIC_BASE_URL value (API endpoint URL)
121        #[arg(
122            long = "url",
123            short = 'u',
124            help = "API endpoint URL (optional if not using interactive mode)"
125        )]
126        url: Option<String>,
127
128        /// ANTHROPIC_MODEL value (custom model name)
129        #[arg(long = "model", short = 'm', help = "Custom model name (optional)")]
130        model: Option<String>,
131
132        /// ANTHROPIC_SMALL_FAST_MODEL value (Haiku-class model for background tasks)
133        #[arg(
134            long = "small-fast-model",
135            help = "Haiku-class model for background tasks (optional)"
136        )]
137        small_fast_model: Option<String>,
138
139        /// ANTHROPIC_MAX_THINKING_TOKENS value (Maximum thinking tokens limit)
140        #[arg(
141            long = "max-thinking-tokens",
142            help = "Maximum thinking tokens limit (optional)"
143        )]
144        max_thinking_tokens: Option<u32>,
145
146        /// API timeout in milliseconds
147        #[arg(
148            long = "api-timeout-ms",
149            help = "API timeout in milliseconds (optional)"
150        )]
151        api_timeout_ms: Option<u32>,
152
153        /// Disable non-essential traffic flag
154        #[arg(
155            long = "disable-nonessential-traffic",
156            help = "Disable non-essential traffic flag (optional)"
157        )]
158        claude_code_disable_nonessential_traffic: Option<u32>,
159
160        /// Default Sonnet model name
161        #[arg(
162            long = "default-sonnet-model",
163            help = "Default Sonnet model name (optional)"
164        )]
165        anthropic_default_sonnet_model: Option<String>,
166
167        /// Default Opus model name
168        #[arg(
169            long = "default-opus-model",
170            help = "Default Opus model name (optional)"
171        )]
172        anthropic_default_opus_model: Option<String>,
173
174        /// Default Haiku model name
175        #[arg(
176            long = "default-haiku-model",
177            help = "Default Haiku model name (optional)"
178        )]
179        anthropic_default_haiku_model: Option<String>,
180
181        /// CLAUDE_CODE_SUBAGENT_MODEL value (model for subagents)
182        #[arg(long = "subagent-model", help = "Subagent model name (optional)")]
183        claude_code_subagent_model: Option<String>,
184
185        /// CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK flag
186        #[arg(
187            long = "disable-nonstreaming-fallback",
188            help = "Disable non-streaming fallback flag (optional)"
189        )]
190        claude_code_disable_nonstreaming_fallback: Option<u32>,
191
192        /// CLAUDE_CODE_EFFORT_LEVEL value
193        #[arg(
194            long = "effort-level",
195            help = "Effort level for Claude Code (optional, e.g., 'max')"
196        )]
197        claude_code_effort_level: Option<String>,
198
199        /// DISABLE_PROMPT_CACHING flag
200        #[arg(
201            long = "disable-prompt-caching",
202            help = "Disable prompt caching flag (optional)"
203        )]
204        disable_prompt_caching: Option<u32>,
205
206        /// CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS flag
207        #[arg(
208            long = "disable-experimental-betas",
209            help = "Disable experimental betas flag (optional)"
210        )]
211        claude_code_disable_experimental_betas: Option<u32>,
212
213        /// DISABLE_AUTOUPDATER flag
214        #[arg(
215            long = "disable-autoupdater",
216            help = "Disable auto-updater flag (optional)"
217        )]
218        disable_autoupdater: Option<u32>,
219
220        /// Force overwrite existing configuration
221        #[arg(
222            long = "force",
223            short = 'f',
224            help = "Overwrite existing configuration with same alias"
225        )]
226        force: bool,
227
228        /// Interactive mode for entering configuration values
229        #[arg(
230            long = "interactive",
231            short = 'i',
232            help = "Enter configuration values interactively"
233        )]
234        interactive: bool,
235
236        /// Positional token argument (for backward compatibility)
237        #[arg(help = "API token (if not using -t flag)")]
238        token_arg: Option<String>,
239
240        /// Positional URL argument (for backward compatibility)
241        #[arg(help = "API endpoint URL (if not using -u flag)")]
242        url_arg: Option<String>,
243
244        /// Import configuration from a JSON file
245        ///
246        /// With no value, imports from `~/.claude/settings.json`.
247        /// With a value, imports from the given path.
248        #[arg(
249            long = "from-file",
250            num_args = 0..=1,
251            value_name = "PATH",
252            help = "Import configuration from JSON file (defaults to ~/.claude/settings.json if no path)"
253        )]
254        from_file: Option<Option<String>>,
255    },
256    /// Remove one or more configurations by alias name
257    ///
258    /// Deletes stored configurations by their alias names
259    Remove {
260        /// Configuration alias name(s) to remove (one or more)
261        #[arg(required = true)]
262        alias_names: Vec<String>,
263    },
264    /// List all stored configurations
265    ///
266    /// Displays all saved configurations with their aliases, tokens, and URLs
267    List {
268        /// Output in plain text format (default is JSON)
269        #[arg(long = "plain", short = 'p')]
270        plain: bool,
271        /// Show only name and URL
272        #[arg(long = "name", short = 'n')]
273        name: bool,
274    },
275    /// Generate shell completion scripts
276    ///
277    /// Generates completion scripts for supported shells
278    #[command(alias = "C")]
279    Completion {
280        /// Shell type (fish, zsh, bash, elvish, powershell)
281        #[arg(default_value = "fish")]
282        shell: String,
283    },
284    /// Switch to a configuration and optionally send a prompt to Claude
285    ///
286    /// Quickly switches to the specified configuration and launches Claude.
287    /// Any additional arguments after the alias name are joined and sent as a prompt.
288    /// Use --resume to resume a previous Claude session by ID.
289    /// Use --continue to continue the most recent Claude session.
290    #[command(trailing_var_arg = true)]
291    Use {
292        /// Configuration alias name to switch to
293        alias_name: String,
294
295        /// Resume a previous Claude session by ID
296        #[arg(long, short = 'r')]
297        resume: Option<String>,
298
299        /// Continue the most recent Claude session
300        #[arg(long, short = 'c')]
301        r#continue: bool,
302
303        /// Prompt to send to Claude (all remaining arguments)
304        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
305        prompt: Vec<String>,
306    },
307    /// Manage Codex (OpenAI CLI) configurations
308    Codex {
309        #[command(subcommand)]
310        command: Option<CodexCommands>,
311    },
312    /// Manage OMP (omp.sh) configurations
313    Omp {
314        #[command(subcommand)]
315        command: Option<OmpCommands>,
316    },
317    /// Manage statusLine integration with Claude Code
318    ///
319    /// Installs a wrapper script that displays the current cc-switch alias name
320    /// in Claude Code's statusLine, alongside the original statusLine content.
321    ///
322    /// Usage:
323    ///   cc-switch statusline install    # Install/update the wrapper
324    ///   cc-switch statusline uninstall  # Remove the wrapper
325    #[command(name = "statusline")]
326    Statusline {
327        /// Action to perform (install or uninstall)
328        #[arg(value_enum, default_value = "install")]
329        action: StatuslineAction,
330    },
331}
332
333/// Actions for the statusline subcommand
334#[derive(Clone, Copy, clap::ValueEnum)]
335pub enum StatuslineAction {
336    /// Install the statusLine wrapper script
337    Install,
338    /// Uninstall the statusLine wrapper script
339    Uninstall,
340}
341
342/// Available subcommands for Codex configuration management
343#[derive(Subcommand)]
344pub enum CodexCommands {
345    /// Add a new Codex (OpenAI CLI) configuration
346    Add {
347        #[arg(help = "Configuration alias name")]
348        alias_name: String,
349        #[arg(long = "api-key", help = "OpenAI API key (optional)")]
350        api_key: Option<String>,
351        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
352        force: bool,
353        #[arg(
354            long = "interactive",
355            short = 'i',
356            help = "Enter configuration values interactively"
357        )]
358        interactive: bool,
359        /// Import from existing auth.json file
360        ///
361        /// With no value, imports from `~/.codex/auth.json`.
362        /// With a value, imports from the given path.
363        #[arg(
364            long = "from-file",
365            num_args = 0..=1,
366            value_name = "PATH",
367            help = "Import from auth.json (defaults to ~/.codex/auth.json if no path)"
368        )]
369        from_file: Option<Option<String>>,
370    },
371    List {
372        #[arg(long = "plain", short = 'p')]
373        plain: bool,
374        #[arg(long = "name", short = 'n', help = "Show only name and auth mode")]
375        name: bool,
376    },
377    #[command(trailing_var_arg = true)]
378    Use {
379        alias_name: String,
380        #[arg(long = "continue", short = 'c')]
381        r#continue: bool,
382        #[arg(long = "resume", short = 'r')]
383        resume: Option<String>,
384        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
385        prompt: Vec<String>,
386    },
387    Remove {
388        #[arg(required = true)]
389        alias_names: Vec<String>,
390    },
391}
392/// Available subcommands for OMP configuration management
393#[derive(Subcommand)]
394pub enum OmpCommands {
395    /// Add a new OMP (omp.sh) configuration
396    Add {
397        #[arg(help = "Configuration alias name")]
398        alias_name: String,
399        #[arg(long = "api-key", help = "API key (Anthropic or OpenAI)")]
400        api_key: Option<String>,
401        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
402        force: bool,
403        #[arg(
404            long = "interactive",
405            short = 'i',
406            help = "Enter configuration values interactively"
407        )]
408        interactive: bool,
409        /// Import from existing .env file
410        ///
411        /// With no value, imports from `~/.omp/agent/.env`.
412        /// With a value, imports from the given path.
413        #[arg(
414            long = "from-file",
415            num_args = 0..=1,
416            value_name = "PATH",
417            help = "Import from .env file (defaults to ~/.omp/agent/.env if no path)"
418        )]
419        from_file: Option<Option<String>>,
420        #[arg(
421            long = "base-url",
422            help = "ANTHROPIC_BASE_URL value (for Anthropic-compatible endpoints)"
423        )]
424        base_url: Option<String>,
425        #[arg(long = "slow-model", help = "PI_SLOW_MODEL value (optional)")]
426        slow_model: Option<String>,
427        #[arg(long = "smol-model", help = "PI_SMOL_MODEL value (optional)")]
428        smol_model: Option<String>,
429        #[arg(long = "plan-model", help = "PI_PLAN_MODEL value (optional)")]
430        plan_model: Option<String>,
431    },
432    List {
433        #[arg(long = "plain", short = 'p')]
434        plain: bool,
435        #[arg(long = "name", short = 'n', help = "Show only name and auth mode")]
436        name: bool,
437    },
438    #[command(trailing_var_arg = true)]
439    Use {
440        alias_name: String,
441        #[arg(long = "continue", short = 'c')]
442        r#continue: bool,
443        #[arg(long = "resume", short = 'r')]
444        resume: Option<String>,
445        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
446        prompt: Vec<String>,
447    },
448    Remove {
449        #[arg(required = true)]
450        alias_names: Vec<String>,
451    },
452}