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
use std::path::PathBuf;
use clap::{Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(name = "codex-switch")]
#[command(about = "Multi-account runtime switcher for Codex")]
#[command(version)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// List stored accounts.
List,
/// Log in with ChatGPT/OpenAI OAuth and save the account.
Login {
/// Account display name.
name: String,
/// Replace an existing ChatGPT OAuth account with the same name after login succeeds.
#[arg(long)]
replace: bool,
/// Use device authorization instead of browser OAuth.
#[arg(long = "device-auth")]
device_auth: bool,
},
/// Import an existing Codex CLI auth.json.
Import {
/// Account display name.
name: String,
/// Path to auth.json. Defaults to the current Codex auth.json.
#[arg(long)]
file: Option<PathBuf>,
},
/// Export a stored account as Codex CLI auth.json.
Export {
/// Account name, full ID, or unique ID prefix.
account: String,
/// Write auth.json to this file instead of stdout.
#[arg(long)]
file: Option<PathBuf>,
/// Overwrite the output file if it already exists.
#[arg(long, requires = "file")]
force: bool,
},
/// Switch Codex to a stored account.
Switch {
/// Account name, full ID, or unique ID prefix.
account: String,
},
/// Switch to a usable account when the current Codex auth account is out of usage.
AutoSwitch,
/// Run Codex with runtime account auto-switching.
Run {
/// Codex executable to launch.
#[arg(long, default_value = "codex")]
codex_bin: String,
/// Arguments forwarded to `codex`. Must be passed after `--`.
#[arg(value_name = "CODEX_ARGS", num_args = 0.., last = true, allow_hyphen_values = true)]
codex_args: Vec<String>,
},
/// Update the current codex-switch installation.
Update {
/// Only check whether an update is available.
#[arg(long)]
check: bool,
/// Install a specific version, such as 0.1.10 or v0.1.10.
#[arg(long)]
version: Option<String>,
},
/// Show usage for one account, the current Codex auth account, or all accounts.
Usage {
/// Query every stored account.
#[arg(long, conflicts_with = "account")]
all: bool,
/// Include additional usage limits.
#[arg(long = "show-additional")]
show_additional: bool,
/// Account name, full ID, or unique ID prefix. Defaults to the current Codex auth account.
account: Option<String>,
},
/// Consume one earned ChatGPT rate-limit reset for an account.
ResetUsage {
/// Account name, full ID, or unique ID prefix. Defaults to the current Codex auth account.
account: Option<String>,
/// Skip the interactive confirmation prompt.
#[arg(long)]
yes: bool,
},
/// Delete a stored account.
Delete {
/// Account name, full ID, or unique ID prefix.
account: String,
},
/// Rename a stored account.
Rename {
/// Account name, full ID, or unique ID prefix.
account: String,
/// New account display name.
new_name: String,
},
}
#[cfg(test)]
mod tests {
use super::{Cli, Command};
use clap::Parser;
#[test]
fn run_args_require_double_dash_separator() {
let err = Cli::try_parse_from(["codex-switch", "run", "resume"])
.expect_err("run arguments should require --");
assert_eq!(err.kind(), clap::error::ErrorKind::UnknownArgument);
}
#[test]
fn run_without_forwarded_args_is_allowed() {
let cli =
Cli::try_parse_from(["codex-switch", "run"]).expect("run without args should parse");
let Command::Run { codex_args, .. } = cli.command else {
panic!("expected run command");
};
assert!(codex_args.is_empty());
}
#[test]
fn run_args_after_double_dash_are_forwarded() {
let cli = Cli::try_parse_from([
"codex-switch",
"run",
"--codex-bin",
"/usr/local/bin/codex",
"--",
"resume",
"--last",
])
.expect("run arguments after -- should parse");
let Command::Run {
codex_bin,
codex_args,
} = cli.command
else {
panic!("expected run command");
};
assert_eq!(codex_bin, "/usr/local/bin/codex");
assert_eq!(codex_args, ["resume", "--last"]);
}
#[test]
fn run_args_after_double_dash_may_start_with_hyphen() {
let cli = Cli::try_parse_from(["codex-switch", "run", "--", "--model", "gpt-5"])
.expect("hyphen-prefixed run arguments after -- should parse");
let Command::Run { codex_args, .. } = cli.command else {
panic!("expected run command");
};
assert_eq!(codex_args, ["--model", "gpt-5"]);
}
#[test]
fn reset_usage_defaults_to_confirmation() {
let cli = Cli::try_parse_from(["codex-switch", "reset-usage", "work"])
.expect("reset-usage should parse");
let Command::ResetUsage { account, yes } = cli.command else {
panic!("expected reset-usage command");
};
assert_eq!(account.as_deref(), Some("work"));
assert!(!yes);
}
#[test]
fn reset_usage_supports_yes_flag_without_account() {
let cli = Cli::try_parse_from(["codex-switch", "reset-usage", "--yes"])
.expect("reset-usage --yes should parse");
let Command::ResetUsage { account, yes } = cli.command else {
panic!("expected reset-usage command");
};
assert_eq!(account, None);
assert!(yes);
}
}