feat: 添加自启动机制,移除 --advice 参数

- 创建 bootstrap.py 承载实际 CLI 逻辑
- 重写 lyxy_document_reader.py 为轻量入口,自动检测依赖并启动
- 使用 subprocess.run() 实现跨平台兼容的自启动
- 移除 --advice 参数及相关测试
- 更新文档和规范,简化使用方式
This commit is contained in:
2026-03-11 23:49:39 +08:00
parent e67ec24dfd
commit 229f17bfee
8 changed files with 252 additions and 202 deletions

View File

@@ -29,7 +29,9 @@ def cli_runner():
if str(scripts_dir) not in sys.path:
sys.path.insert(0, str(scripts_dir))
from lyxy_document_reader import main
# 直接调用 bootstrap.main() 而不是 lyxy_document_reader.main()
# 因为 lyxy_document_reader 会调用 subprocess无法捕获输出
from bootstrap import main
# 保存原始 sys.argv 和 sys.exit
original_argv = sys.argv
@@ -46,7 +48,7 @@ def cli_runner():
try:
# 设置命令行参数
sys.argv = ['lyxy_document_reader'] + args
sys.argv = ['bootstrap'] + args
sys.exit = mock_exit
# 捕获输出

View File

@@ -4,48 +4,6 @@ import pytest
import os
class TestCLIAdviceOption:
"""测试 CLI --advice 参数功能。"""
def test_advice_option_pdf(self, cli_runner):
"""测试 -a/--advice 选项对 PDF 文件。"""
stdout, stderr, exit_code = cli_runner(["test.pdf", "-a"])
assert exit_code == 0
assert "文件类型: PDF" in stdout
assert "[uv 命令]" in stdout
assert "[python 命令]" in stdout
def test_advice_option_docx(self, cli_runner):
"""测试 --advice 选项对 DOCX 文件。"""
stdout, stderr, exit_code = cli_runner(["test.docx", "--advice"])
assert exit_code == 0
assert "文件类型: DOCX" in stdout
def test_advice_option_url(self, cli_runner):
"""测试 --advice 选项对 URL。"""
stdout, stderr, exit_code = cli_runner(["https://example.com", "--advice"])
assert exit_code == 0
assert "文件类型: HTML" in stdout
def test_advice_option_unknown(self, cli_runner):
"""测试 --advice 选项对未知文件类型。"""
stdout, stderr, exit_code = cli_runner(["test.xyz", "--advice"])
assert exit_code != 0
output = stdout + stderr
assert "无法识别" in output or "错误" in output
def test_advice_option_xls(self, cli_runner):
"""测试 --advice 选项对 XLS 文件。"""
stdout, stderr, exit_code = cli_runner(["test.xls", "--advice"])
assert exit_code == 0
assert "文件类型: XLS" in stdout
class TestCLIDefaultOutput:
"""测试 CLI 默认输出功能。"""

View File

@@ -131,7 +131,7 @@ class TestGeneratePythonCommand:
script_path="scripts/lyxy_document_reader.py"
)
assert python_cmd == "python scripts/lyxy_document_reader.py input.pdf"
assert pip_cmd == "pip install pkg1 pkg2"
assert pip_cmd == "pip install pyarmor pkg1 pkg2"
class TestFormatAdvice: