"""Inspect a specified Claude Code executable from a temporary configuration."""
import argparse
import json
import os
from pathlib import Path
import re
import subprocess
from tempfile import TemporaryDirectory
def inspect(executable):
with TemporaryDirectory(prefix="claude-local-check-") as tmp:
root = Path(tmp)
env = {k: os.environ[k] for k in ("SYSTEMROOT", "WINDIR", "PATH", "TEMP", "TMP", "COMSPEC") if k in os.environ}
env.update(CLAUDE_CONFIG_DIR=str(root / "config"), CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC="1",
DISABLE_AUTOUPDATER="1", DISABLE_TELEMETRY="1", NO_COLOR="1")
def command(*args, expected=0):
process = subprocess.run([str(executable), *args], cwd=root, env=env,
capture_output=True, text=True, encoding="utf-8", timeout=15)
if process.returncode != expected:
raise RuntimeError(f"Unexpected exit status for {args}: {process.returncode}")
return process.stdout.strip()
version = command("--version")
help_text = command("--help")
auth = json.loads(command("auth", "status", "--json", expected=1))
flags = ["--print", "--continue", "--resume"]
subcommands = re.findall(r"(?m)^ ([a-z][a-z-]*) (?=\s{2})", help_text.partition("Commands:")[2])
install_help = command("install", "--help")
update_help = command("update", "--help")
auth_help = command("auth", "--help")
return {
"version": version,
"session_options": [flag for flag in flags if flag in help_text],
"supports_prompt_argument": "[prompt]" in help_text.splitlines()[0],
"authentication": {key: auth[key] for key in ("loggedIn", "authMethod")},
"has_commit_subcommand": "commit" in subcommands,
"install_help": install_help.splitlines()[0],
"update_help": update_help.splitlines()[0],
"auth_actions": [name for name in ("login", "logout", "status")
if re.search(rf"(?m)^ {name}\b", auth_help)],
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("claude_executable", type=Path)
args = parser.parse_args()
print(json.dumps(inspect(args.claude_executable.resolve(strict=True)), indent=2))
if __name__ == "__main__":
main()
本例需在 练习在 Windows x64、CPython 3.12.14 和 Claude Code 2.1.325 下验证。第一个项目测试使用 Git。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:练习在 Windows x64、CPython 3.12.14 和 Claude Code 2.1.325 下验证。第一个项目测试使用 Git。
入口文件:projects/claude-code-install/check_cli.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
check_cli.py,或下载右侧课程工程包。 - 确认本地已准备 练习在 Windows x64、CPython 3.12.14 和 Claude Code 2.1.325 下验证。第一个项目测试使用 Git。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a3117.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3131.zip