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))
    

      

  • 相关阅读:
    2dsphere索引
    geoNear查询 near查询的升级版
    geoWithin查询 多边形查询
    [TJOI2013]最长上升子序列
    「bzoj3956: Count」
    「bzoj3687: 简单题」
    「SDOI2008沙拉公主的困惑」
    郑州Day6
    「Luogu-U18201」分析矿洞
    【[COCI2011-2012#5] POPLOCAVANJE】
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/12063889.html
Copyright © 2011-2022 走看看