Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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
如何在Python3中使用argparse解析两组命令行参数中的一组?_Python_Python 3.x_Command Line Interface_Argparse - Fatal编程技术网

如何在Python3中使用argparse解析两组命令行参数中的一组?

如何在Python3中使用argparse解析两组命令行参数中的一组?,python,python-3.x,command-line-interface,argparse,Python,Python 3.x,Command Line Interface,Argparse,我想开发一个CLI,它将两对参数中的一个放在一起: group-resources --limit 5 或 组资源是位置资源,需要限制。其余两人的规则是一样的。 然而,我当前的代码强制要求所有四个都是必需的 我当前的代码: import argparse import analysis import cleanup import etl import initialize_db parser = argparse.ArgumentParser( description='Inven

我想开发一个CLI,它将两对参数中的一个放在一起:

group-resources --limit 5

组资源是位置资源,需要限制。其余两人的规则是一样的。 然而,我当前的代码强制要求所有四个都是必需的

我当前的代码:

import argparse

import analysis
import cleanup
import etl
import initialize_db

parser = argparse.ArgumentParser(
    description='Inventory DB for Cloud Infrastructure')
parser.add_argument(
    'group-resources', help='List top k groups as per their memory, disk, and cpu count.')
parser.add_argument(
    '--limit', help='Number of groups in descending order of their resources.', type=int, required=True)
parser.add_argument(
    'group-overlap', help='Fetch nodes that belong in both the groups')
parser.add_argument(
    '--groups', help='specify the group names separated by a comma.', type=str, required=True
)

args = parser.parse_args()

print(args)
当我运行
python3 cli.py组资源--limit 5
时,出现以下错误:

usage: cli.py [-h] --limit LIMIT --groups GROUPS group-resources group-overlap
cli.py: error: the following arguments are required: group-overlap, --groups
我希望如果我提供了组资源和限制,或者如果我提供了组重叠和组,那么我的代码应该可以工作。不允许进行其他排列。我该怎么做呢?

我们可以使用。下面是我们如何对代码稍加修改

import argparse

parser = argparse.ArgumentParser(
    description='Inventory DB for Cloud Infrastructure')

subparsers = parser.add_subparsers(help='types of arguments')

group_resources_parser = subparsers.add_parser("group-resources", help='List top k groups as per their memory, disk, and cpu count.')
group_overlap_parser = subparsers.add_parser("group-overlap", help='Fetch nodes that belong in both the groups')
group_resources_parser.add_argument(
    '--limit', help='Number of groups in descending order of their resources.', type=int, required=True)
group_overlap_parser.add_argument(
    '--groups', help='specify the group names separated by a comma.', type=str, required=True
)

args = parser.parse_args()

print(args)
示例:

  • python3 cli.py组资源

      usage: cli.py group-resources [-h] --limit LIMIT
      cli.py group-resources: error: the following arguments are required: --limit
    
  • python3 cli.py组重叠

     usage: cli.py group-overlap [-h] --groups GROUPS
     cli.py group-overlap: error: the following arguments are required: --groups
    
      usage: cli.py [-h] {group-resources,group-overlap} ...
      cli.py: error: unrecognized arguments: group-overlap
    
  • python3 cli.py组资源--限制5个组重叠

     usage: cli.py group-overlap [-h] --groups GROUPS
     cli.py group-overlap: error: the following arguments are required: --groups
    
      usage: cli.py [-h] {group-resources,group-overlap} ...
      cli.py: error: unrecognized arguments: group-overlap