具有自然语法的不显眼的argparse包装器
argh的Python项目详细描述
构建命令行界面?发现自己在说“啊!“当 与argparse的api斗争?不喜欢复杂性,但需要 权力?
Everything should be made as simple as possible, but no simpler.
—Albert Einstein (probably)
argh是argparse的智能包装器。argparse是一个非常强大的工具; argh只是使其易于使用。
简而言之
< CART> ARGH >应用程序是{EM1} $简单
Modular: | Declaration of commands can be decoupled from assembling and dispatching; |
---|---|
Pythonic: | Commands are declared naturally, no complex API calls in most cases; |
Reusable: | Commands are plain functions, can be used directly outside of CLI context; |
Layered: | The complexity of code raises with requirements; |
Transparent: | The full power of argparse is available whenever needed; |
Namespaced: | Nested commands are a piece of cake, no messing with subparsers (though they are of course used under the hood); |
Term-Friendly: | Command output is processed with respect to stream encoding; |
Unobtrusive: | Argh can dispatch a subset of pure-argparse code, and pure-argparse code can update and dispatch a parser assembled with Argh; |
DRY: | The amount of boilerplate code is minimal; among other things, Argh will:
|
NIH free: | Argh supports completion, progress bars and everything else by being friendly to excellent 3rd-party libraries. No need to reinvent the wheel. |
听起来不错吧?查看教程!
与argparse的关系
argh与argparse完全兼容。你可以把不可知论和 argh感知代码。记住调度员要做一些额外的工作 自定义调度程序可能不会这样做。
安装
使用pip:
$ pip install argh
Arch Linux(AUR):
$ yaourt python-argh
示例
一个非常简单的应用程序,只有一个命令:
importarghdefmain():return'Hello world'argh.dispatch_command(main)
运行:
$ ./app.py Hello world
具有多个命令的潜在模块化应用程序:
importargh# declaring:defecho(text):"Returns given word as is."returntextdefgreet(name,greeting='Hello'):"Greets the user with given name. The greeting is customizable."returngreeting+', '+name# assembling:parser=argh.ArghParser()parser.add_commands([echo,greet])# dispatching:if__name__=='__main__':parser.dispatch()
当然可以:
$ ./app.py greet Andy Hello, Andy $ ./app.py greet Andy -g Arrrgh Arrrgh, Andy
下面是这个应用程序的自动生成的帮助(注意docstrings 重复使用):
$ ./app.py help usage: app.py {echo,greet} ... positional arguments: echo Returns given word as is. greet Greets the user with given name. The greeting is customizable.
…对于特定命令(普通函数签名被转换 到cli参数):
$ ./app.py help greet usage: app.py greet [-g GREETING] name Greets the user with given name. The greeting is customizable. positional arguments: name optional arguments: -g GREETING, --greeting GREETING 'Hello'
(为了简洁起见,帮助信息被简化了一点。)
argh轻松地将纯python函数映射到cli。有时候这不是 够了;在这些情况下,还提供了强大的argparseapi:
@arg('text',default='hello world',nargs='+',help='The message')defecho(text):printtext
这些方法甚至可以安全地组合到这个水平:
# adding help to `foo` which is in the function signature:@arg('foo',help='blah')# these are not in the signature so they go to **kwargs:@arg('baz')@arg('-q','--quux')# the function itself:defcmd(foo,bar=1,*args,**kwargs):yieldfooyieldbaryield', '.join(args)yieldkwargs['baz']yieldkwargs['quux']
链接
- Project home page(github)
- Documentation(阅读文档)
- Package distribution(pypi)
- 问题、请求、错误报告等:
- Issue tracker(github)
- Mailing list(订阅以获取重要公告)
- 直接电子邮件(Gmail com上的neiThere)
许可
argh是免费软件:您可以重新分发和/或修改它 根据已发布的GNU Lesser通用公共许可条款 由自由软件基金会,许可证的第3版,或者 (由您选择)任何更高版本。
argh的发布是希望它能有用, 但没有任何保证;甚至没有 适销性或适合某一特定目的的适销性。见 GNU Lesser通用公共许可证了解更多详细信息。
你应该收到GNU Lesser通用公共许可证的副本 还有阿尔格。如果没有,请参见<;http://gnu.org/licenses/>;。