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 gpt-5.3-codex
38    cc-switch omp add gw --api-key sk-... --base-url https://api.example.com/v1 --slow-model gpt-4o  # OpenAI-compatible gateway
39    cc-switch omp list
40    cc-switch omp use work                                   # Switch and launch OMP
41    cc-switch omp remove work
42
43GROK CONFIGURATIONS:
44    cc-switch grok add opus --model claude-opus-4-6 --base-url https://api.anthropic.com/v1 --api-backend messages --header x-api-key=sk-ant-...
45    cc-switch grok add gpt --model gpt-4o --base-url https://api.openai.com/v1 --env-key OPENAI_API_KEY
46    cc-switch grok add work --from-file                      # Import a [model.work] entry from ~/.grok/config.toml
47    cc-switch grok add personal -i                           # Interactive mode
48    cc-switch grok list
49    cc-switch grok use opus                                  # Switch default model and launch grok
50    cc-switch grok remove opus
51
52SHELL COMPLETION AND ALIASES:
53    cc-switch completion fish  # Generates shell completions
54
55    Recommended aliases:
56    - cs='cc-switch'           # Quick access to cc-switch
57    - cx='cc-switch codex'     # Quick access to cc-switch codex
58    - co='cc-switch omp'       # Quick access to cc-switch omp
59    - cg='cc-switch grok'      # Quick access to cc-switch grok
60
61    Install permanently:
62    cc-switch completion fish > ~/.config/fish/completions/cc-switch.fish
63    echo \"alias cs='cc-switch'\" >> ~/.config/fish/config.fish
64    echo \"alias cx='cc-switch codex'\" >> ~/.config/fish/config.fish
65    echo \"alias co='cc-switch omp'\" >> ~/.config/fish/config.fish
66    echo \"alias cg='cc-switch grok'\" >> ~/.config/fish/config.fish
67
68    Then use:
69    cs current    # Instead of cc-switch current
70    cx use work   # Instead of cc-switch codex use work
71    co use work   # Instead of cc-switch omp use work
72    cg use opus   # Instead of cc-switch grok use opus"
73)]
74pub struct Cli {
75    #[command(subcommand)]
76    pub command: Option<Commands>,
77
78    /// List available configuration aliases (for shell completion)
79    #[arg(long = "list-aliases", hide = true)]
80    pub list_aliases: bool,
81
82    /// List available Codex configuration aliases (for shell completion)
83    #[arg(long = "list-codex-aliases", hide = true)]
84    pub list_codex_aliases: bool,
85    /// List available OMP configuration aliases (for shell completion)
86    #[arg(long = "list-omp-aliases", hide = true)]
87    pub list_omp_aliases: bool,
88    /// List available grok configuration aliases (for shell completion)
89    #[arg(long = "list-grok-aliases", hide = true)]
90    pub list_grok_aliases: bool,
91
92    /// Migrate old config path (~/.cc_auto_switch/configurations.json) to new path
93    #[arg(
94        long = "migrate",
95        help = "Migrate old config path to new path and exit"
96    )]
97    pub migrate: bool,
98
99    /// Storage mode for writing configuration (env or config)
100    #[arg(
101        long = "store",
102        help = "Storage mode for writing configuration (env: write to env field, config: write to root with camelCase)",
103        global = true
104    )]
105    pub store: Option<String>,
106}
107
108/// Available subcommands for configuration management
109#[derive(Subcommand)]
110#[allow(clippy::large_enum_variant)]
111pub enum Commands {
112    /// Add a new Claude API configuration
113    ///
114    /// Stores a new configuration with alias, API token, base URL, and optional model settings
115    Add {
116        /// Configuration alias name (used to identify this config)
117        #[arg(help = "Configuration alias name (cannot be 'cc')")]
118        alias_name: String,
119
120        /// ANTHROPIC_AUTH_TOKEN value (your Claude API token)
121        #[arg(
122            long = "token",
123            short = 't',
124            help = "API token (ANTHROPIC_AUTH_TOKEN, mutually exclusive with --api-key)"
125        )]
126        token: Option<String>,
127
128        /// ANTHROPIC_API_KEY value (alternative to --token, mutually exclusive)
129        #[arg(
130            long = "api-key",
131            short = 'k',
132            help = "API key (ANTHROPIC_API_KEY, mutually exclusive with --token)"
133        )]
134        api_key: Option<String>,
135
136        /// ANTHROPIC_BASE_URL value (API endpoint URL)
137        #[arg(
138            long = "url",
139            short = 'u',
140            help = "API endpoint URL (optional if not using interactive mode)"
141        )]
142        url: Option<String>,
143
144        /// ANTHROPIC_MODEL value (custom model name)
145        #[arg(long = "model", short = 'm', help = "Custom model name (optional)")]
146        model: Option<String>,
147
148        /// ANTHROPIC_SMALL_FAST_MODEL value (Haiku-class model for background tasks)
149        #[arg(
150            long = "small-fast-model",
151            help = "Haiku-class model for background tasks (optional)"
152        )]
153        small_fast_model: Option<String>,
154
155        /// ANTHROPIC_MAX_THINKING_TOKENS value (Maximum thinking tokens limit)
156        #[arg(
157            long = "max-thinking-tokens",
158            help = "Maximum thinking tokens limit (optional)"
159        )]
160        max_thinking_tokens: Option<u32>,
161
162        /// API timeout in milliseconds
163        #[arg(
164            long = "api-timeout-ms",
165            help = "API timeout in milliseconds (optional)"
166        )]
167        api_timeout_ms: Option<u32>,
168
169        /// Disable non-essential traffic flag
170        #[arg(
171            long = "disable-nonessential-traffic",
172            help = "Disable non-essential traffic flag (optional)"
173        )]
174        claude_code_disable_nonessential_traffic: Option<u32>,
175
176        /// Default Sonnet model name
177        #[arg(
178            long = "default-sonnet-model",
179            help = "Default Sonnet model name (optional)"
180        )]
181        anthropic_default_sonnet_model: Option<String>,
182
183        /// Default Opus model name
184        #[arg(
185            long = "default-opus-model",
186            help = "Default Opus model name (optional)"
187        )]
188        anthropic_default_opus_model: Option<String>,
189
190        /// Default Haiku model name
191        #[arg(
192            long = "default-haiku-model",
193            help = "Default Haiku model name (optional)"
194        )]
195        anthropic_default_haiku_model: Option<String>,
196
197        /// CLAUDE_CODE_SUBAGENT_MODEL value (model for subagents)
198        #[arg(long = "subagent-model", help = "Subagent model name (optional)")]
199        claude_code_subagent_model: Option<String>,
200
201        /// CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK flag
202        #[arg(
203            long = "disable-nonstreaming-fallback",
204            help = "Disable non-streaming fallback flag (optional)"
205        )]
206        claude_code_disable_nonstreaming_fallback: Option<u32>,
207
208        /// CLAUDE_CODE_EFFORT_LEVEL value
209        #[arg(
210            long = "effort-level",
211            help = "Effort level for Claude Code (optional, e.g., 'max')"
212        )]
213        claude_code_effort_level: Option<String>,
214
215        /// CLAUDE_CODE_AUTO_COMPACT_WINDOW value
216        #[arg(
217            long = "auto-compact-window",
218            help = "Auto compact window size for Claude Code (optional, e.g., '950000')"
219        )]
220        claude_code_auto_compact_window: Option<String>,
221
222        /// DISABLE_PROMPT_CACHING flag
223        #[arg(
224            long = "disable-prompt-caching",
225            help = "Disable prompt caching flag (optional)"
226        )]
227        disable_prompt_caching: Option<u32>,
228
229        /// CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS flag
230        #[arg(
231            long = "disable-experimental-betas",
232            help = "Disable experimental betas flag (optional)"
233        )]
234        claude_code_disable_experimental_betas: Option<u32>,
235
236        /// DISABLE_AUTOUPDATER flag
237        #[arg(
238            long = "disable-autoupdater",
239            help = "Disable auto-updater flag (optional)"
240        )]
241        disable_autoupdater: Option<u32>,
242
243        /// Force overwrite existing configuration
244        #[arg(
245            long = "force",
246            short = 'f',
247            help = "Overwrite existing configuration with same alias"
248        )]
249        force: bool,
250
251        /// Interactive mode for entering configuration values
252        #[arg(
253            long = "interactive",
254            short = 'i',
255            help = "Enter configuration values interactively"
256        )]
257        interactive: bool,
258
259        /// Positional token argument (for backward compatibility)
260        #[arg(help = "API token (if not using -t flag)")]
261        token_arg: Option<String>,
262
263        /// Positional URL argument (for backward compatibility)
264        #[arg(help = "API endpoint URL (if not using -u flag)")]
265        url_arg: Option<String>,
266
267        /// Import configuration from a JSON file
268        ///
269        /// With no value, imports from `~/.claude/settings.json`.
270        /// With a value, imports from the given path.
271        #[arg(
272            long = "from-file",
273            num_args = 0..=1,
274            value_name = "PATH",
275            help = "Import configuration from JSON file (defaults to ~/.claude/settings.json if no path)"
276        )]
277        from_file: Option<Option<String>>,
278
279        /// Import configuration from a JSON/text string
280        ///
281        /// Parses known environment variable names from arbitrary text input.
282        #[arg(
283            long = "from-json",
284            value_name = "TEXT",
285            help = "Import configuration from JSON/text string containing env vars"
286        )]
287        from_json: Option<String>,
288
289        /// Import configuration from stdin
290        ///
291        /// Reads text from stdin and parses known environment variable names.
292        #[arg(
293            long = "from-stdin",
294            help = "Import configuration from piped stdin input"
295        )]
296        from_stdin: bool,
297    },
298    /// Remove one or more configurations by alias name
299    ///
300    /// Deletes stored configurations by their alias names
301    Remove {
302        /// Configuration alias name(s) to remove (one or more)
303        #[arg(required = true)]
304        alias_names: Vec<String>,
305    },
306    /// List all stored configurations
307    ///
308    /// Displays all saved configurations with their aliases, tokens, and URLs
309    List {
310        /// Output in plain text format (default is JSON)
311        #[arg(long = "plain", short = 'p')]
312        plain: bool,
313        /// Show only name and URL
314        #[arg(long = "name", short = 'n')]
315        name: bool,
316    },
317    /// Generate shell completion scripts
318    ///
319    /// Generates completion scripts for supported shells
320    #[command(alias = "C")]
321    Completion {
322        /// Shell type (fish, zsh, bash, elvish, powershell)
323        #[arg(default_value = "fish")]
324        shell: String,
325    },
326    /// Switch to a configuration and optionally send a prompt to Claude
327    ///
328    /// Quickly switches to the specified configuration and launches Claude.
329    /// Any additional arguments after the alias name are joined and sent as a prompt.
330    /// Use --resume to resume a previous Claude session by ID.
331    /// Use --continue to continue the most recent Claude session.
332    #[command(trailing_var_arg = true)]
333    Use {
334        /// Configuration alias name to switch to
335        alias_name: String,
336
337        /// Resume a previous Claude session by ID
338        #[arg(long, short = 'r')]
339        resume: Option<String>,
340
341        /// Continue the most recent Claude session
342        #[arg(long, short = 'c')]
343        r#continue: bool,
344
345        /// Prompt to send to Claude (all remaining arguments)
346        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
347        prompt: Vec<String>,
348    },
349    /// Manage Codex (OpenAI CLI) configurations
350    Codex {
351        #[command(subcommand)]
352        command: Option<CodexCommands>,
353    },
354    /// Manage OMP (omp.sh) configurations
355    Omp {
356        #[command(subcommand)]
357        command: Option<OmpCommands>,
358    },
359    /// Manage grok (xAI CLI) custom model configurations
360    Grok {
361        #[command(subcommand)]
362        command: Option<GrokCommands>,
363    },
364    /// Manage statusLine integration with Claude Code
365    ///
366    /// Installs a wrapper script that displays the current cc-switch alias name
367    /// in Claude Code's statusLine, alongside the original statusLine content.
368    ///
369    /// Usage:
370    ///   cc-switch statusline install    # Install/update the wrapper
371    ///   cc-switch statusline uninstall  # Remove the wrapper
372    #[command(name = "statusline")]
373    Statusline {
374        /// Action to perform (install or uninstall)
375        #[arg(value_enum, default_value = "install")]
376        action: StatuslineAction,
377    },
378}
379
380/// Actions for the statusline subcommand
381#[derive(Clone, Copy, clap::ValueEnum)]
382pub enum StatuslineAction {
383    /// Install the statusLine wrapper script
384    Install,
385    /// Uninstall the statusLine wrapper script
386    Uninstall,
387}
388
389/// Available subcommands for Codex configuration management
390#[derive(Subcommand)]
391pub enum CodexCommands {
392    /// Add a new Codex (OpenAI CLI) configuration
393    Add {
394        #[arg(help = "Configuration alias name")]
395        alias_name: String,
396        #[arg(long = "api-key", help = "OpenAI API key (optional)")]
397        api_key: Option<String>,
398        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
399        force: bool,
400        #[arg(
401            long = "interactive",
402            short = 'i',
403            help = "Enter configuration values interactively"
404        )]
405        interactive: bool,
406        /// Import from existing auth.json file
407        ///
408        /// With no value, imports from `~/.codex/auth.json`.
409        /// With a value, imports from the given path.
410        #[arg(
411            long = "from-file",
412            num_args = 0..=1,
413            value_name = "PATH",
414            help = "Import from auth.json (defaults to ~/.codex/auth.json if no path)"
415        )]
416        from_file: Option<Option<String>>,
417    },
418    List {
419        #[arg(long = "plain", short = 'p')]
420        plain: bool,
421        #[arg(long = "name", short = 'n', help = "Show only name and auth mode")]
422        name: bool,
423    },
424    #[command(trailing_var_arg = true)]
425    Use {
426        alias_name: String,
427        #[arg(long = "continue", short = 'c')]
428        r#continue: bool,
429        #[arg(long = "resume", short = 'r')]
430        resume: Option<String>,
431        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
432        prompt: Vec<String>,
433    },
434    Remove {
435        #[arg(required = true)]
436        alias_names: Vec<String>,
437    },
438}
439/// Available subcommands for OMP configuration management
440#[derive(Subcommand)]
441pub enum OmpCommands {
442    /// Add a new OMP (omp.sh) configuration
443    Add {
444        #[arg(help = "Configuration alias name")]
445        alias_name: String,
446        #[arg(long = "api-key", help = "OpenAI API key")]
447        api_key: Option<String>,
448        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
449        force: bool,
450        #[arg(
451            long = "interactive",
452            short = 'i',
453            help = "Enter configuration values interactively"
454        )]
455        interactive: bool,
456        /// Import from existing .env file
457        ///
458        /// With no value, imports from `~/.omp/agent/.env`.
459        /// With a value, imports from the given path.
460        #[arg(
461            long = "from-file",
462            num_args = 0..=1,
463            value_name = "PATH",
464            help = "Import from .env file (defaults to ~/.omp/agent/.env if no path)"
465        )]
466        from_file: Option<Option<String>>,
467        #[arg(
468            long = "base-url",
469            help = "ANTHROPIC_BASE_URL value (for Anthropic-compatible endpoints)"
470        )]
471        base_url: Option<String>,
472        #[arg(long = "slow-model", help = "PI_SLOW_MODEL value (optional)")]
473        slow_model: Option<String>,
474        #[arg(long = "smol-model", help = "PI_SMOL_MODEL value (optional)")]
475        smol_model: Option<String>,
476        #[arg(long = "plan-model", help = "PI_PLAN_MODEL value (optional)")]
477        plan_model: Option<String>,
478    },
479    List {
480        #[arg(long = "plain", short = 'p')]
481        plain: bool,
482        #[arg(long = "name", short = 'n', help = "Show only name and auth mode")]
483        name: bool,
484    },
485    #[command(trailing_var_arg = true)]
486    Use {
487        alias_name: String,
488        #[arg(long = "continue", short = 'c')]
489        r#continue: bool,
490        #[arg(long = "resume", short = 'r')]
491        resume: Option<String>,
492        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
493        prompt: Vec<String>,
494    },
495    Remove {
496        #[arg(required = true)]
497        alias_names: Vec<String>,
498    },
499}
500
501/// Available subcommands for grok configuration management
502#[derive(Subcommand)]
503pub enum GrokCommands {
504    /// Add a new grok custom model configuration
505    Add {
506        #[arg(help = "Configuration alias name (used as the [model.<alias>] id)")]
507        alias_name: String,
508        #[arg(long = "model", help = "Underlying model id (e.g. claude-opus-4-6)")]
509        model: Option<String>,
510        #[arg(long = "base-url", help = "API endpoint base URL")]
511        base_url: Option<String>,
512        #[arg(long = "name", help = "Human-friendly display name")]
513        name: Option<String>,
514        #[arg(
515            long = "api-backend",
516            help = "API backend: chat_completions | responses | messages"
517        )]
518        api_backend: Option<String>,
519        #[arg(long = "context-window", help = "Context window size")]
520        context_window: Option<u32>,
521        #[arg(
522            long = "api-key",
523            help = "API key written directly to config (optional)"
524        )]
525        api_key: Option<String>,
526        #[arg(
527            long = "env-key",
528            help = "Environment variable name to read the key from (repeatable)"
529        )]
530        env_key: Vec<String>,
531        #[arg(
532            long = "header",
533            value_name = "KEY=VALUE",
534            help = "Extra HTTP header for the model, e.g. x-api-key=sk-ant-... (repeatable)"
535        )]
536        header: Vec<String>,
537        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
538        force: bool,
539        #[arg(
540            long = "interactive",
541            short = 'i',
542            help = "Enter configuration values interactively"
543        )]
544        interactive: bool,
545        /// Import a [model.<alias>] entry from an existing config.toml
546        ///
547        /// With no value, imports from `~/.grok/config.toml`.
548        /// With a value, imports from the given path.
549        #[arg(
550            long = "from-file",
551            num_args = 0..=1,
552            value_name = "PATH",
553            help = "Import from config.toml (defaults to ~/.grok/config.toml if no path)"
554        )]
555        from_file: Option<Option<String>>,
556    },
557    List {
558        #[arg(long = "plain", short = 'p')]
559        plain: bool,
560        #[arg(long = "name", short = 'n', help = "Show only alias name and model")]
561        name: bool,
562    },
563    #[command(trailing_var_arg = true)]
564    Use {
565        alias_name: String,
566        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
567        prompt: Vec<String>,
568    },
569    Remove {
570        #[arg(required = true)]
571        alias_names: Vec<String>,
572    },
573}