python命令行解析工具argparse模块【5】
>>> # create the top-level parser
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--foo', action='store_true', help='foo help')
>>> subparsers = parser.add_subparsers(help='sub-command help')
>>>
>>> # create the parser for the "a" command
>>> parser_a = subparsers.add_parser('a', help='a help')
>>> parser_a.add_argument('bar', type=int, help='bar help')
>>>
>>> # create the parser for the "b" command
>>> parser_b = subparsers.add_parser('b', help='b help')
>>> parser_b.add_argument('--baz', choices='XYZ', help='baz help')
>>>
>>> # parse some arg lists
>>> parser.parse_args(['a', ''])
Namespace(bar=12, foo=False)
>>> parser.parse_args(['--foo', 'b', '--baz', 'Z'])
Namespace(baz='Z', foo=True)
>>> parser.parse_args(['--help'])
usage: PROG [-h] [--foo] {a,b} ... positional arguments:
{a,b} sub-command help
a a help
b b help optional arguments:
-h, --help show this help message and exit
--foo foo help >>> parser.parse_args(['a', '--help'])
usage: PROG a [-h] bar positional arguments:
bar bar help optional arguments:
-h, --help show this help message and exit >>> parser.parse_args(['b', '--help'])
usage: PROG b [-h] [--baz {X,Y,Z}] optional arguments:
-h, --help show this help message and exit
--baz {X,Y,Z} baz help
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(title='subcommands',
... description='valid subcommands',
... help='additional help')
>>> subparsers.add_parser('foo')
>>> subparsers.add_parser('bar')
>>> parser.parse_args(['-h'])
usage: [-h] {foo,bar} ... optional arguments:
-h, --help show this help message and exit subcommands:
valid subcommands {foo,bar} additional help
>>> # sub-command functions
>>> def foo(args):
... print args.x * args.y
...
>>> def bar(args):
... print '((%s))' % args.z
...
>>> # create the top-level parser
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers()
>>>
>>> # create the parser for the "foo" command
>>> parser_foo = subparsers.add_parser('foo')
>>> parser_foo.add_argument('-x', type=int, default=1)
>>> parser_foo.add_argument('y', type=float)
>>> parser_foo.set_defaults(func=foo)
>>>
>>> # create the parser for the "bar" command
>>> parser_bar = subparsers.add_parser('bar')
>>> parser_bar.add_argument('z')
>>> parser_bar.set_defaults(func=bar)
>>>
>>> # parse the args and call whatever function was selected
>>> args = parser.parse_args('foo 1 -x 2'.split())
>>> args.func(args)
2.0
>>>
>>> # parse the args and call whatever function was selected
>>> args = parser.parse_args('bar XYZYX'.split())
>>> args.func(args)
((XYZYX))
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(dest='subparser_name')
>>> subparser1 = subparsers.add_parser('')
>>> subparser1.add_argument('-x')
>>> subparser2 = subparsers.add_parser('')
>>> subparser2.add_argument('y')
>>> parser.parse_args(['', 'frobble'])
Namespace(subparser_name='', y='frobble')
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--output', type=argparse.FileType('wb', 0))
>>> parser.parse_args(['--output', 'out'])
Namespace(output=<open file 'out', mode 'wb' at 0x...>)
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('infile', type=argparse.FileType('r'))
>>> parser.parse_args(['-'])
Namespace(infile=<open file '<stdin>', mode 'r' at 0x...>)
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
>>> group = parser.add_argument_group('group')
>>> group.add_argument('--foo', help='foo help')
>>> group.add_argument('bar', help='bar help')
>>> parser.print_help()
usage: PROG [--foo FOO] bar group:
bar bar help
--foo FOO foo help
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
>>> group1 = parser.add_argument_group('group1', 'group1 description')
>>> group1.add_argument('foo', help='foo help')
>>> group2 = parser.add_argument_group('group2', 'group2 description')
>>> group2.add_argument('--bar', help='bar help')
>>> parser.print_help()
usage: PROG [--bar BAR] foo group1:
group1 description foo foo help group2:
group2 description --bar BAR bar help
argparse.add_mutually_exclusive_group()创建一个互斥的参数组,这意味着同时只能指定其中的一个参数。>>> parser = argparse.ArgumentParser(prog='PROG')
>>> group = parser.add_mutually_exclusive_group()
>>> group.add_argument('--foo', action='store_true')
>>> group.add_argument('--bar', action='store_false')
>>> parser.parse_args(['--foo'])
Namespace(bar=True, foo=True)
>>> parser.parse_args(['--bar'])
Namespace(bar=False, foo=False)
>>> parser.parse_args(['--foo', '--bar'])
usage: PROG [-h] [--foo | --bar]
PROG: error: argument --bar: not allowed with argument --foo
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> group = parser.add_mutually_exclusive_group(required=True)
>>> group.add_argument('--foo', action='store_true')
>>> group.add_argument('--bar', action='store_false')
>>> parser.parse_args([])
usage: PROG [-h] (--foo | --bar)
PROG: error: one of the arguments --foo --bar is required
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', type=int)
>>> parser.set_defaults(bar=42, baz='badger')
>>> parser.parse_args([''])
Namespace(bar=42, baz='badger', foo=736)
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default='bar')
>>> parser.set_defaults(foo='spam')
>>> parser.parse_args([])
Namespace(foo='spam')
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default='badger')
>>> parser.get_default('foo')
'badger'
- ArgumentParser.print_usage([file]):打印用法示例
ArgumentParser.print_help([file]):打印帮助信息ArgumentParser.format_usage():格式化用法示例信息- ArgumentParser.format_help():格式化帮助信息
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='store_true')
>>> parser.add_argument('bar')
>>> parser.parse_known_args(['--foo', '--badger', 'BAR', 'spam'])
(Namespace(bar='BAR', foo=True), ['--badger', 'spam'])
def convert_arg_line_to_args(self, arg_line):
for arg in arg_line.split():
if not arg.strip():
continue
yield arg
python命令行解析工具argparse模块【5】的更多相关文章
- python命令行解析工具argparse模块【1】
argpaser是python中很好用的一个命令行解析模块,使用它我们可以很方便的创建用户友好型命令行程序.而且argparse会自动生成帮助信息和错误信息. 一.示例 例如下面的例子,从命令行中获取 ...
- python命令行解析工具argparse模块【3】
上一节,我们讲解了ArgumentParser对象,这一节我们将学习这个对象的add_argument()方法. add_argument()方法的定义了如何解析一个命令行参数,每个参 ...
- python命令行解析工具argparse模块【2】
上一节,我们简要的介绍了argparse的用法,接下来几节,将详细讲解其中的参数及用法,这一节我们讲解ArgumentParser对象. argparse.ArgumentParser([descri ...
- python命令行解析工具argparse模块【4】
上一节我们讲解了add_argument()方法,这一节我们将学习parse_args()方法. parse_args()方法的作用是解析命令行参数,并返回解析之后的 ...
- Python 命令行解析工具 Argparse介绍
最近在研究pathon的命令行解析工具,argparse,它是Python标准库中推荐使用的编写命令行程序的工具. 以前老是做UI程序,今天试了下命令行程序,感觉相当好,不用再花大把时间去研究界面问题 ...
- python之命令行解析工具argparse
以前写python的时候都会自己在文件开头写一个usgae函数,用来加上各种注释,给用这个脚本的人提供帮助文档. 今天才知道原来python已经有一个自带的命令行解析工具argparse,用了一下,效 ...
- Python命令行解析库argparse
2.7之后python不再对optparse模块进行扩展,python标准库推荐使用argparse模块对命令行进行解析. 1.example 有一道面试题:编写一个脚本main.py,使用方式如下: ...
- Python命令行解析库argparse(转)
原文:http://www.cnblogs.com/linxiyue/p/3908623.html 2.7之后python不再对optparse模块进行扩展,python标准库推荐使用argparse ...
- 【python】命令行解析工具argparse用法
python的命令行参数 之前有用到optget, optparse, 现在这些都被弃用了. import argparse parser = argparse.ArgumentParser() ar ...
随机推荐
- XHProf 初探
XHProf是一个分层PHP性能分析工具.它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情况.一个函数的开销,可细分成调用者和被调用者的开销,XHProf数据收集阶段,它记录调 ...
- C# DLL文件注册问题(涉及AxInterop.WMPLib.dll等)
近日遇到问题,给客户安装软件涉及视频等音影播放,安装软件启动过程遇到这样问题: 分析报错原因: 没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG ...
- TCP的核心系列 — SACK和DSACK的实现(一)
TCP的实现中,SACK和DSACK是比较重要的一部分. SACK和DSACK的处理部分由Ilpo Järvinen (ilpo.jarvinen@helsinki.fi) 维护. tcp_ack() ...
- Android中各种Adapter的使用方法
1.概念 Adapter是连接后端数据和前端显示的适配器接口.是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都须要用到Adapter.例如以下 ...
- 普通IT和文艺IT工程师的区别
在一个UITableView的editing设置的方法实现过程中,我想到两种写法,顺便想了一下两种方法的区别.觉得这时一个普通IT工程师和NB工程师的区别一个有趣的印记. 您通常时怎么去实现的呢? - ...
- Android TextView 实现文字大小不同和文字颜色不同
效果图如下: 关键代码如下: StringBuffer sb = new StringBuffer(); if(day > 0) { sb.append("<a href=\&q ...
- PDFium-PDF开源之旅(1)-初探代码下载编译
近日,Google和Foxit合作开源了Foxit的PDF源代码. 叫PDFium 相关新闻不少.哈,虽说已经不是程序猿了.只是还是有兴趣跑起来围观看看.废话少说.先说编译代码的步骤(事实上Googl ...
- 使用python抓取知乎日报的API数据
使用 urllib2 抓取数据时,最简单的方法是: import urllib2, json def getStartImage(): stream = urllib2.urlopen('http:/ ...
- string s = HttpContext.Current.Server.MapPath("");
string s = HttpContext.Current.Server.MapPath(""); 获取当前文件夹路径 而后用相对路径读取图片
- mvc模式jsp+servel+dbutils oracle基本增删改查demo
mvc模式jsp+servel+dbutils oracle基本增删改查demo 下载地址