Utoljára aktív 1 month ago

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

Revízió a0d77efaa41c1386d2b30ef73f0c9c9785e3b05c

profile.ps1 Eredeti
1function cc {
2 param ([Parameter(Position=0, ValueFromRemainingArguments=$true)][string[]]$UserArgs)
3
4 # 颜色表(常用后缀高亮,其他白色)
5 $colorMap = @{
6 mscope = 'Cyan'; streamlake = 'Magenta'; opendevin = 'Green'; deepseek = 'Yellow'
7 groq = 'DarkCyan'; together = 'DarkMagenta'; azure = 'Blue'; openrouter = 'DarkGreen'
8 deepinfra = 'Red'; ollama = 'DarkYellow'; lmstudio = 'White'; local = 'Gray'
9 }
10
11 $selected = $env:CLAUDE_DEFAULT
12 $actualArgs = @()
13
14 for($i=0; $i -lt $UserArgs.Count; $i++) {
15 $arg = $UserArgs[$i]
16 # 支持 cc modescope(无 -)和 cc -mscope 两种写法
17 if ($arg -match '^-?(.+)$') {
18 $selected = $matches[1]
19 continue
20 }
21 $actualArgs += $arg
22 }
23
24 # 构建 settings 路径
25 $defaultFile = "$HOME\.claude\settings.json"
26 $selectedFile = if ($selected) { "$HOME\.claude\settings.json-$selected" } else { $defaultFile }
27
28 # 检查文件是否存在,不存在则不传 --settings(避免报错)
29 if ($selected -and -not (Test-Path $selectedFile)) {
30 Write-Host "警告:配置文件不存在 $selectedFile,使用默认配置" -ForegroundColor Yellow
31 $settings = $defaultFile
32 if (-not (Test-Path $defaultFile)) { $settings = $null } # 连默认都没有
33 } elseif (-not (Test-Path $defaultFile)) {
34 $settings = $null
35 } else {
36 $settings = $selectedFile
37 }
38
39 # 构建命令
40 $cmd = @('claude')
41 if ($settings) { $cmd += '--settings', $settings }
42 $cmd += $actualArgs
43
44 # 显示命令(美化)
45 $cmdStr = $cmd | ForEach-Object { '"{0}"' -f $_ }
46 $color = if ($colorMap.ContainsKey($selected)) { $colorMap[$selected] } else { 'White' }
47 $disp = if ($selected) { "($selected)" } else { "(default)" }
48 Write-Host "Executing cc $disp$cmdStr" -ForegroundColor $color
49
50 # 执行
51 if ($settings) {
52 & claude --settings $settings @actualArgs
53 } else {
54 & claude @actualArgs
55 }
56}