Sphinx and argparse - autodocumenting command line scripts?

青春壹個敷衍的年華 提交于 2019-12-17 18:48:19

问题


I'm building a Python package, and using Sphinx to create the docs. Aside from my package code, I also include a lot of command line Python scripts, which use argparse. I was wondering if there is a way to get Sphinx to autodocument these scripts? The end goal would be a pretty-printed list of scripts, with the associated help print, arguments and options. And to be clear, I'm looking for a pre-existing way to do this, not a way to implement this myself.

This isn't as specific of a question as I usually ask on S.O., if there is a more appropriate S.E. site to post this question, please let me know. Thanks.


回答1:


You can use sphinxcontrib.programoutput to include the help messages from the command line in your documentation.

This is not specific to argparse but can be used to document any script printing help messages to the command line.




回答2:


Use sphinx-argparse extension:

http://sphinx-argparse.readthedocs.org/en/latest/




回答3:


You can use sphinxcontrib.autoprogram. pip install sphinxcontrib-autoprogram, then put

extensions += ['sphinxcontrib.autoprogram']

in your conf.py. To document command cli.py by importing cli with the argparse parser object parser (which can be a Python expression, like a function get_parser()), use

.. autoprogram:: cli:parser
   :prog: cli.py


来源:https://stackoverflow.com/questions/10886841/sphinx-and-argparse-autodocumenting-command-line-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!