How do I format positional argument help using Python's optparse?

后端 未结 4 1256
刺人心
刺人心 2021-02-02 09:57

As mentioned in the docs the optparse.OptionParser uses an IndentedHelpFormatter to output the formatted option help, for which which I found some API

4条回答
  •  不知归路
    2021-02-02 10:36

    I'd be interested in a clean solution to this; I wasn't able to come up with one. The OptionParser really focuses entirely on the options; it doesn't give you anything to work with position args, as far as I've been able to find.

    What I did was to generate a list of little documentation blocks for each of my positional arguments, using \ts to get the right spacing. Then I joined them with newlines, and appended that to the 'usage' string that gets passed to the OptionParser.

    It looks fine, but it feels silly, and of course that documentation ends up appearing above the list of options. I haven't found any way around that, or how to do any complex stuff, i.e. a given set of options is described beneath the description for a positional arg, because they only apply to that arg.

    I looked at monkey-patching OptionParser's methods and I remember (this was a year or so ago) that it wouldn't have been that difficult, but I didn't want to go down that path.

提交回复
热议问题