zoukankan      html  css  js  c++  java
  • python argparse模块--转载

    add_argument:读入命令行参数,该调用有多个参数

    ArgumentParser.add_argument(name or flags…[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

    name or flags:是必须的参数,该参数接受选项参数或者是位置参数(一串文件名)

    [python] view plain copy
     
    1. >>> parser.add_argument('-f', '--foo')    #选项参数  
    2. >>> parser.add_argument('bar')        #位置参数  


    nargs: 当选项后接受多个或者0个参数时需要这个来指定
    比如-u选项接受2个参数

    [python] view plain copy
     
    1. >>> parser.add_argument('-u',nargs=2)  
    2. >>> parser.parse_args('-u a b'.split())  
    3. Namespace(u=['a', 'b'])  


    当选项接受1个或者不需要参数时指定nargs=’?',当没有参数时,会从default中取值。对于选项参数有一个额外的情况,就是出现选项而后面没有跟具体参数,那么会从const中取值

    [python] view plain copy
     
    1. >>> parser.add_argument('-u',nargs='?')  
    2. >>> parser.parse_args(''.split())  
    3. Namespace(u=None)  
    4. >>> parser.parse_args('-u a'.split())  
    5. Namespace(u='a')  
    6.   
    7. >>> parser.add_argument('-u',nargs='?',default='d')  
    8. >>> parser.add_argument('A',nargs='?',default='e')  
    9. >>> parser.parse_args(''.split())  
    10. Namespace(A='e', u='d')  
    11. >>> parser.parse_args('-u'.split())  
    12. Namespace(A='e', u=None)  
    13.   
    14. >>> parser.add_argument('-u',nargs='?',default='d',const='s')  
    15. >>> parser.add_argument('A',nargs='?',default='T',const='P')  
    16. >>> parser.parse_args(''.split())  
    17. Namespace(A='T', u='d')  
    18. >>> parser.parse_args('-u'.split())  
    19. Namespace(A='T', u='s')  
    20. >>> parser.parse_args('A'.split())  
    21. Namespace(A='A', u='d')  



    而对于后面需要跟多个参数的情况(–foo a1 a2 a3…),则需要设置nargs=’*’

    [python] view plain copy
     
    1. >>> parser.add_argument('-u',nargs='*')  
    2. >>> parser.parse_args('-u a b c d e'.split())  
    3. Namespace(u=['a', 'b', 'c', 'd', 'e'])  



    nargs=’+'也和nargs=’*'一样,但是有一个区别当’+'时少于1个参数(没有参数)位置参数会报错误

    [python] view plain copy
     
    1. >>> parser.add_argument('u',nargs='+')  
    2. >>> parser.parse_args(''.split())  
    3. usage: [-h] u [u ...]  
    4. : error: too few arguments  



    而‘*’会使用默认值

    [python] view plain copy
     
    1. >>> parser.add_argument('u',nargs='*',default='e')  
    2. >>> parser.parse_args(''.split())  
    3. Namespace(u='e')  



    default: 当参数需要默认值时,由这个参数指定,默认为None,当default=argparse.SUPPRESS时,不使用任何值

    [python] view plain copy
     
    1. >>> parser.add_argument('u',nargs='*',default=argparse.SUPPRESS)  
    2. >>> parser.parse_args(''.split())  
    3. Namespace()  


    type: 使用这个参数,转换输入参数的具体类型,这个参数可以关联到某个自定义的处理函数,这种函数通常用来检查值的范围,以及合法性

    [python] view plain copy
     
    1. >>> parser.parse_args('-u',type=int)  
    2. >>> parser.add_argument('f',type=file)  
    3. >>> parser.parse_args('-u 2 aa'.split())  
    4. Namespace(f=<open file 'aa', mode 'r' at 0x8b4ee38>, u=2)  



    choices: 这个参数用来检查输入参数的范围

    [python] view plain copy
     
    1. >>> parser.add_argument('-u',type=int,choices=[1,3,5])  
    2. >>> parser.parse_args('-u 3'.split())  
    3. Namespace(u=3)  
    4. >>> parser.parse_args('-u 4'.split())  
    5. usage: [-h] [-u {1,3,5}]  
    6. : error: argument -u: invalid choice: 4 (choose from 1, 3, 5)  



    required: 当某个选项指定需要在命令中出现的时候用这个参数

    [python] view plain copy
     
    1. >>> parser.add_argument('-u',required=True)  
    2. >>> parser.parse_args(''.split())  
    3. usage: [-h] -u U  
    4. : error: argument -u is required  


    help: 使用这个参数描述选项作用

    [python] view plain copy
     
    1. >>> parser.add_argument('-u',required=True,default='wowo',help='%(prog)s for test sth(default: %(default)s)')  
    2. >>> parser.print_help()                                                        usage: [-h] -u U  
    3.   
    4. optional arguments:  
    5.   -h, --help  show this help message and exit  
    6.   -u U        for test sth(default: wowo)  



    dest: 这个参数相当于把位置或者选项关联到一个特定的名字

    [python] view plain copy
     
    1. >>> parser.add_argument('--str',nargs='*')  
    2. >>> parser.parse_args('--str a b c'.split())  
    3. Namespace(str=['a', 'b', 'c'])  
    4.   
    5. >>> parser.add_argument('--str',nargs='*',dest='myname')  
    6. >>> parser.parse_args('--str a b c'.split())  
    7. Namespace(myname=['a', 'b', 'c'])  


    metavar: 这个参数用于help 信息输出中

    [python] view plain copy
     
      1. >>> parser.add_argument('--str',nargs='*',metavar='AAA')  
      2. >>> parser.print_help()  
      3. usage: [-h] [--str [AAA [AAA ...]]]  
      4.   
      5. optional arguments:  
      6.   -h, --help            show this help message and exit  
      7.   --str [AAA [AAA ...]]  
      8.   
      9. >>> parser.add_argument('str',nargs='*',metavar='AAA')  
      10. >>> parser.print_help()  
      11. usage: [-h] [AAA [AAA ...]]  
      12.   
      13. positional arguments:  
      14.   AAA  
      15.   
      16. optional arguments:  
  • 相关阅读:
    hdu 1028 Ignatius and the Princess III (n的划分)
    CodeForces
    poj 3254 Corn Fields (状压DP入门)
    HYSBZ 1040 骑士 (基环外向树DP)
    PAT 1071 Speech Patterns (25)
    PAT 1077 Kuchiguse (20)
    PAT 1043 Is It a Binary Search Tree (25)
    PAT 1053 Path of Equal Weight (30)
    c++ 常用标准库
    常见数学问题
  • 原文地址:https://www.cnblogs.com/nkwy2012/p/6272567.html
Copyright © 2011-2022 走看看