用argparse将未知数量的参数分组

编程入门 行业动态 更新时间:2024-10-22 10:50:13
本文介绍了用argparse将未知数量的参数分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为命令行程序设计用户界面,该程序需要能够接受一个或多个选项组.每个组都是相同的,但是需要链接在一起,就像这样:

I am designing the user interface for a command line program that needs to be able to accept one or more groups of options. Each group is the same, but needs to be linked together, like so:

./myprogram.py --group1 name1,name2,pathA,pathB --group2 name3,name4,pathC,pathD

对于用户来说,这似乎很笨拙.我已经考虑过使用INI样式的configparser设置,但是它也很笨拙,因为除此以外,我还有很多其他参数,这些参数通常具有默认值,然后我失去了argparse的所有功能.用于处理需求,检查文件是否存在等的模块.

This seems very clunky for a user to deal with. I have considered using a INI-style configparser setup, but it is also clunky, because I have a lot of other arguments besides this that generally have default values, and then I lose all of the power of the argparse module for handling requirements, checking if files exist, etc.

有人对我如何最好地构造ArgumentParser有什么想法,以便我可以让用户一起为给定的组提供四个必需的输入吗?我没有嫁给以逗号分隔的列表,这只是一个例子.如果他们可以输入某种键值对(例如

Does anyone have any ideas on how I could best structure my ArgumentParser so that I can get a user to provide the four required inputs for a given group, together? I am not married to the comma separated list, that was just an example. It would actually be far better if they could enter some sort of key-value pair, such as

./myprogram.py --group1 firstname:name1 secondname:name2 firstpath:pathA secondpath:pathB

但是我知道argparse不支持dict类型,任何允许它的黑客都将变得不那么用户友好.

But I know that argparse does not support the dict type, and any hack to allow it would be even less user-friendly.

推荐答案

您可以将nargs=4与'append'动作一起使用:

You can use nargs=4 with an 'append' action:

import argparse parser = argparse.ArgumentParser() parser.add_argument('--group', nargs=4, action='append') print parser.parse_args()

它称为:

$ python ~/sandbox/test.py --group 1 2 3 4 --group 1 2 3 4 Namespace(group=[['1', '2', '3', '4'], ['1', '2', '3', '4']])

在这里,您可以根据需要解析键值对.

From here, you can parse the key-value pairs if you'd like.

另一种选择是使用自定义动作进行解析-这是一个简单的接受--group key:value key2:value2 ... --group ...

Another option is to use a custom action to do the parsing -- Here's a simple one which accepts arguments of the form --group key:value key2:value2 ... --group ...

import argparse class DictAction(argparse.Action): def __init__(self, *args, **kwargs): super(DictAction, self).__init__(*args, **kwargs) self.nargs = '*' def __call__(self, parser, namespace, values, option_string=None): # The default value is often set to `None` rather than an empty list. current_arg_vals = getattr(namespace, self.dest, []) or [] setattr(namespace, self.dest, current_arg_vals) arg_vals = getattr(namespace, self.dest) arg_vals.append(dict(v.split(':') for v in values)) parser = argparse.ArgumentParser() parser.add_argument('--group', action=DictAction) print parser.parse_args()

这没有检查(如果key:value格式不正确,用户可能会得到有趣的TypeError),并且如果您希望将其限制为指定的键,则还需要内置该键. ..但这些细节应该很容易添加.您还可能要求使用DictAction.__init__中的self.nargs = 4提供四个值.

This has no checking (so the user could get funny TypeErrors if the key:value are not formatted properly) and if you want to restrict it to specified keys, you'll need to build that in as well... but those details should be easy enough to add. You could also require that they provide 4 values using self.nargs = 4 in DictAction.__init__.

更多推荐

用argparse将未知数量的参数分组

本文发布于:2023-10-27 11:26:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   参数   argparse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!