蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

用本机帮助和补全命令核对速查表

PYTHONWindows、CPython 3.12.14 与 codex-cli 0.167.0-alpha.4.1。quickref_lab.py
quickref_lab.py70 行
"""Verify installed CLI parsing without opening a session, and parse local example data."""
from hashlib import sha256
import json
from pathlib import Path
import shutil
import subprocess
import sys
import tomllib


def main():
    executable = shutil.which("codex.exe") or shutil.which("codex")
    if not executable:
        raise RuntimeError("Codex CLI must already be installed")

    def invoke(args):
        return subprocess.run([executable, *args], capture_output=True, encoding="utf-8",
                              errors="replace", timeout=20)

    cases = []

    def check(name, data, operation, expected, actual):
        assert expected == actual, (name, expected, actual)
        cases.append(dict(name="quickref_" + name, input=data, operation=operation,
                          expected=expected, actual=actual))

    version = invoke(["--version"])
    check("version", ["--version"], "Identify the installed executable", [0, True],
          [version.returncode, version.stdout.startswith("codex-cli ")])
    for name, args, flags in (
        ("help", ["--help"], ["--model", "--sandbox", "--image", "--ask-for-approval"]),
        ("exec_help", ["exec", "--help"], ["--json", "--ephemeral", "--output-last-message"]),
        ("resume_help", ["resume", "--help"], ["--last", "--all"]),
    ):
        result = invoke(args)
        check(name, dict(args=args, help_terms=flags), "Read documented switches from installed CLI help",
              [0, True], [result.returncode, all(flag in result.stdout for flag in flags)])

    for name, args in (
        ("legacy_approval", ["--approval-mode", "ask"]),
        ("legacy_auto", ["--full-auto"]),
        ("legacy_effort", ["--reasoning-effort", "high"]),
    ):
        result = invoke([*args, "completion", "powershell"])
        check(name, [*args, "completion", "powershell"], "Check how the executable parses the historical flag",
              [2, True], [result.returncode, "unexpected argument" in result.stderr])

    args = ["--no-alt-screen", "completion", "powershell"]
    result = invoke(args)
    check("no_alt_screen", args, "Check global switch parsing while generating shell completion",
          [0, True], [result.returncode, "Register-ArgumentCompleter" in result.stdout])
    config = '[tui]\nalternate_screen = "never"\n'
    check("toml", config, "Parse sample TOML with Python and look up the nested setting", "never",
          tomllib.loads(config)["tui"]["alternate_screen"])
    fixture = [
        '{"type":"thread.started","thread_id":"fixture"}',
        '{"type":"item.completed","item":{"type":"agent_message","text":"检查完成"}}',
        '{"type":"turn.completed","usage":{"output_tokens":5}}',
    ]
    messages = [event["item"]["text"] for event in map(json.loads, fixture)
                if event["type"] == "item.completed" and event["item"]["type"] == "agent_message"]
    check("jsonl", fixture, "Read the message from independently authored JSONL fixture data",
          ["检查完成"], messages)
    print(json.dumps(dict(version={"python": sys.version.split()[0], "codex": version.stdout.strip()},
                          codex_sha256=sha256(Path(executable).read_bytes()).hexdigest(), cases=cases)))


if __name__ == "__main__":
    main()
本例需在 Windows、CPython 3.12.14 与 codex-cli 0.167.0-alpha.4.1。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows、CPython 3.12.14 与 codex-cli 0.167.0-alpha.4.1。

入口文件:projects/codex-quickref/quickref_lab.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 quickref_lab.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows、CPython 3.12.14 与 codex-cli 0.167.0-alpha.4.1。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a117.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习