https://github.com/lwmacct/251215-go-pkg-scmd
为 Go 语言 AI Agent 生态提供 Claude Code slash-commands 兼容加载能力
https://github.com/lwmacct/251215-go-pkg-scmd
claude claude-code
Last synced: about 2 months ago
JSON representation
为 Go 语言 AI Agent 生态提供 Claude Code slash-commands 兼容加载能力
- Host: GitHub
- URL: https://github.com/lwmacct/251215-go-pkg-scmd
- Owner: lwmacct
- License: mit
- Created: 2025-12-22T08:56:41.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-12-22T11:41:35.000Z (6 months ago)
- Last Synced: 2025-12-23T22:46:43.974Z (6 months ago)
- Topics: claude, claude-code
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# scmd
Go 语言的 Claude Code Slash Commands 加载和管理库。
[](LICENSE)
[](https://pkg.go.dev/github.com/lwmacct/251215-go-pkg-scmd)
[](https://github.com/lwmacct/251215-go-pkg-scmd/actions/workflows/go-ci.yml)
[](https://codecov.io/gh/lwmacct/251215-go-pkg-scmd)
[](https://goreportcard.com/report/github.com/lwmacct/251215-go-pkg-scmd)
[](https://github.com/lwmacct/251215-go-pkg-scmd/tags)
## Table of Contents
- [Features](#features) `:31+11`
- [Installation](#installation) `:42+6`
- [Quick Start](#quick-start) `:48+48`
- [Basic Usage](#basic-usage) `:50+23`
- [Using Presets](#using-presets) `:73+23`
- [API Reference](#api-reference) `:96+56`
- [Manager](#manager) `:98+14`
- [Options](#options) `:112+18`
- [Presets](#presets) `:130+22`
- [Command File Format](#command-file-format) `:152+41`
- [Development](#development) `:193+16`
- [Related Links](#related-links) `:209+5`
## Features
- 兼容 Claude Code 的 slash commands 格式
- 支持项目命令 (`.claude/commands/`)、个人命令 (`~/.claude/commands/`)、插件命令
- 自动处理命令优先级:Project > Personal > Plugin
- 支持 YAML frontmatter 元数据解析
- 参数占位符替换:`$ARGUMENTS`、`$1`、`${N}`
- Bash 命令执行:`!`\`command\``
- 文件引用解析:`@path/to/file`
- 预设配置模式,简化初始化
## Installation
```shell
go get github.com/lwmacct/251215-go-pkg-scmd
```
## Quick Start
### Basic Usage
```go
package main
import "github.com/lwmacct/251215-go-pkg-scmd/pkg/scmd"
func main() {
// 创建管理器(自动检测目录)
mgr := scmd.NewDefault()
mgr.Reload()
// 列出所有命令
for _, cmd := range mgr.List() {
fmt.Printf("/%s - %s\n", cmd.FullName(), cmd.GetDescription())
}
// 执行命令
result, _ := mgr.Execute("review", []string{"file.go"})
fmt.Println(result)
}
```
### Using Presets
```go
// 默认预设
mgr := scmd.New(scmd.PresetDefault()...)
// 指定项目目录
mgr := scmd.New(scmd.PresetProject("/path/to/project")...)
// 安全模式(禁用 Bash 执行)
mgr := scmd.New(scmd.PresetSafe()...)
// 组合预设
mgr := scmd.New(append(
scmd.PresetProject("/myproject"),
scmd.PresetSafe()...,
)...)
// 从环境变量读取配置
// SCMD_PROJECT_DIR=/custom SCMD_SAFE_MODE=true
mgr := scmd.New(scmd.PresetFromEnv()...)
```
## API Reference
### Manager
```go
// 构造函数
New(opts ...Option) *Manager // 创建管理器
NewDefault() *Manager // 使用默认配置创建
// 方法
Reload() error // 重新加载命令
List() []*Command // 列出所有命令
Get(name string) (*Command, bool) // 获取指定命令
Execute(name string, args []string) (string, error) // 执行命令
```
### Options
```go
WithWorkDir(dir) // 设置工作目录(自动配置项目命令目录)
WithHomeDir(dir) // 设置 home 目录(自动配置个人命令目录)
WithProjectDir(dir) // 直接设置项目命令目录
WithPersonalDir(dir) // 直接设置个人命令目录
WithPluginDir(name, dir) // 添加插件命令目录
WithBashExecutor(e) // 自定义 Bash 执行器
WithFileResolver(r) // 自定义文件解析器
WithNoBash() // 禁用 Bash 执行
WithNoFileResolve() // 禁用文件引用解析
WithBashTimeout(seconds) // 设置 Bash 超时
WithBashShell(shell) // 设置 Shell 路径
WithBashWorkDir(dir) // 设置 Bash 执行工作目录
WithFileBaseDir(dir) // 设置文件解析器基础目录
```
### Presets
| 函数 | 用途 |
| --------------------------------- | -------------------------------- |
| `PresetDefault()` | 自动检测工作目录和用户主目录 |
| `PresetProject(dir)` | 指定项目目录 |
| `PresetFull(projectDir, homeDir)` | 同时配置项目和用户目录 |
| `PresetSafe()` | 禁用 Bash 和文件引用(安全模式) |
| `PresetNoBash()` | 仅禁用 Bash 执行 |
| `PresetFromEnv()` | 从环境变量读取配置 |
| `PresetClaudeCode()` | Claude Code 标准目录结构 |
**环境变量支持:**
| 变量 | 说明 |
| ------------------- | ----------------------- |
| `SCMD_PROJECT_DIR` | 项目命令目录 |
| `SCMD_PERSONAL_DIR` | 个人命令目录 |
| `SCMD_SAFE_MODE` | `true`/`1` 启用安全模式 |
| `SCMD_NO_BASH` | `true`/`1` 禁用 Bash |
| `SCMD_BASH_TIMEOUT` | Bash 超时秒数 |
## Command File Format
命令文件为 Markdown 格式(`.md`),支持 YAML frontmatter:
```markdown
---
description: 命令描述
argument-hint: "[file] [options]"
allowed-tools:
- Read
- Write
model: claude-3-5-sonnet
---
审查 @$1 文件的代码质量。
当前分支: !`git branch --show-current`
参数: $ARGUMENTS
```
**Frontmatter 字段:**
| 字段 | 类型 | 说明 |
| -------------------------- | -------- | -------------- |
| `description` | string | 命令描述 |
| `argument-hint` | string | 参数提示 |
| `allowed-tools` | []string | 允许的工具列表 |
| `model` | string | 指定模型 |
| `disable-model-invocation` | bool | 禁止模型调用 |
**占位符语法:**
| 语法 | 说明 |
| ---------------- | ------------------ |
| `$ARGUMENTS` | 所有参数的空格连接 |
| `$1`, `$2`, `$N` | 位置参数 |
| `${N}` | 带花括号的位置参数 |
| `!`\`cmd\`` | 执行 Bash 命令 |
| `@path` | 引用文件内容 |
## Development
```shell
# 初始化开发环境
pre-commit install
# 运行测试
go test ./pkg/scmd/... -v
# 运行 lint
golangci-lint run ./pkg/scmd/...
# 查看所有任务
task -a
```
## Related Links
- [Claude Code Slash Commands 官方文档](https://docs.anthropic.com/en/docs/claude-code/slash-commands)
- [Taskfile](https://taskfile.dev) - 项目 CLI 管理
- [Pre-commit](https://pre-commit.com/) - Git hooks 管理