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

前端 未结 6 2203
野性不改
野性不改 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 05:04

    Another idea on how to do this would be disabling the default behavior for -h and printing your own help screen, which can include the default one:

    from optparse import OptionParser
    
    parser = OptionParser(add_help_option=False, 
                          epilog="This can't be easily\n multilined")
    parser.add_option('-h', '--help', dest='help', action='store_true',
                      help='show this help message and exit')
    
    (options, args) = parser.parse_args()
    
    if options.help:
        parser.print_help()
        print 'now we have an epilog'
        print 'with as many lines as you wish'
        sys.exit()
    

    That is basically what the parser does with the default behavior of add_help_option=True, excluding of course the prints.

    But, in all honesty, I'd also prefer a way to simply add any given number of description lines in the beginning and in the end.

提交回复
热议问题