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        /// DISABLE_PROMPT_CACHING flag
216        #[arg(
217            long = "disable-prompt-caching",
218            help = "Disable prompt caching flag (optional)"
219        )]
220        disable_prompt_caching: Option<u32>,
221
222        /// CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS flag
223        #[arg(
224            long = "disable-experimental-betas",
225            help = "Disable experimental betas flag (optional)"
226        )]
227        claude_code_disable_experimental_betas: Option<u32>,
228
229        /// DISABLE_AUTOUPDATER flag
230        #[arg(
231            long = "disable-autoupdater",
232            help = "Disable auto-updater flag (optional)"
233        )]
234        disable_autoupdater: Option<u32>,
235
236        /// Force overwrite existing configuration
237        #[arg(
238            long = "force",
239            short = 'f',
240            help = "Overwrite existing configuration with same alias"
241        )]
242        force: bool,
243
244        /// Interactive mode for entering configuration values
245        #[arg(
246            long = "interactive",
247            short = 'i',
248            help = "Enter configuration values interactively"
249        )]
250        interactive: bool,
251
252        /// Positional token argument (for backward compatibility)
253        #[arg(help = "API token (if not using -t flag)")]
254        token_arg: Option<String>,
255
256        /// Positional URL argument (for backward compatibility)
257        #[arg(help = "API endpoint URL (if not using -u flag)")]
258        url_arg: Option<String>,
259
260        /// Import configuration from a JSON file
261        ///
262        /// With no value, imports from `~/.claude/settings.json`.
263        /// With a value, imports from the given path.
264        #[arg(
265            long = "from-file",
266            num_args = 0..=1,
267            value_name = "PATH",
268            help = "Import configuration from JSON file (defaults to ~/.claude/settings.json if no path)"
269        )]
270        from_file: Option<Option<String>>,
271    },
272    /// Remove one or more configurations by alias name
273    ///
274    /// Deletes stored configurations by their alias names
275    Remove {
276        /// Configuration alias name(s) to remove (one or more)
277        #[arg(required = true)]
278        alias_names: Vec<String>,
279    },
280    /// List all stored configurations
281    ///
282    /// Displays all saved configurations with their aliases, tokens, and URLs
283    List {
284        /// Output in plain text format (default is JSON)
285        #[arg(long = "plain", short = 'p')]
286        plain: bool,
287        /// Show only name and URL
288        #[arg(long = "name", short = 'n')]
289        name: bool,
290    },
291    /// Generate shell completion scripts
292    ///
293    /// Generates completion scripts for supported shells
294    #[command(alias = "C")]
295    Completion {
296        /// Shell type (fish, zsh, bash, elvish, powershell)
297        #[arg(default_value = "fish")]
298        shell: String,
299    },
300    /// Switch to a configuration and optionally send a prompt to Claude
301    ///
302    /// Quickly switches to the specified configuration and launches Claude.
303    /// Any additional arguments after the alias name are joined and sent as a prompt.
304    /// Use --resume to resume a previous Claude session by ID.
305    /// Use --continue to continue the most recent Claude session.
306    #[command(trailing_var_arg = true)]
307    Use {
308        /// Configuration alias name to switch to
309        alias_name: String,
310
311        /// Resume a previous Claude session by ID
312        #[arg(long, short = 'r')]
313        resume: Option<String>,
314
315        /// Continue the most recent Claude session
316        #[arg(long, short = 'c')]
317        r#continue: bool,
318
319        /// Prompt to send to Claude (all remaining arguments)
320        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
321        prompt: Vec<String>,
322    },
323    /// Manage Codex (OpenAI CLI) configurations
324    Codex {
325        #[command(subcommand)]
326        command: Option<CodexCommands>,
327    },
328    /// Manage OMP (omp.sh) configurations
329    Omp {
330        #[command(subcommand)]
331        command: Option<OmpCommands>,
332    },
333    /// Manage grok (xAI CLI) custom model configurations
334    Grok {
335        #[command(subcommand)]
336        command: Option<GrokCommands>,
337    },
338    /// Manage statusLine integration with Claude Code
339    ///
340    /// Installs a wrapper script that displays the current cc-switch alias name
341    /// in Claude Code's statusLine, alongside the original statusLine content.
342    ///
343    /// Usage:
344    ///   cc-switch statusline install    # Install/update the wrapper
345    ///   cc-switch statusline uninstall  # Remove the wrapper
346    #[command(name = "statusline")]
347    Statusline {
348        /// Action to perform (install or uninstall)
349        #[arg(value_enum, default_value = "install")]
350        action: StatuslineAction,
351    },
352}
353
354/// Actions for the statusline subcommand
355#[derive(Clone, Copy, clap::ValueEnum)]
356pub enum StatuslineAction {
357    /// Install the statusLine wrapper script
358    Install,
359    /// Uninstall the statusLine wrapper script
360    Uninstall,
361}
362
363/// Available subcommands for Codex configuration management
364#[derive(Subcommand)]
365pub enum CodexCommands {
366    /// Add a new Codex (OpenAI CLI) configuration
367    Add {
368        #[arg(help = "Configuration alias name")]
369        alias_name: String,
370        #[arg(long = "api-key", help = "OpenAI API key (optional)")]
371        api_key: Option<String>,
372        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
373        force: bool,
374        #[arg(
375            long = "interactive",
376            short = 'i',
377            help = "Enter configuration values interactively"
378        )]
379        interactive: bool,
380        /// Import from existing auth.json file
381        ///
382        /// With no value, imports from `~/.codex/auth.json`.
383        /// With a value, imports from the given path.
384        #[arg(
385            long = "from-file",
386            num_args = 0..=1,
387            value_name = "PATH",
388            help = "Import from auth.json (defaults to ~/.codex/auth.json if no path)"
389        )]
390        from_file: Option<Option<String>>,
391    },
392    List {
393        #[arg(long = "plain", short = 'p')]
394        plain: bool,
395        #[arg(long = "name", short = 'n', help = "Show only name and auth mode")]
396        name: bool,
397    },
398    #[command(trailing_var_arg = true)]
399    Use {
400        alias_name: String,
401        #[arg(long = "continue", short = 'c')]
402        r#continue: bool,
403        #[arg(long = "resume", short = 'r')]
404        resume: Option<String>,
405        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
406        prompt: Vec<String>,
407    },
408    Remove {
409        #[arg(required = true)]
410        alias_names: Vec<String>,
411    },
412}
413/// Available subcommands for OMP configuration management
414#[derive(Subcommand)]
415pub enum OmpCommands {
416    /// Add a new OMP (omp.sh) configuration
417    Add {
418        #[arg(help = "Configuration alias name")]
419        alias_name: String,
420        #[arg(long = "api-key", help = "OpenAI API key")]
421        api_key: Option<String>,
422        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
423        force: bool,
424        #[arg(
425            long = "interactive",
426            short = 'i',
427            help = "Enter configuration values interactively"
428        )]
429        interactive: bool,
430        /// Import from existing .env file
431        ///
432        /// With no value, imports from `~/.omp/agent/.env`.
433        /// With a value, imports from the given path.
434        #[arg(
435            long = "from-file",
436            num_args = 0..=1,
437            value_name = "PATH",
438            help = "Import from .env file (defaults to ~/.omp/agent/.env if no path)"
439        )]
440        from_file: Option<Option<String>>,
441        #[arg(
442            long = "base-url",
443            help = "ANTHROPIC_BASE_URL value (for Anthropic-compatible endpoints)"
444        )]
445        base_url: Option<String>,
446        #[arg(long = "slow-model", help = "PI_SLOW_MODEL value (optional)")]
447        slow_model: Option<String>,
448        #[arg(long = "smol-model", help = "PI_SMOL_MODEL value (optional)")]
449        smol_model: Option<String>,
450        #[arg(long = "plan-model", help = "PI_PLAN_MODEL value (optional)")]
451        plan_model: Option<String>,
452    },
453    List {
454        #[arg(long = "plain", short = 'p')]
455        plain: bool,
456        #[arg(long = "name", short = 'n', help = "Show only name and auth mode")]
457        name: bool,
458    },
459    #[command(trailing_var_arg = true)]
460    Use {
461        alias_name: String,
462        #[arg(long = "continue", short = 'c')]
463        r#continue: bool,
464        #[arg(long = "resume", short = 'r')]
465        resume: Option<String>,
466        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
467        prompt: Vec<String>,
468    },
469    Remove {
470        #[arg(required = true)]
471        alias_names: Vec<String>,
472    },
473}
474
475/// Available subcommands for grok configuration management
476#[derive(Subcommand)]
477pub enum GrokCommands {
478    /// Add a new grok custom model configuration
479    Add {
480        #[arg(help = "Configuration alias name (used as the [model.<alias>] id)")]
481        alias_name: String,
482        #[arg(long = "model", help = "Underlying model id (e.g. claude-opus-4-6)")]
483        model: Option<String>,
484        #[arg(long = "base-url", help = "API endpoint base URL")]
485        base_url: Option<String>,
486        #[arg(long = "name", help = "Human-friendly display name")]
487        name: Option<String>,
488        #[arg(
489            long = "api-backend",
490            help = "API backend: chat_completions | responses | messages"
491        )]
492        api_backend: Option<String>,
493        #[arg(long = "context-window", help = "Context window size")]
494        context_window: Option<u32>,
495        #[arg(
496            long = "api-key",
497            help = "API key written directly to config (optional)"
498        )]
499        api_key: Option<String>,
500        #[arg(
501            long = "env-key",
502            help = "Environment variable name to read the key from (repeatable)"
503        )]
504        env_key: Vec<String>,
505        #[arg(
506            long = "header",
507            value_name = "KEY=VALUE",
508            help = "Extra HTTP header for the model, e.g. x-api-key=sk-ant-... (repeatable)"
509        )]
510        header: Vec<String>,
511        #[arg(long = "force", short = 'f', help = "Overwrite existing configuration")]
512        force: bool,
513        #[arg(
514            long = "interactive",
515            short = 'i',
516            help = "Enter configuration values interactively"
517        )]
518        interactive: bool,
519        /// Import a [model.<alias>] entry from an existing config.toml
520        ///
521        /// With no value, imports from `~/.grok/config.toml`.
522        /// With a value, imports from the given path.
523        #[arg(
524            long = "from-file",
525            num_args = 0..=1,
526            value_name = "PATH",
527            help = "Import from config.toml (defaults to ~/.grok/config.toml if no path)"
528        )]
529        from_file: Option<Option<String>>,
530    },
531    List {
532        #[arg(long = "plain", short = 'p')]
533        plain: bool,
534        #[arg(long = "name", short = 'n', help = "Show only alias name and model")]
535        name: bool,
536    },
537    #[command(trailing_var_arg = true)]
538    Use {
539        alias_name: String,
540        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
541        prompt: Vec<String>,
542    },
543    Remove {
544        #[arg(required = true)]
545        alias_names: Vec<String>,
546    },
547}