Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python argparse-使用子parser和父级时未正确解析命令_Python_Command Line Arguments_Argparse - Fatal编程技术网

Python argparse-使用子parser和父级时未正确解析命令

Python argparse-使用子parser和父级时未正确解析命令,python,command-line-arguments,argparse,Python,Command Line Arguments,Argparse,在我的应用程序中,我有这样一个解析器: description = ("Cluster a matrix using bootstrap resampling or " "Bayesian hierarchical clustering.") sub_description = ("Use these commands to cluster data depending on " "the algorithm.") parse

在我的应用程序中,我有这样一个解析器:

description = ("Cluster a matrix using bootstrap resampling or "
               "Bayesian hierarchical clustering.")
sub_description = ("Use these commands to cluster data depending on "
                   "the algorithm.")

parser = argparse.ArgumentParser(description=description, add_help=False)
subparsers = parser.add_subparsers(title="Sub-commands",
                                   description=sub_description)
parser.add_argument("--no-logfile", action="store_true", default=False,
                    help="Don't log to file, use stdout")
parser.add_argument("source", metavar="FILE",
                    help="Source data to cluster")
parser.add_argument("destination", metavar="FILE",
                     help="File name for clustering results")
然后我添加了一系列类似这样的子解析器(使用函数,因为它们很长):

这些调用(以一个为例):

问题是,不知何故,命令行的解析失败了,我肯定这是我的错。运行时的示例:

  my_program.py bootstrap --boot 10 --no-logfile test.txt test.pdf

  my_program.py bootstrap: error: too few arguments
好像解析有点错误。如果我在subparser调用中删除parent=[],这种行为就会消失,但我宁愿避免它,因为它会创建大量重复

编辑:在
add_参数
调用之后移动
子parser
调用可以修复部分问题。但是,现在解析器无法正确解析子命令:

my_program.py bootstrap --boot 100 --no-logfile test.txt test.pdf

my_program.py: error: invalid choice: 'test.txt' (choose from 'bootstrap', 'native')

基本问题是,您混淆了
解析器应该处理的参数和子解析器应该处理的参数。事实上,通过将解析器作为父对象传递给子解析器,您最终将在两个位置定义这些参数

此外,
source
destination
是位置参数,子参数也是位置参数。如果它们都是在基本解析器中定义的,那么它们的顺序很重要

我建议定义一个单独的
parent
解析器

parent = argparse.ArgumentParser(add_help=False)
parent.add_argument("--no-logfile", action="store_true". help="...")
parent.add_argument("source", metavar="FILE", help="...")
parent.add_argument("destination", metavar="FILE", help="...")

parser = argparse.ArgumentParser(description=description)
subparsers = parser.add_subparsers(title="Sub-commands", description=sub_description)
setup_pvclust_parser(subparsers, parent)
setup_native_parser(subparsers, parent)
my_program.py bootstrap --boot 100 --no-logfile test.txt test.pdf

my_program.py: error: invalid choice: 'test.txt' (choose from 'bootstrap', 'native')
parent = argparse.ArgumentParser(add_help=False)
parent.add_argument("--no-logfile", action="store_true". help="...")
parent.add_argument("source", metavar="FILE", help="...")
parent.add_argument("destination", metavar="FILE", help="...")

parser = argparse.ArgumentParser(description=description)
subparsers = parser.add_subparsers(title="Sub-commands", description=sub_description)
setup_pvclust_parser(subparsers, parent)
setup_native_parser(subparsers, parent)