Last active 1 month ago

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

Revision 5372198e758bca2589cd3b352830c32393101a98

profile.ps1 Raw
1function cc {
2 param ([Parameter(Position=0, ValueFromRemainingArguments=$true)][string[]]$UserArgs)
3
4 $defaultFile = "$HOME\.claude\settings.json"
5 $selected = $env:CLAUDE_DEFAULT # 完全靠环境变量控制默认(可为空)
6 $actualArgs = @()
7 $i = 0
8
9 # 第一参数:支持 modelscope / -modelscope / -anything
10 if ($UserArgs.Count -gt 0) {
11 $first = $UserArgs[0]
12 if ($first -match '^-?(.+)') {
13 $selected = $matches[1]
14 $i = 1
15 }
16 }
17
18 for (; $i -lt $UserArgs.Count; $i++) {
19 $actualArgs += $UserArgs[$i]
20 }
21
22 # ============ 超级详细日志 ============
23 #Write-Host "========== cc 日志 ==========" -ForegroundColor DarkGray
24 #Write-Host "你输入的完整参数: $($UserArgs -join ' ')" -ForegroundColor Gray
25 #Write-Host "识别到的后缀: $selected" -ForegroundColor Gray
26 Write-Host "环境变量默认后缀: '$env:CLAUDE_DEFAULT'" -ForegroundColor Gray
27
28 $settingsFile = $null
29 $log = ""
30
31 if ($selected) {
32 $candidate = "$HOME\.claude\settings.json-$selected"
33 if (Test-Path $candidate) {
34 $settingsFile = $candidate
35 $log = "成功加载专用配置 → $settingsFile"
36 } else {
37 $log = "专用配置不存在 → $candidate"
38 }
39 } else {
40 $log = "未指定后缀"
41 }
42
43 if (-not $settingsFile -and (Test-Path $defaultFile)) {
44 $settingsFile = $defaultFile
45 $log += " → 回退到默认配置 → $defaultFile"
46 } elseif (-not $settingsFile) {
47 $log += " → 无任何配置文件,将直接运行 claude(无 --settings 参数)"
48 }
49
50 Write-Host $log -ForegroundColor Yellow
51 Write-Host "最终实际使用的配置文件: $($settingsFile ? $settingsFile : '无')" -ForegroundColor Green
52 Write-Host "===============================" -ForegroundColor DarkGray
53 # ======================================
54
55 $baseCmd = @('claude')
56 if ($settingsFile) { $baseCmd += '--settings', $settingsFile }
57
58 if ($actualArgs.Count -gt 0) {
59 # 一次性发送
60 $cmd = $baseCmd + $actualArgs
61 $cmdStr = $cmd | ForEach-Object { '"{0}"' -f $_ }
62 Write-Host "Executing → $cmdStr" -ForegroundColor White
63 if ($settingsFile) {
64 & claude --settings $settingsFile @actualArgs
65 } else {
66 & claude @actualArgs
67 }
68 } else {
69 # 交互模式
70 Write-Host "进入交互模式(按 Ctrl+C 退出)" -ForegroundColor White
71 if ($settingsFile) {
72 & claude --settings $settingsFile
73 } else {
74 & claude
75 }
76 }
77}