zoukankan      html  css  js  c++  java
  • python-argparse

    2019-12-18 14:25:53

    argparse是python的一个命令行解析包,用于编写可读性非常好的程序。

    import argparse
    
    
    parser = argparse.ArgumentParser()
    
    # 定位参数,可以指定参数类型,以及帮助信息
    # 所谓定位参数,可以理解为必选参数,当使用python进行脚本运行的时候,必须显示的给出各个定位参数的具体数值
    # 定位参数没有缺省数值,必须在程序运行的时候按照声明的顺序依次给出所有的数值
    # 定位参数由于不够灵活,所以一般来说用的还是比较少的
    parser.add_argument("arg1", type = int, help = "This is the first arg1")
    parser.add_argument("arg2", type = int, help = "This is the first arg2")
    
    
    # 可选参数
    # 可选参数和定位参数在定义上的最大的不同就是 -- 
    # 可选参数可以给出缺省值,而且一般来说也是需要加上缺省数值的;对于没有缺省值的可选参数并且没有在执行的时候给出具体的数值,一般会默认给None
    # 可选参数的数值可以在程序运行的时候显示的给定,例如 python args.py --opt_arg1 5
    # 如果给了 action = "store_true" 那么默认为false,如果在执行的之后显示的给出了这个参数,那么其值变为true
    parser.add_argument("--opt_arg1", type = int, help = "This is the first opt arg1") 
    parser.add_argument("--opt_arg2", type = int, default = 1, help = "This is the second opt arg2")
    parser.add_argument("--opt_arg3", action = "store_true", help = "This is the third opt arg3")
    
    args = parser.parse_args()
    
    print("定位参数")
    print("{}, {}".format(args.arg1, args.arg2))
    
    print("可选参数")
    print("{}, {}, {}".format(args.opt_arg1, args.opt_arg2, args.opt_arg3))
    

      

  • 相关阅读:
    WCF+Silverlight 制作一个简单RSS的阅读器(一)
    将Ironpython嵌入到你的程序中
    今天修改了网页
    开发小议
    Silverlight 可以支持windows2000了
    昨天晚上开始寻找圣诞的素材了
    需要每天的坚持!
    今天开始学习ADO.NET中的Connection对象(一)SqlConnection对象连接SQL Server
    一个基于Ajax简单的数据验证
    《微软:DirectShow开发指南》第4章 Capturing Audio with DirectShow
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/12063889.html
Copyright © 2011-2022 走看看