最後活躍 1 month ago

启动 PowerShell 时自动加载的配置文件

修訂 d3bcf78c65b950f9e55bd18b057352619d7b61d6

profile.ps1 原始檔案
1
2function ccc {
3 param ([Parameter(Position=0, ValueFromRemainingArguments=$true)][string[]]$UserArgs)
4
5 $defaultFile = "$HOME\.claude\settings.json"
6 $selected = $env:CLAUDE_DEFAULT # 完全靠环境变量控制默认(可为空)
7 $actualArgs = @()
8 $i = 0
9
10 # 第一参数:支持 modelscope / -modelscope / -anything
11 if ($UserArgs.Count -gt 0) {
12 $first = $UserArgs[0]
13 if ($first -match '^-?(.+)') {
14 $selected = $matches[1]
15 $i = 1
16 }
17 }
18
19 for (; $i -lt $UserArgs.Count; $i++) {
20 $actualArgs += $UserArgs[$i]
21 }
22
23 # ============ 超级详细日志 ============
24 Write-Host "========== ccc 日志 ==========" -ForegroundColor DarkGray
25 Write-Host "你输入的完整参数: $($UserArgs -join ' ')" -ForegroundColor Gray
26 Write-Host "识别到的后缀: $selected" -ForegroundColor Gray
27 Write-Host "环境变量默认后缀: '$env:CLAUDE_DEFAULT'" -ForegroundColor Gray
28
29 $settingsFile = $null
30 $log = ""
31
32 if ($selected) {
33 $candidate = "$HOME\.claude\settings.json-$selected"
34 if (Test-Path $candidate) {
35 $settingsFile = $candidate
36 $log = "成功加载专用配置 → $settingsFile"
37 } else {
38 $log = "专用配置不存在 → $candidate"
39 }
40 } else {
41 $log = "未指定后缀"
42 }
43
44 if (-not $settingsFile -and (Test-Path $defaultFile)) {
45 $settingsFile = $defaultFile
46 $log += " → 回退到默认配置 → $defaultFile"
47 } elseif (-not $settingsFile) {
48 $log += " → 无任何配置文件,将直接运行 claude(无 --settings 参数)"
49 }
50
51 Write-Host $log -ForegroundColor Yellow
52 Write-Host "最终实际使用的配置文件: $($settingsFile ? $settingsFile : '无')" -ForegroundColor Green
53 Write-Host "===============================" -ForegroundColor DarkGray
54 # ======================================
55
56 $baseCmd = @('claude')
57 if ($settingsFile) { $baseCmd += '--settings', $settingsFile }
58
59 if ($actualArgs.Count -gt 0) {
60 # 一次性发送
61 $cmd = $baseCmd + $actualArgs
62 $cmdStr = $cmd | ForEach-Object { '"{0}"' -f $_ }
63 Write-Host "Executing → $cmdStr" -ForegroundColor White
64 if ($settingsFile) {
65 & claude --settings $settingsFile @actualArgs
66 } else {
67 & claude @actualArgs
68 }
69 } else {
70 # 交互模式
71 Write-Host "进入交互模式(按 Ctrl+C 退出)" -ForegroundColor White
72 if ($settingsFile) {
73 & claude --settings $settingsFile
74 } else {
75 & claude
76 }
77 }
78}
79
80function claude-modelscope-default {
81 # 1. 构造要执行的完整命令数组
82 $cmd = @('claude', '--settings', "$HOME\.claude\settings.json-mscope") + $args
83
84 # 2. 打印命令(用双引号包裹每个参数,方便阅读)
85 $cmdStr = $cmd | ForEach-Object { '"{0}"' -f $_ }
86 Write-Host "Executing: $cmdStr" -ForegroundColor Cyan
87
88 # 3. 真正执行(使用 & 调用运算符 + 参数展开)
89 & $cmd[0] $cmd[1..($cmd.Count-1)]
90}
91
92function claude-modelscope {
93 $env:ANTHROPIC_AUTH_TOKEN = $env:MODELSCOPE_API_KEY
94 ccc modelscope @args
95}
96
97function claude-streamlake {
98 $env:ANTHROPIC_AUTH_TOKEN = $env:STREAMLAKE_API_KEY
99 ccc streamlake @args
100}
101
102function claude-minimax{
103 $env:ANTHROPIC_AUTH_TOKEN = $env:MINIMAX_API_KEY
104 ccc minimax @args
105}
106
107function claude-longcat {
108 $env:ANTHROPIC_AUTH_TOKEN = $env:LONGCAT_API_KEY
109 ccc longcat @args
110}
111