Python 选项解析器覆盖 '-h'

Python option parser overwrite '-h'

我有以下选择

parser = OptionParser()
parser.add_option('-a', '--all', action='store_true', dest='all', help='writes all header information')
parser.add_option('-h', '--file-header', action='store_true', dest='head',  help='prints the elf file header information')
parser.add_option('-l', '--program-header', action='store_true', dest='prog',  help='prints the program header')
parser.add_option('-S', '--section-header', action='store_true', dest='sec',  help='prints the section header')

当运行脚本我得到错误信息:

 optparse.OptionConflictError: option -h/--file-header: conflicting option string(s): -h

我知道通常-h是保留显示帮助的。但是我正在尝试为一些特殊的 elf 文件编写一个 ELF 文件 reader,因此我想使用 readelf 等相同的命令。 readelf 使用 -h 打印 header 信息。

是否有可能覆盖选项解析器中的 -h 选项或已修复?

创建解析器时,传递add_help_option=False。然后你就可以自己定义了:

parser =  OptionParser(add_help_option=False)