argparse.ArgumentParser()用法解析

此模块有中文文档,建议看到此文的读者链接到中文文档查看,本文只是自己学习、个人理解的重点总结

  • argparse模块 官方文档

    argparse是一个Python模块:命令行选项、参数和子命令解析器。

    argparse 模块可以让人轻松编写用户友好的命令行接口。程序定义它需要的参数,然后 argparse 将弄清如何从 sys.argv 解析出那些参数。 argparse 模块还会自动生成帮助和使用手册,并在用户给程序传入无效参数时报出错误信息。

  • 使用流程

    1. 创建解析器

      parser = argparse.ArgumentParser(description='Process some integers.')
      
             
             
      • 1

      使用 argparse 的第一步是创建一个 ArgumentParser 对象。

      ArgumentParser 对象包含将命令行解析成 Python 数据类型所需的全部信息。

    2. 添加参数

      parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')
      
             
             
      • 1

      给一个 ArgumentParser 添加程序参数信息是通过调用 add_argument() 方法完成的。

    3. 解析参数

      >>> parser.parse_args(['--sum', '7', '-1', '42'])
      Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42])
      
             
             
      • 1
      • 2

      ArgumentParser 通过 parse_args() 方法解析参数。

  • ArgumentParser 对象

    class argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True)
    
         
         
    • 1
    • prog - 程序的名称(默认:sys.argv[0]
    • usage - 描述程序用途的字符串(默认值:从添加到解析器的参数生成)
    • description - 在参数帮助文档之前显示的文本(默认值:无)
    • epilog - 在参数帮助文档之后显示的文本(默认值:无)
    • parents - 一个 ArgumentParser 对象的列表,它们的参数也应包含在内
    • formatter_class - 用于自定义帮助文档输出格式的类
    • prefix_chars - 可选参数的前缀字符集合(默认值:’-’)
    • fromfile_prefix_chars - 当需要从文件中读取其他参数时,用于标识文件名的前缀字符集合(默认值:None
    • argument_default - 参数的全局默认值(默认值: None
    • conflict_handler - 解决冲突选项的策略(通常是不必要的)
    • add_help - 为解析器添加一个 -h/--help 选项(默认值: True
    • allow_abbrev - 如果缩写是无歧义的,则允许缩写长选项 (默认值:True
  • add_argument() 方法

    ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
    
         
         
    • 1
    • name or flags - 一个命名或者一个选项字符串的列表,例如 foo-f, --foo
    • action - 当参数在命令行中出现时使用的动作基本类型。
    • nargs - 命令行参数应当消耗的数目。
    • const - 被一些 actionnargs 选择所需求的常数。
    • default - 当参数未在命令行中出现时使用的值。
    • type - 命令行参数应当被转换成的类型。
    • choices - 可用的参数的容器。
    • required - 此命令行选项是否可省略 (仅选项可用)。
    • help - 一个此选项作用的简单描述。
    • metavar - 在使用方法消息中使用的参数值示例。
    • dest - 被添加到 parse_args() 所返回对象上的属性名。
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count">9</span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/The_Time_Runner">
                <img src="https://profile.csdnimg.cn/D/9/1/3_the_time_runner" class="avatar_pic" username="The_Time_Runner">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/2x/1.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/The_Time_Runner" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">Quant_Learner</a></span>
                                        </div>
                <div class="text"><span>发布了739 篇原创文章</span> · <span>获赞 990</span> · <span>访问量 51万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=The_Time_Runner" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值