zoukankan      html  css  js  c++  java
  • 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/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    HDOJ-3635-Dragon Balls 解题报告
    HDOJ-1217-Arbitrage 解题报告
    C语言控制台窗口图形界面编程(总结)
    PC(win10)上搭建 kubernetes + docker 集群环境
    Hessian 2.0 序列化协议
    对响应式布局的理解
    常用链接集合
    dubbox升级dubbo的过渡方案:通过扩展dubbo的Protocol实现dubbo与dubbox的相互调用
    Wordpress部署 —— 基于Ubuntu、Mysql和Nginx
    关于27种常见设计模式的总结
  • 原文地址:https://www.cnblogs.com/lightsong/p/15120913.html
Copyright © 2011-2022 走看看