python optparse, how to include additional info in usage output?

前端 未结 6 2202
野性不改
野性不改 2021-02-01 04:03

Using python\'s optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this:

usage: ch         


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 04:45

    Elaborating on the winning answer (which helped me solve the same problem in my own code), one quick-and-dirty option is to directly override the class's method with an identity method:

    optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog
    optparser = optparse.OptionParser(epilog=helptext)
    

    to get helptext printed as a verbatim epilog.

    I think this overrides the epilog formatting for all uses of the OptionParser class in your program, however, so all such epilogs must be passed in verbatim where you use OptionParser elsewhere in your program.

提交回复
热议问题