Skip to content

[1417] feat(gfsh): 完整实现 type 内置命令并支持 -afptP 选项 - #104

Merged
da-liii merged 2 commits into
mainfrom
da/1417/gfsh_type
Nov 30, 2026
Merged

da-liii merged 2 commits into
mainfrom
da/1417/gfsh_type

Conversation

@da-liii

@da-liii da-liii commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

变更内容

在 gfsh 中完整实现 type 内置命令:

  1. 选项支持:
    • -t:仅输出类型(builtin、keyword、file);未找到时静默且退出码 1。
    • -p:仅在目标解析为文件时输出可执行文件路径;内建/关键字不输出但返回 0;未找到返回 1。
    • -P:强制在 PATH 中搜索外部文件(忽略关键字与内建命令);未找到外部文件返回 1。
    • -a:列出全部位置(支持与 -t、-p 组合)。
    • 支持多选项组合覆盖与 -- 结束选项解析;未知选项提示并返回退出码 2。
  2. 命令识别与路径检索:
    • 识别 shell 关键字(if、for、while 等)与内建命令(cd、echo、type 等)。
    • 包含 / 或 \\ 的路径跳过关键字和内建匹配直接检查可执行权限。
    • 跨平台(POSIX 与 Windows PATHEXT)搜索 PATH 目录下的可执行文件。
  3. 求值与解析联动:
    • eval.scm 中内建分派 type,支持输出重定向及退出码透传,以及命令替换内存捕获。
    • 修复 parser.scm 对命令参数位置保留关键字的词法解析限制。
  4. 测试与质量:
    • 补齐 eval-type-test.scm 单元测试及集成测试,通过 gf check 与 gf fmt。

此前 type --help 被当成短选项逐字符解析,遇到第二个 - 后报
"gfsh: type: --: invalid option" 并以退出码 2 失败,与 bash 约定不符。

按 bash 内建约定(CASE_HELPOPT):--help 打印长帮助到 stdout 并返回 0;
-h 因 h 不在 [-afptP] 中仍按无效选项报错(退出码 2),与真实 bash 一致。

新增 type --help / type --help cd 的单元测试。
@da-liii
da-liii merged commit 45be373 into main Nov 30, 2026
8 checks passed
@da-liii
da-liii deleted the da/1417/gfsh_type branch November 30, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant