我正在创建一个使用子命令模式的命令行界面的Python包:kevlar count
,kevlar partition
等等。 CLI的工作原理非常奇妙,现在我试图将CLI添加到我的Sphinx文档中。在寻找解决方案时,我碰到了sphinxcontrib-autoprogram,这似乎正是我想要的,甚至明确地处理子命令。但是当我执行狮身人面像构建时,出现以下错误。抓取解析器后sphinxcontrib-autoprogram解析参数?
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
这似乎是狮身人面像的扩展,不仅创造了argparse对象(预期),但也呼吁它(意外)parse_args()
。 “无效的”html
参数来自sphinx命令行构建调用,但被误认为是来自我的图书馆CLI的子命令之一。
我的语法似乎与sphinxcontrib-autoprogram文档相匹配。
.. autoprogram:: cli:parser
:prog: kevlar
什么可能导致此行为?
我不知道,如果这些细节有关的问题,但如果他们是:
- 解析器在
kevlar/cli/__init__.py:parser()
- 定义每个命令的subparsers定义在专用文件中kevlar/cli/(例如kevlar/cli/count.py)
我不确定它是否有帮助,但是这个问题让我想起了https://stackoverflow.com/q/6912025/407651(这是关于optparse而不是argparse)。 – mzjn