我正在爲各種子分析器編寫一個Python模塊的參數分析器。我的目標是有一個分享的內容,其參數的構造函數傳遞給多個孩子一個說法:Python argparse:獲取幫助字符串中的子分析器程序的名稱
from argparse import ArgumentParser
parser = ArgumentParser(prog = 'master')
parser1 = ArgumentParser(help = None)
parser1.add_argument('foo', type = int, help = 'Number of times to process %(prog)s') # Line of interest
parser2 = ArgumentParser(help = None)
parser2.add_argument('--bar', type = int, default = 0, help = 'Start at this number')
parser3 = ArgumentParser(help = None)
parser3.add_argument('--baz', type = str, default = 'DEFAULT', help = 'Init file with this text')
subparsers = parser.add_subparsers()
sp1 = subparsers.add_parser('prog1', parents = [parser1, parser2])
sp2 = subparsers.add_parser('prog2', parents = [parser1, parser3])
parser.parse_args('prog1 -h'.split())
所需的輸出會是這樣的
usage: master prog1 [-h] [--bar BAR] foo
positional arguments:
foo Number of times to process prog1
optional arguments:
-h, --help show this message and exit
--bar Start at this number
當我使用這個確切的設置,我得到master prog1
代替的幫助字符串中的prog1
。我應該在標記爲#Line of interest
的行中更改哪些內容以獲得理想的結果?