ArgumentParser -h 在 parse_known_args 之后不打印选项

ArgumentParser -h not printing options after parse_known_args

我有一个带有一长串可选参数的脚本,我最近在其中添加了通过文件传递参数的选项。代码如下:我首先添加 from_file args,然后添加 parse_known_args。除了 -h 标志外,一切正常。调用此标志时的输出仅指在 parse_known_args 调用之前添加的参数。

问题: 如何在 parse_known_args 调用后获得识别所有参数的帮助选项?

# grab values from file_parser or default
def getInitVar(variable, parser, default, varList=False):
    if parser:
        if varList:
            return [o.strip() for o in parser.get('constants',variable).split(",")] if parser.has_option('constants',variable) else default
        else:
            return parser.get('constants',variable) if parser.has_option('constants',variable) else default
    else:
        return default

# first parser for to/from file parameters
parser = argparse.ArgumentParser(
    description='', prefix_chars='-+', formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# Meta variables
group = parser.add_argument_group('Meta Variables', '')
group.add_argument('--to_file', dest='toinitfile', nargs='?', const=DEF_INIT_FILE, default=None,
                  help='write flag values to text file')
group.add_argument('--from_file', type=str, dest='frominitfile', default='',
                   help='reads flag values from file')

args, remaining_argv = parser.parse_known_args()

# create second parser for reading from files
if args.frominitfile:
    conf_parser = SafeConfigParser()
    conf_parser.read(args.frominitfile)
else:
    conf_parser = None

group = parser.add_argument_group('Some Group', 'blah blah')

group.add_argument('-someFlag', dest='somevar', default=getInitVar('corpdb', conf_parser, DEF_VAR),
                    help='Some help.')
....

使用 -h 标志时的输出:

optional arguments:
-h, --help            show this help message and exit

Meta Variables:
--to_file [TOINITFILE]
                    write flag values to text file (default: None)
--from_file FROMINITFILE
                    reads flag values from file (default: )

编辑: 在我的代码中添加了一些细节(如评论中所建议的),说明我为什么要调用 parse_known_args

  1. 我创建解析器并添加两个参数:from_fileto_file
  2. 解析参数。如果存在 from_file,我将创建第二个解析器并读取输入变量。
  3. 继续向解析器添加参数。默认值是我将第二个解析器和默认值传递给的函数。

编辑: 终于想出了如何做到这一点,在下面发布了答案。

看起来您正在解析输入,并根据某些值添加更多参数,您将再次解析这些参数。对吗?

第一个解析正在处理 -h。请记住它在打印其参数后退出。所以它没有到达 some group 添加,更不用说显示后面的帮助了。

解决此问题的一种方法是使用 add_help=False 定义第一个解析器,因此第一个 parse_know_args 不处理 -h.

现在将您自己的 -h 参数添加到解析器(尝试 action='help');或重新创建解析器。无论哪种方式,第二个解析步骤都会作用于 -h.

进行了以下更改:

  1. 使用参数 add_help=False
  2. 创建了一个 init_parser
  3. 已将 init_parser 作为 parent 传递给 parserparents=[init_parser]
  4. 将描述参数从 init_parser 移动到 parser

这是最终代码:

init_parser = argparse.ArgumentParser(prefix_chars='-+', formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)

# Meta variables
group = init_parser.add_argument_group('Meta Variables', '')
group.add_argument('--to_file', dest='toinitfile', nargs='?', const=DEF_INIT_FILE, default=None, help='write flag values to text file')
group.add_argument('--from_file', type=str, dest='frominitfile', default='', help='reads flag values from file')

args, remaining_argv = init_parser.parse_known_args()

if args.frominitfile:
    conf_parser = SafeConfigParser()
    conf_parser.read(args.frominitfile)
else:
    conf_parser = None

# Inherit options from init_parser
parser = argparse.ArgumentParser(description='Extract and Manage Language Feature Data.', 
    parents=[init_parser])

group = parser.add_argument_group('Some Group', 'blah blah')

group.add_argument('-someFlag', dest='somevar', default=getInitVar('corpdb', conf_parser, DEF_VAR),
                    help='Some help.')
....

输出:

optional arguments:
    -h, --help            show this help message and exit

Meta Variables:
--to_file [TOINITFILE]
                write flag values to text file (default: None)
--from_file FROMINITFILE
                reads flag values from file (default: )
Some group:
    blah blah blah
--someFlag
...