Python 当不使用dest时,argparse如何确定参数名称?

Python 当不使用dest时,argparse如何确定参数名称?,python,argparse,Python,Argparse,我对以下示例中使用的batch\u size参数有疑问 https://github.com/pytorch/examples/blob/master/imagenet/main.py#L150 parser.add_argument('-b', '--batch-size', default=256, type=int, metavar='N', # dest='batch_size' why this is

我对以下示例中使用的
batch\u size
参数有疑问

https://github.com/pytorch/examples/blob/master/imagenet/main.py#L150


parser.add_argument('-b', '--batch-size', default=256, type=int,
                    metavar='N',
                    # dest='batch_size' why this is not needed? 
                    help='mini-batch size (default: 256), this is the total '
                         'batch size of all GPUs on the current node when '
                         'using Data Parallel or Distributed Data Parallel')

# missing code

args.batch_size = int(args.batch_size / ngpus_per_node)
我的问题是,如果没有
dest
参数,如何从命令行解析和保存
batch\u size

来自:

ArgumentParser
通过获取第一个长选项字符串并去除初始的
--
字符串,生成
dest
的值 ... 任何内部
-
字符都将转换为
.
字符,以确保字符串是有效的属性名称


在这种情况下,
long选项是
--batch size
,因此默认情况下,
dest
设置为
batch\u size

最好在执行
args=parser.parse\u args()
后不久打印(args)
。这将使您清楚地了解解析器所做的工作,包括为每个参数选择的
dest