long option and short option of ArgumentParser in Python

dest

https://docs.python.org/3/library/argparse.html#dest

dest用于指定解析后将参数值,存储到参数表中的参数名称, 简称存储参数名。

dest本身是add_argument参数之一

The name of this attribute is determined by the dest keyword argument of add_argument().

如果参数是位置参数, 则dest就是待解析的参数名。

For positional argument actions, dest is normally supplied as the first argument to add_argument():

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('bar')
>>> parser.parse_args(['XXX'])
Namespace(bar='XXX')

如果是可选参数, dest是由待解析的可选参数项推断而来。

如果有 长可选参数, 以 --开头, 则其后的字符串,将作为存储参数名。

For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string.

如果没有 长参数名, 只有短参数名, 则将第一个短参数名,作为存储参数名。

If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name. The examples below illustrate this behavior:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-f', '--foo-bar', '--foo')
>>> parser.add_argument('-x', '-y')
>>> parser.parse_args('-f 1 -x 2'.split())
Namespace(foo_bar='1', x='2')
>>> parser.parse_args('--foo 1 -y 2'.split())
Namespace(foo_bar='1', x='2')

dest显示出现的手, 则以dest为准。

dest allows a custom attribute name to be provided:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', dest='bar')
>>> parser.parse_args('--foo XXX'.split())
Namespace(bar='XXX')
出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
原文地址:https://www.cnblogs.com/lightsong/p/15120913.html