python的命令行解析argparse

# -*- coding: utf-8 -*-

import argparse

args = "-f hello.txt -n 1 2 3 -x 100 -y b -z a -q hello @args.txt i_am_bar -h".split()
# 使用@args.txt要求fromfile_prefix_chars="@"
# args.txt文件中应该一行一个参数,想改变行为参考convert_arg_line_to_args()
>>> with open('args.txt', 'w') as fp:
... fp.write('-f\nbar')
>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
>>> parser.add_argument('-f')
>>> parser.parse_args(['-f', 'foo', '@args.txt'])
Namespace(f='bar')

# ArgumentParser参数的简单说明
## description - 命令行帮助的开始文字,大部分情况下,我们只会用到这个参数
# epilog - 命令行帮助的结尾文字
# prog - (default: sys.argv[0])程序的名字,一般不需要修改,另外,如果你需要在help中使用到程序的名字,可以使用%(prog)s
# prefix_chars - 命令的前缀,默认是-,例如-f/--file。有些程序可能希望支持/f这样的选项,可以使用prefix_chars="/"
# fromfile_prefix_chars - (default: None)如果你希望命令行参数可以从文件中读取,就可能用到。例如,如果fromfile_prefix_chars='@',命令行参数中有一个为"@args.txt",args.txt的内容会作为命令行参数
# add_help - 是否增加-h/-help选项 (default: True),一般help信息都是必须的,所以不用设置啦。
##   parents - 类型是list,如果这个parser的一些选项跟其他某些parser的选项一样,可以用parents来实现继承,例如parents=[parent_parser]
>>> parent_parser = argparse.ArgumentParser(add_help=False) >>>parent_parser.add_argument('--parent', type=int) >>> foo_parser =argparse.ArgumentParser(parents=[parent_parser]) >>> foo_parser.add_argument('foo')>>> foo_parser.parse_args(['--parent', '2', 'XXX']) Namespace(foo='XXX', parent=2) >>>bar_parser = argparse.ArgumentParser(parents=[parent_parser]) >>>bar_parser.add_argument('--bar') >>> bar_parser.parse_args(['--bar', 'YYY'])Namespace(bar='YYY', parent=None)
## formatter_class - 自定义帮助信息的格式(description和epilog)。默认情况下会将长的帮助信息进行<自动换行和消除多个连续空白>。
#三个允许的值:
# class argparse.RawDescriptionHelpFormat ter 直接输出description和epilog的原始形式(不进行自动换行和消除空白的操作)
# class argparse.RawTextHelpFormatter 直接输出description和epilog以及add_argument中的help字符串的原始形式(不进行自动换行和消除空白的操作)
## class argparse.ArgumentDefaultsHelpForm atter 在每个选项的帮助信息后面输出他们对应的缺省值,如果有设置的话。这个最常用吧!
# argument_default - (default: None)设置一个全局的选项的缺省值,一般每个选项单独设置,所以这个参数用得少,不细说
# usage - (default: generated)如果你需要修改usage的信息(usage: PROG [-h] [--foo [FOO]] bar [bar ...]),那么可以修改这个,一般不要修改。
# conflict_handler - 不建议使用。这个在极端情况下才会用到,主要是定义两个add_argument中添加的选项的名字发生冲突时怎么处理,默认处理是抛出异常。
#注释一行有##表示这几个参数比较常用
parser = argparse.ArgumentParser(description="This is a description of %(prog)s", epilog="This is a epilog of %(prog)s", prefix_chars="-+", fromfile_prefix_chars="@", formatter_class=argparse.ArgumentDefaultsHelpForm atter)

# ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
# add_argument的参数是比较复杂的。。。

# name or flags - 指定参数的形式,想写几个写几个,不过我们一般就写两个,一个短参数,一个长参数,看下面的例子"-f", "--file"
# 可选的选项,位置不固定,想怎么写就怎么写,默认是可选的
parser.add_argument("-f", "--file", help="test test test")
# 位置固定的选项,例如"prog i_am_bar",这样子的话,i_am_bar就是bar选项的值啦,默认是必须有的
parser.add_argument("bar", help="test test test")

# nargs - 指定这个参数后面的value有多少个,例如,我们希望使用-n 1 2 3 4,来设置n的值为[1, 2, 3, 4]
parser.add_argument("-n", "--num", nargs="+", type=int)
# 这里nargs="+"表示,如果你指定了-n选项,那么-n后面至少要跟一个参数,+表示至少一个,?表示一个或0个,*0个或多个,

# default - 如果命令行没有出现这个选项,那么使用default指定的默认值
parser.add_argument("+g", "++gold", help="test test test", default="test_gold")#需要prefix_chars包含"+"

# type - 如果希望传进来的参数是指定的类型(例如 float, int or file等可以从字符串转化过来的类型),可以使用
parser.add_argument("-x", type=int)

# choices - 设置参数值的范围,如果choices中的类型不是字符串,记得指定type哦
parser.add_argument("-y", choices=['a', 'b', 'd'])

# required - 通常-f这样的选项是可选的,但是如果required=True那么就是必须的了
parser.add_argument("-z", choices=['a', 'b', 'd'], required=True)

# metavar - 参数的名字,在显示 帮助信息时才用到.
parser.add_argument("-o", metavar="OOOOOO")

# help - 设置这个选项的帮助信息
# dest - 设置这个选项的值就是解析出来后放到哪个属性中
parser.add_argument("-q", dest="world")

args = parser.parse_args(args) # 如果你没有args参数,那么就使用sys.argv,也就是命令行参数啦。有这个参数,就方便我们调试啊
# args.world就是-q的值啦

# action - The basic type of action to be taken when this argument is encountered at the command line.

ArgumentParser objects associate command-line arguments with actions. These actions can do just about anything with the command-line arguments associated with them, though most actions simply add an attribute to the object returned by parse_args(). The action keyword argument specifies how the command-line arguments should be handled. The supported actions are:

  • 'store' - This just stores the argument’s value. This is the default action. For example:

    >>> parser = argparse.ArgumentParser() 
  • >>> parser.add_argument('--foo') 
  • >>> parser.parse_args('--foo 1'.split()) Namespace(foo='1')
  • 'store_const' - This stores the value specified by the const keyword argument. (Note that the const keyword argument defaults to the rather unhelpful None.) The 'store_const'action is most commonly used with optional arguments that specify some sort of flag. For example:

    >>> parser = argparse.ArgumentParser() 
  • >>> parser.add_argument('--foo', action='store_const', const=42) 
  • >>> parser.parse_args('--foo'.split()) Namespace(foo=42)
  • 'store_true' and 'store_false' - These are special cases of 'store_const' using for storing the values True and False respectively. In addition, they create default values ofFalse and True respectively. For example:

    >>> parser = argparse.ArgumentParser() 
  • >>> parser.add_argument('--foo', action='store_true') 
  • >>> parser.add_argument('--bar', action='store_false') 
  • >>> parser.add_argument('--baz', action='store_false') 
  • >>> parser.parse_args('--foo --bar'.split()) Namespace(bar=False, baz=True, foo=True)
  • 'append' - This stores a list, and appends each argument value to the list. This is useful to allow an option to be specified multiple times. Example usage:

    >>> parser = argparse.ArgumentParser() 
  • >>> parser.add_argument('--foo', action='append') 
  • >>> parser.parse_args('--foo 1 --foo 2'.split()) Namespace(foo=['1', '2'])
  • 'append_const' - This stores a list, and appends the value specified by the constkeyword argument to the list. (Note that the const keyword argument defaults to None.) The'append_const' action is typically useful when multiple arguments need to store constants to the same list. For example:

    >>> parser = argparse.ArgumentParser() 
  • >>> parser.add_argument('--str', dest='types', action='append_const',const=str) 
  • >>> parser.add_argument('--int', dest='types', action='append_const',const=int) 
  • >>> parser.parse_args('--str --int'.split()) Namespace(types=[, ])
  • 'count' - This counts the number of times a keyword argument occurs. For example, this is useful for increasing verbosity levels:

    >>> parser = argparse.ArgumentParser() 
  • >>> parser.add_argument('--verbose', '-v', action='count') 
  • >>> parser.parse_args('-vvv'.split()) Namespace(verbose=3)
  • 'help' - This prints a complete help message for all the options in the current parser and then exits. By default a help action is automatically added to the parser. SeeArgumentParser for details of how the output is created.

  • 'version' - This expects a version= keyword argument in the add_argument() call, and prints version information and exits when invoked:

    >>> import argparse 
  • >>> parser = argparse.ArgumentParser(prog='PROG') 
  • >>> parser.add_argument('--version', action='version', version='%(prog)s 2.0') 
  • >>> parser.parse_args(['--version']) PROG 2.0

You can also specify an arbitrary action by passing an object that implements the Action API. The easiest way to do this is to extend argparse.Action, supplying an appropriate __call__method. The __call__ method should accept four parameters:

  • parser - The ArgumentParser object which contains this action.
  • namespace - The Namespace object that will be returned by parse_args(). Most actions add an attribute to this object.
  • values - The associated command-line arguments, with any type conversions applied. (Type conversions are specified with the type keyword argument to add_argument().)
  • option_string - The option string that was used to invoke this action. Theoption_string argument is optional, and will be absent if the action is associated with a positional argument.

An example of a custom action:

>>> class FooAction(argparse.Action): 
... def __call__(self, parser, namespace, values, option_string=None): 
... print '%r %r %r' % (namespace, values, option_string) 
... setattr(namespace, self.dest, values) 
... 
>>> parser = argparse.ArgumentParser() 
>>> parser.add_argument('--foo', action=FooAction) 
>>> parser.add_argument('bar', action=FooAction) 
>>> args = parser.parse_args('1 --foo 2'.split()) 
Namespace(bar=None, foo=None) '1' None Namespace(bar='1', foo=None) '2' '--foo' 
>>> args Namespace(bar='1', foo='2')

# const - A constant value required by some action and nargs selections.
# 这两个自己看帮助文档啦,比较复杂
# http://docs.python.org/library/argparse.html

print args


这个复杂的代码,最后输出的帮助信息是(只要命令行有-h选项就会输出帮助信息并退出哦)

 

usage: argparse_sample.py [-h] [-f FILE] [-n NUM [NUM ...]] [+g GOLD] [-x X]
                                        [-y {a,b,d}] -z {a,b,d} [-o OOOOOO] [-q WORLD]
                                        bar
 
This is a description of argparse_sample.py
 
positional arguments:
    bar                            test test test
 
optional arguments:
    -h, --help                  show this help message and exit
    -f FILE, --file FILE   test test test (default: None)
    -n NUM [NUM ...], --num NUM [NUM ...]
    +g GOLD, ++gold GOLD   test test test (default: test_gold)
    -x X
    -y {a,b,d}
    -z {a,b,d}
    -o OOOOOO
    -q WORLD
 
This is a epilog of argparse_sample.py
 

原地址:http://blog.iamzsx.me/show.html?id=100001
稍作修改。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值