使用 argparse 处理不确定的配对参数 [英] Handling indefinite paired arguments with argparse

查看:21
本文介绍了使用 argparse 处理不确定的配对参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中,我需要定义一个类似

In my project, I need to define a syntax like

mcraw recipe add COUNT ID COUNT_1 ID_1 [COUNT_2 ID_2 ..]

argparse 似乎是最好的一般工作的工具.

and argparse seems to be the best tool for the general job.

如何指示 Python 及其 argparse 构建这样的字典?

How can I instruct Python and its argparse to construct a dictionary like this?

{
  ID_1: COUNT_1,
  ID_2: COUNT_2,
  ...
}

推荐答案

成对阅读你的论点:

argdict = {args[i + 1]: args[i] for i in xrange(0, len(args), 2)}

argparse 否则对此类输入没有特殊处理.

argparse has otherwise no special handling for this kind of input.

这篇关于使用 argparse 处理不确定的配对参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆