|
1 | 1 | --- |
2 | 2 | name: mirascript-cli |
3 | | -description: Guide AI to use MiraScript CLI to run and format MiraScript code |
4 | | -author: CloudPSS |
5 | | -license: MIT |
| 3 | +description: 使用官方 MiraScript CLI 运行、验证、调试和格式化 MiraScript 脚本或模板。需要执行 .mira/.miratpl 文件、验证生成的代码片段、注入变量、检查 CLI 错误、进入 REPL,或安全地预览及写回格式化结果时使用。 |
6 | 4 | --- |
7 | 5 |
|
8 | | -# MiraScript CLI 技能 |
| 6 | +# 使用 MiraScript CLI |
9 | 7 |
|
10 | | -MiraScript CLI 是验证、运行和格式化 MiraScript 代码的官方命令行工具。本技能指导 AI **在生成代码后主动使用 CLI 验证代码正确性并根据错误迭代修复**。 |
| 8 | +## 选择调用方式 |
11 | 9 |
|
12 | | -## When to use |
| 10 | +优先使用目标项目已固定的版本: |
13 | 11 |
|
14 | | -当需要执行、验证或格式化 MiraScript 代码时使用本技能,尤其是在生成代码后应立即用 CLI 进行验证。 |
15 | | - |
16 | | ---- |
17 | | - |
18 | | -## 安装与调用方式 |
19 | | - |
20 | | -无需全局安装,可直接通过包管理器调用最新版本: |
21 | | - |
22 | | -```sh |
23 | | -# 推荐方式(三选一) |
24 | | -npx @mirascript/cli <command> |
25 | | -pnpm dlx @mirascript/cli <command> |
26 | | -yarn dlx @mirascript/cli <command> |
| 12 | +```powershell |
| 13 | +pnpm exec mirascript --version |
27 | 14 | ``` |
28 | 15 |
|
29 | | -若已在本地项目安装(`devDependencies`),则直接使用: |
| 16 | +按以下顺序选择命令: |
30 | 17 |
|
31 | | -```sh |
32 | | -npx mirascript <command> |
33 | | -``` |
34 | | - |
35 | | ---- |
| 18 | +1. 在已安装 `@mirascript/cli` 的 pnpm 项目中使用 `pnpm exec mirascript`。 |
| 19 | +2. 已有全局或环境提供的二进制时使用 `mirascript`。 |
| 20 | +3. 项目未安装 CLI 且允许联网下载时,使用 `pnpm dlx @mirascript/cli` 或 `npx --package=@mirascript/cli mirascript`。不要仅为验证代码擅自修改项目依赖。 |
36 | 21 |
|
37 | | -## 命令:`run`(默认命令) |
| 22 | +后文用 `pnpm exec mirascript` 展示命令;若选择了其他入口,保持子命令和参数不变。在自动化流程中始终传入脚本、`--eval` 或子命令,避免无意进入交互式 REPL。 |
38 | 23 |
|
39 | | -执行 MiraScript 脚本,是 CLI 的默认命令(可省略 `run`)。 |
| 24 | +## 运行与验证 |
40 | 25 |
|
41 | | -### 语法 |
| 26 | +运行脚本文件: |
42 | 27 |
|
43 | | -```sh |
44 | | -mirascript run [options] [script] |
45 | | -# 或省略 run: |
46 | | -mirascript [options] [script] |
| 28 | +```powershell |
| 29 | +pnpm exec mirascript run script.mira |
47 | 30 | ``` |
48 | 31 |
|
49 | | -### 选项 |
50 | | - |
51 | | -| 选项 | 说明 | |
52 | | -| -------------------------- | ------------------------------------------------ | |
53 | | -| `-e, --eval <script>` | 直接执行字符串代码(不传文件路径时使用) | |
54 | | -| `-v, --variable <key=val>` | 设置全局变量,值支持 MiraScript 表达式;可多次用 | |
55 | | -| `-t, --template` | 以模板模式运行(`.miratpl` 文件或模板字符串) | |
56 | | -| `--no-template` | 强制以脚本模式运行 | |
57 | | -| `--timeout <ms>` | 执行超时时间(毫秒),`0` 表示不超时,默认 3000 | |
| 32 | +`run` 是默认子命令,因此也可写 `pnpm exec mirascript script.mira`。显式写出 `run` 可让自动化日志更清楚。 |
58 | 33 |
|
59 | | -### 示例 |
| 34 | +对不含复杂 shell 字符的短代码使用 `--eval`: |
60 | 35 |
|
61 | | -```sh |
62 | | -# 执行脚本文件 |
63 | | -mirascript script.mira |
| 36 | +```powershell |
| 37 | +pnpm exec mirascript run --eval 'let x = 1 + 2; x' |
| 38 | +``` |
64 | 39 |
|
65 | | -# 执行内联代码片段(AI 验证代码的主要方式) |
66 | | -mirascript -e "let x = 1 + 2; debug_print(x)" |
| 40 | +对多行代码、插值或复杂引号优先使用 stdin。PowerShell 使用单引号 here-string,防止 PowerShell 展开 MiraScript 的 `$`: |
67 | 41 |
|
68 | | -# 使用标准输入传递多行代码(script 为 -) |
69 | | -mirascript - <<EOF |
| 42 | +```powershell |
| 43 | +@' |
70 | 44 | fn factorial(n) { |
71 | | - if n <= 1 { return 1; } |
72 | | - n * factorial(n - 1) |
| 45 | + if n <= 1 { 1 } else { n * factorial(n - 1) } |
73 | 46 | } |
74 | | -factorial(10) |
75 | | -EOF |
76 | | - |
77 | | -# 带变量执行 |
78 | | -mirascript -e "debug_print(name)" -v "name='Alice'" |
79 | | - |
80 | | -# 变量值为 MiraScript 表达式 |
81 | | -mirascript -e "debug_print(data::map(fn { it * 2 }))" -v "data=[1,2,3]" |
82 | | - |
83 | | -# 模板模式执行文件 |
84 | | -mirascript --template template.miratpl |
85 | | - |
86 | | -# 不超时执行(长计算) |
87 | | -mirascript --timeout 0 heavy.mira |
| 47 | +factorial(5) |
| 48 | +'@ | pnpm exec mirascript run - |
88 | 49 | ``` |
89 | 50 |
|
90 | | -### 输出行为 |
91 | | - |
92 | | -- **脚本模式**:打印最终表达式的值(格式化后) |
93 | | -- **模板模式**:打印渲染结果字符串 |
94 | | -- **错误**:错误信息输出到 stderr,退出码 `2`;文件不存在退出码 `2`,权限不足退出码 `3` |
95 | | - |
96 | | ---- |
97 | | - |
98 | | -## 命令:`format` |
99 | | - |
100 | | -格式化 MiraScript 脚本文件。 |
101 | | - |
102 | | -### 语法 |
| 51 | +POSIX shell 使用带引号的 heredoc: |
103 | 52 |
|
104 | 53 | ```sh |
105 | | -mirascript format [options] <script...> |
| 54 | +pnpm exec mirascript run - <<'MIRA' |
| 55 | +let values = [1, 2, 3]; |
| 56 | +values::map(fn { it * 2 }) |
| 57 | +MIRA |
106 | 58 | ``` |
107 | 59 |
|
108 | | -`<script...>` 支持多个文件路径、glob 模式,或 `-` 表示从标准输入读取。 |
| 60 | +脚本模式会在 stdout 打印最后表达式的值,`debug_print` 会产生额外输出。验证时同时检查退出码、stderr 和预期 stdout;不要只检查“命令有输出”。 |
109 | 61 |
|
110 | | -### 选项 |
| 62 | +### 常用运行选项 |
111 | 63 |
|
112 | | -| 选项 | 说明 | |
113 | | -| ---------------- | --------------------------------------------- | |
114 | | -| `-w, --write` | 直接将格式化结果写回文件(否则输出到 stdout) | |
115 | | -| `-t, --template` | 对无法通过扩展名推断类型的文件使用模板模式 | |
| 64 | +| 选项 | 用途 | |
| 65 | +| ---------------------------- | -------------------------------------------------------- | |
| 66 | +| `-e, --eval <script>` | 执行内联脚本;不可同时传脚本路径 | |
| 67 | +| `-v, --variable <key=value>` | 注入全局变量;可重复使用,值优先按 MiraScript 表达式解析 | |
| 68 | +| `-t, --template` | 对 `--eval`、stdin 或需强制覆盖的输入启用模板模式 | |
| 69 | +| `--no-template` | 即使文件名为 `.miratpl` 也强制使用脚本模式 | |
| 70 | +| `--timeout <ms>` | 设置检查点超时;默认 3000,`0` 表示不超时 | |
116 | 71 |
|
117 | | -### 示例 |
| 72 | +```powershell |
| 73 | +pnpm exec mirascript run script.mira --variable "name='Mira'" --variable 'count=3' |
| 74 | +pnpm exec mirascript run --template --eval 'Hello, $name!' --variable "name='World'" |
| 75 | +``` |
118 | 76 |
|
119 | | -```sh |
120 | | -# 检查格式化结果(输出到 stdout,不修改文件) |
121 | | -mirascript format script.mira |
| 77 | +运行文件时会根据 `.miratpl` 扩展名自动选择模板模式;对 stdin 和 `--eval` 必须显式传 `--template`。仅对可信且确定会终止的代码使用 `--timeout 0`。 |
122 | 78 |
|
123 | | -# 直接格式化并写回文件 |
124 | | -mirascript format -w script.mira |
| 79 | +## 格式化 |
125 | 80 |
|
126 | | -# 格式化目录下所有 .mira 文件 |
127 | | -mirascript format -w "src/**/*.mira" |
| 81 | +先预览,再决定是否写回: |
128 | 82 |
|
129 | | -# 从 stdin 格式化(管道) |
130 | | -echo 'let x=1+2' | mirascript format - |
| 83 | +```powershell |
| 84 | +pnpm exec mirascript format script.mira |
| 85 | +pnpm exec mirascript format --write script.mira |
131 | 86 | ``` |
132 | 87 |
|
133 | | ---- |
134 | | - |
135 | | -## AI 工作流:生成→验证→迭代 |
| 88 | +不带 `--write` 时,文件输入的格式化结果写到 stdout,并带 `// File: ...` 标题;不会执行专用的 check 模式。从 stdin 格式化时只输出格式化后的代码: |
136 | 89 |
|
137 | | -**生成 MiraScript 代码后,必须遵循以下验证流程:** |
138 | | - |
139 | | -### 1. 语法与运行时验证 |
| 90 | +```powershell |
| 91 | +@' |
| 92 | +let x=1+2; |
| 93 | +x |
| 94 | +'@ | pnpm exec mirascript format - |
| 95 | +``` |
140 | 96 |
|
141 | | -使用 `run -e` / `run -` 验证代码片段: |
| 97 | +为模板格式化显式传 `--template`: |
142 | 98 |
|
143 | | -```sh |
144 | | -npx mirascript -e "<生成的代码>" |
| 99 | +```powershell |
| 100 | +pnpm exec mirascript format --template template.miratpl |
145 | 101 | ``` |
146 | 102 |
|
147 | | -**注意**:`-e` 传入的代码会被当作完整脚本执行,最后一个表达式的值会被打印。 |
148 | | - |
149 | | -### 2. 解读错误并修复 |
| 103 | +当前 `format` 实现不会像 `run` 那样根据 `.miratpl` 文件名切换输入模式。对多个文件或 glob,把模式作为单个参数交给 CLI,并在 `--write` 后检查实际改动: |
150 | 104 |
|
151 | | -根据错误信息定位问题,修改代码后重新验证,直到运行成功。 |
| 105 | +```powershell |
| 106 | +pnpm exec mirascript format --write 'src/**/*.mira' |
| 107 | +``` |
152 | 108 |
|
153 | | -### 3. 典型验证场景 |
| 109 | +不要对超出任务范围的宽泛 glob 使用 `--write`。格式化时还要检查 stderr 和匹配到的文件;无匹配项或个别文件格式化失败时,退出码本身不足以证明全部成功。 |
154 | 110 |
|
155 | | -```sh |
156 | | -# 验证函数定义和调用 |
157 | | -mirascript - <<EOF |
158 | | -fn factorial(n) { |
159 | | - if n <= 1 { return 1; } |
160 | | - n * factorial(n - 1) |
161 | | -} |
162 | | -factorial(10) |
163 | | -EOF |
| 111 | +## REPL |
164 | 112 |
|
165 | | -# 验证带变量的逻辑 |
166 | | -mirascript - <<EOF |
167 | | -let items = [1, 2, 3, 4, 5]; |
168 | | -items::filter(fn { it > 2 })::map(fn { it * 2 }) |
169 | | -EOF |
| 113 | +仅在用户需要交互探索时运行: |
170 | 114 |
|
171 | | -# 验证模板 |
172 | | -mirascript --template -e "Hello, \${name}!" -v "name='World'" |
| 115 | +```powershell |
| 116 | +pnpm exec mirascript repl |
173 | 117 | ``` |
174 | 118 |
|
175 | | -### 4. 常见错误代码 |
176 | | - |
177 | | -| 退出码 | 含义 | |
178 | | -| ------ | --------------------------- | |
179 | | -| `0` | 成功 | |
180 | | -| `1` | 内部错误 | |
181 | | -| `2` | 语法/运行时错误或文件不存在 | |
182 | | -| `3` | 权限不足 | |
183 | | - |
184 | | -退出码非 `0` 时必须检查 stderr 输出并修复。 |
185 | | - |
186 | | ---- |
| 119 | +没有脚本路径和 `--eval` 的默认 `run` 也会进入 REPL。不要在非交互验证或 CI 中这样调用。 |
187 | 120 |
|
188 | | -## 注意事项 |
| 121 | +## 错误处理与完成标准 |
189 | 122 |
|
190 | | -- **多行代码**:在 shell 中传递多行代码时,使用 heredoc;在 PowerShell 中使用 `echo @''@| mirascript -` 避免 `npx` 传递参数错误 |
191 | | -- **引号转义**:`-v` 中的字符串字面量须注意 shell 引号转义,优先使用逐字字符串 `@"..."@` 避免转义冲突 |
192 | | -- **超时限制**:默认超时 3000ms,复杂算法或循环测试时加 `--timeout 0` |
| 123 | +- 退出码 `0` 表示命令进程成功;运行时编译或执行错误通常为 `2`,权限错误为 `3`,其他访问、内部或 stdin 格式化错误可能为 `1`。多文件格式化可能只记录单个文件的失败,因此仍要检查 stderr。 |
| 124 | +- 根据错误中的文件名、行列和诊断信息修复代码,并重新执行同一代表性输入。 |
| 125 | +- 对 `--variable` 解析、模板渲染、超时和宿主全局等行为分别保留针对性用例。 |
| 126 | +- 只有在退出码、stderr 和实际输出都符合预期后,才报告运行验证通过。 |
| 127 | +- 格式化写回后检查 diff,确认只修改了预期文件且语义未改变。 |
0 commit comments