有没有办法按照定义的顺序获取 argparse 的参数? [英] Is there a way to get the argument of argparse in the order in which they were defined?

查看:23
本文介绍了有没有办法按照定义的顺序获取 argparse 的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印程序的所有选项,并将它们分组以便于阅读.但是当通过 vars(args) 访问参数时,顺序是随机的.

I'd like to print all options of the program and they are grouped for readability. However when accessing the arguments via vars(args), the order is random.

推荐答案

argparse 解析 sys.argv[1:] (sys.argv[0] 用作 usage 中的 prog 值).

argparse parses the list of arguments in sys.argv[1:] (sys.argv[0] is used as the prog value in usage).

args=parser.parse_args() 返回一个 argparse.Namespace 对象.vars(args) 返回基于此对象的字典 (args.__dict__).字典的键是无序的.print(args) 也使用这个字典顺序.

args=parser.parse_args() returns a argparse.Namespace object. vars(args) returns a dictionary based on this object (args.__dict__). Keys of a dictionary are unordered. print(args) also uses this dictionary order.

解析器为自己的簿记目的保留所见操作的记录.但它不暴露给用户,并且是一个无序的set.我可以想象定义一个自定义的 Action 子类来记录其实例的使用顺序.

The parser keeps a record of seen-actions for its own bookkeeping purposes. But it is not exposed to the user, and is an unordered set. I can imagine defining an custom Action subclass that recorded the order in which its instances were used.

可以按照创建解析器时定义的顺序检索参数.那是因为 parser 有一个所有 Actions_actions 列表.它不是公共 API 的一部分,而是一个基本属性,不可能全部消失.

It is possible to retrieve arguments in the order in which they were defined when creating the parser. That's because the parser has a _actions list of all the Actions. It's not part of the public API, but a basic attribute and unlikely to every disappear.

举例说明:

In [622]: parser=argparse.ArgumentParser()
In [623]: parser.add_argument('foo')
In [624]: parser.add_argument('--bar')
In [625]: parser.add_argument('--baz')

In [626]: parser.print_help()
usage: ipython3 [-h] [--bar BAR] [--baz BAZ] foo

positional arguments:
  foo

optional arguments:
  -h, --help  show this help message and exit
  --bar BAR
  --baz BAZ

用法和帮助列表按照定义的顺序显示参数,除了 positionalsoptionals 是分开的.

The usage and help listings show the arguments in the order that they are defined, except that positionals and optionals are separated.

In [627]: args=parser.parse_args(['--bar','one','foobar'])
In [628]: args
Out[628]: Namespace(bar='one', baz=None, foo='foobar')
In [629]: vars(args)
Out[629]: {'bar': 'one', 'baz': None, 'foo': 'foobar'}

In [631]: [(action.dest, getattr(args,action.dest, '***')) for action in parser._actions]
Out[631]: [('help', '***'), ('foo', 'foobar'), ('bar', 'one'), ('baz', None)]

在这里,我遍历 _actions 列表,获取每个 Actiondest,并从 args<获取该值/code> 命名空间.我也可以从 vars(args) 字典中获取它.

Here I iterate on the _actions list, get the dest for each Action, and fetch that value from the args namespace. I could have fetched it from the vars(args) dictionary just as well.

我不得不给 getattr 一个默认的 ***,因为 help 动作没有出现在命名空间中.我本可以从显示中过滤掉这种操作.

I had to give getattr a default ***, because the help action does not appear in the namespace. I could have filtered that sort of action out of the display.

这篇关于有没有办法按照定义的顺序获取 argparse 的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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