Frage

I have seen questions about displaying pydoc in the output of argparse's help, but I haven't seen anything about the reverse, which to me, seems more useful.

Is there any way to have pydoc automagically render the synopsis and per-item help description for a Python program?

War es hilfreich?

Lösung

You can access the help string with the format_help() method. You could use that to add to your script's doc string. One simple example:

"""
This is my script.
"""

import argparse

def _make_parser():
    p = argparse.ArgumentParser()
    p.add_argument("foo", help="Specify foo")
    x = p.add_mutually_exclusive_group(required=True)
    x.add_argument('--uid')
    x.add_argument('--username')
    return p

_p = _make_parser()

__doc__ += _p.format_help()

if __name__ == '__main__':
    args = _p.parse_args()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top