add: sign in to a new account and save it in one step

Onboards a new account via 'claude auth login', then snapshots it as a
profile. Deliberately does not call 'claude auth logout' — logout may
revoke the token server-side and invalidate an already-saved profile.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
mysqtoandClaude Opus 4.8 committed 2026-07-04 22:44:03 +08:00
1 parent c6d5ef1fa6
commit defea8d217
3 files changed
+35 -1

No files matched your search

+1
View File
@@ -43,6 +43,7 @@ Requires [`jq`](https://jqlang.github.io/jq/).
| --- | --- |
| `ccswitch <name> [args...]` | Switch to `<name>`, then start `claude` (extra args passed through) |
| `ccswitch use <name>` | Switch without launching |
| `ccswitch add <name>` | Sign in to a new account (`claude auth login`) and save it as `<name>` |
| `ccswitch save <name>` | Save the current account as `<name>` |
| `ccswitch list` / `ls` | List profiles (`*` marks the active one) |
| `ccswitch current` / `whoami` | Show the active account |
+1
View File
@@ -14,6 +14,7 @@ end
complete -c ccswitch -f
# subcommands and bare profile names (first token only)
complete -c ccswitch -n __fish_use_subcommand -a add -d "log in a new account and save it"
complete -c ccswitch -n __fish_use_subcommand -a save -d "save current account"
complete -c ccswitch -n __fish_use_subcommand -a use -d "switch without launching"
complete -c ccswitch -n __fish_use_subcommand -a list -d "list profiles"
+33 -1
View File
@@ -6,6 +6,8 @@ function ccswitch -d "switch between multiple Claude Code accounts"
switch "$cmd"
case save
__ccswitch_save "$home" "$config" $argv[2]
case add
__ccswitch_add "$home" "$config" $argv[2]
case list ls
__ccswitch_list "$home" "$config"
case current whoami
@@ -79,7 +81,7 @@ function __ccswitch_save -d "snapshot the current account into a profile"
echo "usage: ccswitch save <name>" >&2
return 1
end
if contains -- "$name" save list ls current whoami use rm remove delete help
if contains -- "$name" save add list ls current whoami use rm remove delete help
echo "ccswitch: '$name' is a reserved word, pick another profile name" >&2
return 1
end
@@ -110,6 +112,35 @@ function __ccswitch_save -d "snapshot the current account into a profile"
echo "saved profile '$name' ($email)"
end
function __ccswitch_add -d "log in a new account and save it as a profile"
set -l home $argv[1]
set -l config $argv[2]
set -l name $argv[3]
if test -z "$name"
echo "usage: ccswitch add <name>" >&2
return 1
end
if contains -- "$name" save add list ls current whoami use rm remove delete help
echo "ccswitch: '$name' is a reserved word, pick another profile name" >&2
return 1
end
if test -d "$home/$name"
echo "ccswitch: profile '$name' already exists (ccswitch rm $name to replace it)" >&2
return 1
end
# note: no `claude auth logout` here — logout may revoke the token
# server-side, which would invalidate an already-saved profile. `login`
# simply replaces the active credential slot, which is all we need.
echo "signing in as '$name' (a browser window will open)..."
if not command claude auth login
echo "ccswitch: login did not complete — nothing saved" >&2
return 1
end
__ccswitch_save "$home" "$config" "$name"
end
function __ccswitch_use -d "restore a profile as the active account"
set -l home $argv[1]
set -l config $argv[2]
@@ -230,6 +261,7 @@ function __ccswitch_help -d "show ccswitch usage"
echo "usage:"
echo " ccswitch <name> [args...] switch to <name> and start a claude session"
echo " ccswitch use <name> switch to <name> without launching claude"
echo " ccswitch add <name> sign in to a new account and save it as <name>"
echo " ccswitch save <name> save the current account as <name>"
echo " ccswitch list | ls list saved profiles (* marks the active one)"
echo " ccswitch current | whoami show the active account"