metavar和action在Python的argparse中意味着什么? [英] What does metavar and action mean in argparse in Python?

查看:682
本文介绍了metavar和action在Python的argparse中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 argparse 模块。我对metavar和操作的含义感到困惑

I am reading through argparse module. I got stuck as what to metavar and action means

>>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
...                     help='an integer for the accumulator')
>>> parser.add_argument('--sum', dest='accumulate', action='store_const',
...                     const=sum, default=max,
...                     help='sum the integers (default: find the max)')

我可能已经错过了,但是从我的阅读中我可以找不到元变量
action(action = store_const等)的定义。

I might have missed but from what I read, I could not find definitions for metavar and action (action="store_const", etc). what do they actually mean?

推荐答案

metavar 用于帮助消息中预期参数的位置。请参阅 FOO 是默认的 metavar 此处:

metavar is used in help messages in a place of an expected argument. See FOO is a default metavar here:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> parser.add_argument('bar')
>>> parser.parse_args('X --foo Y'.split())
Namespace(bar='X', foo='Y')
>>> parser.print_help()
usage:  [-h] [--foo FOO] bar
...

操作定义了如何处理命令-行参数:将其存储为常量,追加到列表中,存储布尔值等。有几种内置操作,加上编写自定义操作很容易。

action defines how to handle command-line arguments: store it as a constant, append into a list, store a boolean value etc. There are several built-in actions available, plus it's easy to write a custom one.

这篇关于metavar和action在Python的argparse中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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