Python 使用许多可选子参数设计argparse

Python 使用许多可选子参数设计argparse,python,python-3.x,argparse,Python,Python 3.x,Argparse,我目前正在为我的库编写一个脚本,最后我被困在如何设计argparse上,它将有很多选项和子参数 目前我正在设计搜索功能,它有以下选项,有些是必需的,有些不是: 搜索会话id-必需 用户会话id-必需 发现\u字段-可选 开始时间-可选 结束时间-可选 摘要字段-可选 字段汇总-可选 本地搜索-可选 我的问题是: 如果所有选项都需要一起工作,但如果只定义了其中一个,那么如何使argparse和if语句工作 如果我需要检查每一个组合,我会得到如下结果: #!/usr/bin/env python

我目前正在为我的库编写一个脚本,最后我被困在如何设计argparse上,它将有很多选项和子参数

目前我正在设计搜索功能,它有以下选项,有些是必需的,有些不是:

  • 搜索会话id-必需
  • 用户会话id-必需
  • 发现\u字段-可选
  • 开始时间-可选
  • 结束时间-可选
  • 摘要字段-可选
  • 字段汇总-可选
  • 本地搜索-可选
我的问题是:

如果所有选项都需要一起工作,但如果只定义了其中一个,那么如何使argparse和if语句工作

如果我需要检查每一个组合,我会得到如下结果:

#!/usr/bin/env python3

"""Script to generate searches on the ArcSight Logger"""

import arcsightrest
import argparse

parser = argparse.ArgumentParser(description='Script used to send search '
                                             'queries to ArcSight Logger API')
parser.add_argument('-t', '--target',
                    help='IP Address of the Loggger', required=True)
parser.add_argument('-u', '--username',
                    help='Username to access the logger', required=True)
parser.add_argument('-p', '--password',
                    help='Password to access the logger', required=True)
parser.add_argument('-ussl', '--unsecuressl', action='store_true',
                    help='Disable ssl warnings', )
parser.add_argument('-w', '--wait', action='store_true',
                    help='Wait for query to finish', )
parser.add_argument('-q', '--query',
                    help='Query to be used in the search')
parser.add_argument('-st', '--starttime',
                    help='From which time the query should look')
parser.add_argument('-et', '--endtime',
                    help='To which time the query should look')
parser.add_argument('-e', '--event',
                    help='Events based input search id')
parser.add_argument('-s', '--status',
                    help='Status of running search')
args = (parser.parse_args())

"""
Sets the target Logger Server
"""
arcsightrest.ArcsightLogger.TARGET = args.target

"""
Gets login token from the Logger API
"""
arc = arcsightrest.ArcsightLogger(args.username, args.password,
                                  args.unsecuressl)
"""
Checks if query is used, and starts a search
"""
if args.query:
    if args.starttime:
        search_id, response = arc.search(args.query, start_time=args.starttime,
                                         end_time=args.endtime)
    search_id, response = arc.search(args.query)

    if args.starttime and args.discover_fields:
        search_id, response = arc.search(args.query, start_time=args.starttime,
                                         end_time=args.endtime,
                                         discover_fields=args.discover_fields)
    print('The search id is {}'.format(search_id))
    if response:
        print('The search has successfully started')

正如您所看到的,我可以无休止地继续,生成包含每个可选参数组合的if语句。一定有更简单的方法来设计这个?如果我只是将其解析为KWARG,它们将不会以正确的格式发送,或者我会要求使用脚本的人编写类似于
end\u time=someone
,而不仅仅是
--endtime
。现在,这似乎是一个很小的代价,但如果我需要将每个函数及其所有参数添加到脚本中,那么这将变得更长、更乏味。

您可以收集传递给
arc的所有可选关键字参数。搜索
dict
,然后在调用函数时将其解压缩:

import argparse

parser = argparse.ArgumentParser(description='Script used to send search '
                                             'queries to ArcSight Logger API')
parser.add_argument('-t', '--target',
                    help='IP Address of the Loggger', required=True)
parser.add_argument('-u', '--username',
                    help='Username to access the logger', required=True)
parser.add_argument('-p', '--password',
                    help='Password to access the logger', required=True)
parser.add_argument('-q', '--query',
                    help='Query to be used in the search')
parser.add_argument('-st', '--starttime',
                    help='From which time the query should look')
parser.add_argument('-et', '--endtime',
                    help='To which time the query should look')
args = (parser.parse_args())

# Mock search
def search(query, start_time=None, end_time=None, discover_fields=None):
    return 'Id', ', '.join(str(x) for x in [start_time, end_time, discover_fields])

"""
Checks if query is used, and starts a search
"""
if args.query:
    # {name used in argparse: search parameter name}
    query_args = {
        'starttime': 'start_time',
        'endtime': 'end_time',
        'discover_fields': 'discover_fields'
    }
    d = vars(args)
    real_args = {v: d[k] for k, v in query_args.items() if k in d}
    search_id, response = search(args.query, **real_args)

    print('The search id is {}'.format(search_id))
    print('Response is {}'.format(response))
输出:

>python test.py -t foo -u user -p pass -q
query -st start -et end
The search id is Id
Response is start, end, None

由于解析器使用的某些参数名称与传递给
search
的参数名称不同,因此需要重新映射这些名称。用于从
parse_args()
返回的对象创建
dict
。然后dictionary comprehension对映射的参数名进行迭代,选择给用户的参数名,并创建一个新的字典,其中的键名
arc.search
可以理解。最后,
**real_args
在函数调用中解包字典命名的参数。

您可以收集传递给
arc的所有可选关键字参数。搜索
dict
,然后在调用函数时解包:

import argparse

parser = argparse.ArgumentParser(description='Script used to send search '
                                             'queries to ArcSight Logger API')
parser.add_argument('-t', '--target',
                    help='IP Address of the Loggger', required=True)
parser.add_argument('-u', '--username',
                    help='Username to access the logger', required=True)
parser.add_argument('-p', '--password',
                    help='Password to access the logger', required=True)
parser.add_argument('-q', '--query',
                    help='Query to be used in the search')
parser.add_argument('-st', '--starttime',
                    help='From which time the query should look')
parser.add_argument('-et', '--endtime',
                    help='To which time the query should look')
args = (parser.parse_args())

# Mock search
def search(query, start_time=None, end_time=None, discover_fields=None):
    return 'Id', ', '.join(str(x) for x in [start_time, end_time, discover_fields])

"""
Checks if query is used, and starts a search
"""
if args.query:
    # {name used in argparse: search parameter name}
    query_args = {
        'starttime': 'start_time',
        'endtime': 'end_time',
        'discover_fields': 'discover_fields'
    }
    d = vars(args)
    real_args = {v: d[k] for k, v in query_args.items() if k in d}
    search_id, response = search(args.query, **real_args)

    print('The search id is {}'.format(search_id))
    print('Response is {}'.format(response))
输出:

>python test.py -t foo -u user -p pass -q
query -st start -et end
The search id is Id
Response is start, end, None
由于解析器使用的某些参数名称与传递给
search
的参数名称不同,因此需要重新映射这些名称。用于从
parse_args()
返回的对象创建
dict
。然后dictionary comprehension对映射的参数名进行迭代,选择给用户的参数名,并创建一个新的字典,其中的键名
arc.search
可以理解。最后,
**real_args
在函数调用中解压字典命名的参数。

子命令呢?子命令呢?