【发布时间】:2020-06-25 17:38:10
【问题描述】:
我正在创建一个带有命令行界面的 Python 包,该命令行界面使用子命令模式:kevlar count、kevlar partition 等等。 CLI 运行良好,现在我正尝试将 CLI 添加到我的 Sphinx 文档中。在寻找解决方案时,我遇到了sphinxcontrib-autoprogram,它似乎完全符合我的要求,甚至明确处理子命令。但是当我执行 sphinx 构建时,出现以下错误。
sphinx-build -b html -d _build/doctrees . _build/html
Running Sphinx v1.6.3
loading pickled environment... not yet created
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 5 source files that are out of date
updating environment: 5 added, 0 changed, 0 removed
reading sources... [ 20%] cli
usage: sphinx-build [-h] [-v] [-l F] cmd ...
sphinx-build: error: argument cmd: invalid choice: 'html' (choose from 'reaugment', 'dump', 'novel', 'collect', 'mutate', 'assemble', 'filter', 'partition', 'count', 'localize')
make[1]: *** [html] Error 2
make: *** [doc] Error 2
似乎 sphinx 扩展不仅在创建 argparse 对象(预期),而且还在其上调用parse_args()(意外)。 “无效”html 参数来自 sphinx 命令行构建调用,但在某处被误认为是我的库 CLI 中的子命令之一。
我的语法似乎与 sphinxcontrib-autoprogram 文档相匹配。
.. autoprogram:: cli:parser
:prog: kevlar
什么可能导致这种行为?
我不确定这些细节是否与问题相关,但如果它们是:
- 解析器在
kevlar/cli/__init__.py:parser()中定义 - 每个命令的子解析器在kevlar/cli/ 的专用文件中定义(例如kevlar/cli/count.py)
【问题讨论】:
-
我不确定它是否有帮助,但这个问题让我想起了stackoverflow.com/q/6912025/407651(这是关于 optparse 而不是 argparse)。
标签: python python-sphinx argparse