Last active 1 month ago

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

jetsung revised this gist 6 months ago. Go to revision

1 file changed, 14 insertions, 3 deletions

profile.ps1

@@ -1,3 +1,4 @@
1 +
1 2 function cc {
2 3 param ([Parameter(Position=0, ValueFromRemainingArguments=$true)][string[]]$UserArgs)
3 4
@@ -20,9 +21,9 @@ function cc {
20 21 }
21 22
22 23 # ============ 超级详细日志 ============
23 - #Write-Host "========== cc 日志 ==========" -ForegroundColor DarkGray
24 - #Write-Host "你输入的完整参数: $($UserArgs -join ' ')" -ForegroundColor Gray
25 - #Write-Host "识别到的后缀: $selected" -ForegroundColor Gray
24 + Write-Host "========== cc 日志 ==========" -ForegroundColor DarkGray
25 + Write-Host "你输入的完整参数: $($UserArgs -join ' ')" -ForegroundColor Gray
26 + Write-Host "识别到的后缀: $selected" -ForegroundColor Gray
26 27 Write-Host "环境变量默认后缀: '$env:CLAUDE_DEFAULT'" -ForegroundColor Gray
27 28
28 29 $settingsFile = $null
@@ -91,4 +92,14 @@ function claude-modelscope-default {
91 92 function claude-modelscope {
92 93 $env:ANTHROPIC_AUTH_TOKEN = $env:MODELSCOPE_API_KEY
93 94 cc modelscope $args
95 + }
96 +
97 + function claude-streamlake {
98 + $env:ANTHROPIC_AUTH_TOKEN = $env:WQ_API_KEY
99 + cc streamlake $args
100 + }
101 +
102 + function claude-minimax{
103 + $env:ANTHROPIC_AUTH_TOKEN = $env:MINIMAX_API_KEY
104 + cc minimax $args
94 105 }

jetsung revised this gist 6 months ago. Go to revision

1 file changed, 17 insertions

profile.ps1

@@ -74,4 +74,21 @@ function cc {
74 74 & claude
75 75 }
76 76 }
77 + }
78 +
79 + function claude-modelscope-default {
80 + # 1. 构造要执行的完整命令数组
81 + $cmd = @('claude', '--settings', "$HOME\.claude\settings.json-mscope") + $args
82 +
83 + # 2. 打印命令(用双引号包裹每个参数,方便阅读)
84 + $cmdStr = $cmd | ForEach-Object { '"{0}"' -f $_ }
85 + Write-Host "Executing: $cmdStr" -ForegroundColor Cyan
86 +
87 + # 3. 真正执行(使用 & 调用运算符 + 参数展开)
88 + & $cmd[0] $cmd[1..($cmd.Count-1)]
89 + }
90 +
91 + function claude-modelscope {
92 + $env:ANTHROPIC_AUTH_TOKEN = $env:MODELSCOPE_API_KEY
93 + cc modelscope $args
77 94 }

jetsung revised this gist 6 months ago. Go to revision

1 file changed, 60 insertions, 39 deletions

profile.ps1

@@ -1,56 +1,77 @@
1 1 function cc {
2 2 param ([Parameter(Position=0, ValueFromRemainingArguments=$true)][string[]]$UserArgs)
3 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
4 + $defaultFile = "$HOME\.claude\settings.json"
5 + $selected = $env:CLAUDE_DEFAULT # 完全靠环境变量控制默认(可为空)
12 6 $actualArgs = @()
7 + $i = 0
13 8
14 - for($i=0; $i -lt $UserArgs.Count; $i++) {
15 - $arg = $UserArgs[$i]
16 - # 支持 cc modescope(无 -)和 cc -mscope 两种写法
17 - if ($arg -match '^-?(.+)$') {
9 + # 第一参数:支持 modelscope / -modelscope / -anything
10 + if ($UserArgs.Count -gt 0) {
11 + $first = $UserArgs[0]
12 + if ($first -match '^-?(.+)') {
18 13 $selected = $matches[1]
19 - continue
14 + $i = 1
20 15 }
21 - $actualArgs += $arg
22 16 }
23 17
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
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 + }
35 39 } else {
36 - $settings = $selectedFile
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 参数)"
37 48 }
38 49
39 - # 构建命令
40 - $cmd = @('claude')
41 - if ($settings) { $cmd += '--settings', $settings }
42 - $cmd += $actualArgs
50 + Write-Host $log -ForegroundColor Yellow
51 + Write-Host "最终实际使用的配置文件: $($settingsFile ? $settingsFile : '无')" -ForegroundColor Green
52 + Write-Host "===============================" -ForegroundColor DarkGray
53 + # ======================================
43 54
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
55 + $baseCmd = @('claude')
56 + if ($settingsFile) { $baseCmd += '--settings', $settingsFile }
49 57
50 - # 执行
51 - if ($settings) {
52 - & claude --settings $settings @actualArgs
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 + }
53 68 } else {
54 - & claude @actualArgs
69 + # 交互模式
70 + Write-Host "进入交互模式(按 Ctrl+C 退出)" -ForegroundColor White
71 + if ($settingsFile) {
72 + & claude --settings $settingsFile
73 + } else {
74 + & claude
75 + }
55 76 }
56 77 }

jetsung revised this gist 6 months ago. Go to revision

1 file changed, 56 insertions

profile.ps1(file created)

@@ -0,0 +1,56 @@
1 + function 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 + }
Newer Older