跳到正文
FunCoding

搜索

搜索文档、智能体、博客、Skill 和 MCP

Lint、测试与脚本化

让 Aider 在每次编辑后自动 Lint 与测试并修复,用 --message 在 shell 脚本里批量调用,以及 Python 接口。

Lint

Aider 对主流语言内置了 linter,并会自动检查它改过的文件;也可以用 --lint-cmd <cmd> 指定自己的 linter(命令接受文件名,有错误时在 stdout/stderr 输出并返回非零退出码)。用 --no-auto-lint 关闭自动 Lint;按语言分别指定用 --lint "language: cmd"。

很多人拿格式化工具当 linter,但它们改了文件会返回非零,让 Aider 误以为有 lint 错误。办法是包一层脚本,运行两次:第一次可能改动文件并失败,第二次若没有真正问题就返回 0:

#!/bin/bash
pre-commit run --files "$@" >/dev/null \
    || pre-commit run --files "$@"

测试

用 /test <命令> 运行测试(不带额外参数);命令返回非零时 Aider 会读取输出并尝试修复。想在每次 AI 编辑后自动跑测试,用 --test-cmd <命令> 加 --auto-test。编译型语言可以用这两个选项做编译检查:--lint-cmd 写一个既 Lint 又编译单文件的脚本;--test-cmd 做整个项目的构建加测试,例如 --test-cmd "dotnet build && dotnet test"。/run 可手动运行代码,把输出共享给 Aider。

脚本化

命令行:--message(-m)给一条指令,Aider 完成一件事、应用编辑后退出;--message-file(-f)从文件读取消息。

aider --message "make a script that prints hello" hello.js
for FILE in *.py ; do
    aider --message "add descriptive docstrings to all the functions" $FILE
done

脚本里常用的选项:--yes(所有确认都答是)、--no-stream、--no-auto-commits、--no-dirty-commits、--dry-run(不修改文件)、--commit(用合适的信息提交所有挂起的改动然后退出)。Python 接口:

from aider.coders import Coder
from aider.models import Model

coder = Coder.create(main_model=Model("gpt-4-turbo"), fnames=["greeting.py"])
coder.run("make a script that prints hello world")
coder.run("/tokens")

文档注明 Python 脚本接口没有正式支持或文档化,后续版本可能不保证向后兼容。