You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在pre-commit hook的entry值中转义含空格的参数?

Pre-commit Hook Entry 参数转义规则与跨平台兼容性

pre-commit 的 entry 字段不依赖系统自带的 shell(不管是 Bash、Cmd 还是 PowerShell),它用 Python 的 shlex.split() 来解析命令,所以有一套跨平台一致的分词规则:

  • 带空格的参数直接用双引号包裹即可,不用纠结不同平台的 shell 转义差异
  • 别用反斜杠转义空格——这种写法只在类 Unix 系统的 shell 里有效,但会被 pre-commit 当成参数的一部分,导致 Nix 收到nix-command\ flakes这类错误参数

针对你的需求,正确的 hook 配置应该是这样:

- repo: local
  hooks:
    - id: nix-check-flake
      name: Nix Flake Check
      entry: nix --extra-experimental-features "nix-command flakes" flake check
      language: system
      files: \.nix$

pre-commit 解析这个 entry 后,会生成参数列表:['nix', '--extra-experimental-features', 'nix-command flakes', 'flake', 'check'],不管在 Linux、macOS 还是 Windows 上,这个拆分结果完全一致,能满足你无需依赖用户配置就能运行 nix flake check 的需求。

额外提醒:别用单引号包裹参数,shlex.split() 在 Windows 下对单引号的处理和类 Unix 系统不一致,双引号是跨平台最安全的选择。

内容的提问来源于stack exchange,提问作者Ginger Jesus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 00:57:36