"""Offline Ruby 3.x semantic verification lab for Chapter 07 (command-line-options)."""
import json
import re
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def parse_short_bundled(argv):
flags = []
exprs = []
files = []
i = 0
while i < len(argv):
tok = argv[i]
if tok.startswith("--"):
flags.append(tok)
elif tok.startswith("-") and len(tok) > 1:
j = 1
while j < len(tok):
ch = tok[j]
if ch == "e":
flags.append("-e")
rest = tok[j + 1 :]
if rest:
exprs.append(rest)
else:
i += 1
exprs.append(argv[i])
break
else:
flags.append(f"-{ch}")
j += 1
else:
files.append(tok)
i += 1
return {"flags": flags, "exprs": exprs, "files": files}
def case_cli_invocation_and_bundled_short_flags():
bundled = parse_short_bundled(["-ne", "print if /Ruby/", "/usr/share/bin"])
separated = parse_short_bundled(["-n", "-e", "print if /Ruby/", "/usr/share/bin"])
expected = {
"equivalent": True,
"flags": ["-n", "-e"],
"exprs": ["print if /Ruby/"],
"files": ["/usr/share/bin"],
}
actual = {
"equivalent": bundled == separated,
"flags": bundled["flags"],
"exprs": bundled["exprs"],
"files": bundled["files"],
}
return {
"name": "cli_invocation_and_bundled_short_flags",
"summary": "验证 ruby -ne '...' 与 ruby -n -e '...' 单字符短选项合并的等价解析规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_inline_eval_and_n_p_a_loop_modes():
lines = ["Ruby:3.3:MRI\n", "Python:3.12:CPython\n", "Ruby:3.4:YJIT\n"]
# -n -e 'print if /Ruby/'
n_out = [ln for ln in lines if re.search(r"Ruby", ln)]
# -p -l -e '$_.upcase!'
p_out = [ln.rstrip("\n").upper() + "\n" for ln in lines]
# -a -n -F: -> $F fields
a_fields = [ln.rstrip("\n").split(":")[0] for ln in lines]
expected = {
"n_matched_count": 2,
"p_first_line": "RUBY:3.3:MRI\n",
"a_first_columns": ["Ruby", "Python", "Ruby"],
}
actual = {
"n_matched_count": len(n_out),
"p_first_line": p_out[0],
"a_first_columns": a_fields,
}
return {
"name": "inline_eval_and_n_p_a_loop_modes",
"summary": "模拟 -n、-p、-l 与 -a/-F 自动按行读取、回显及字段拆分($F)的执行模型",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_load_path_require_and_syntax_check():
load_path = ["/usr/lib/ruby/3.3.0"]
load_path.insert(0, "./lib")
preloaded = ["json", "set"]
syntax_only = {"flag": "-c", "executed_body": False, "syntax_ok": True}
expected = {
"load_path_head": "./lib",
"preloaded_count": 2,
"c_check_no_exec": True,
}
actual = {
"load_path_head": load_path[0],
"preloaded_count": len(preloaded),
"c_check_no_exec": (syntax_only["flag"] == "-c" and not syntax_only["executed_body"] and syntax_only["syntax_ok"]),
}
return {
"name": "load_path_require_and_syntax_check",
"summary": "校验 -I 扩展 $LOAD_PATH、-r 预加载库及 -c 仅语法检查不执行主体的行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_legacy_kcode_and_safe_taint_deprecation():
option_status = {
"-K": "deprecated_since_1_9",
"-T": "removed_in_ruby_3_0",
"--verbose": "sets_$VERBOSE_true",
"--debug": "sets_$DEBUG_true",
"-C": "alias_for_-X",
}
expected = {
"kcode_deprecated": True,
"safe_level_removed_in_3": True,
"chdir_alias_match": True,
}
actual = {
"kcode_deprecated": option_status["-K"] == "deprecated_since_1_9",
"safe_level_removed_in_3": option_status["-T"] == "removed_in_ruby_3_0",
"chdir_alias_match": option_status["-C"] == "alias_for_-X",
}
return {
"name": "legacy_kcode_and_safe_taint_deprecation",
"summary": "核对 -K、-T/$SAFE 在现代 Ruby 3.x 中的废弃与移除状态以及 -C/-X、--verbose/--debug 等价关系",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_cli_invocation_and_bundled_short_flags(),
case_inline_eval_and_n_p_a_loop_modes(),
case_load_path_require_and_syntax_check(),
case_legacy_kcode_and_safe_taint_deprecation(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。
入口文件:projects/ruby-command-line-options-lab/explore-command-line-options.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-command-line-options.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2442.zip
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip