具有自然语法的不显眼的argparse包装器

argh的Python项目详细描述


https://img.shields.io/coveralls/neithere/argh.svghttps://img.shields.io/travis/neithere/argh.svghttps://img.shields.io/pypi/format/argh.svghttps://img.shields.io/pypi/status/argh.svghttps://img.shields.io/pypi/v/argh.svghttps://img.shields.io/pypi/pyversions/argh.svghttps://img.shields.io/pypi/dd/argh.svghttps://readthedocs.org/projects/argh/badge/?version=stablehttps://readthedocs.org/projects/argh/badge/?version=latest

构建命令行界面?发现自己在说“啊!“当 与argparse的api斗争?不喜欢复杂性,但需要 权力?

Everything should be made as simple as possible, but no simpler.

—Albert Einstein (probably)

arghargparse的智能包装器。argparse是一个非常强大的工具; argh只是使其易于使用。

简而言之

< CART> ARGH >应用程序是{EM1} $简单但^ {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:

  • infer command name from function name;
  • infer arguments from function signature;
  • infer argument type from the default value;
  • infer argument action from the default value (for booleans);
  • add an alias root command ^{tt1}$ for the ^{tt2}$ argument.
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的关系

arghargparse完全兼容。你可以把不可知论和 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']

作者

安德烈·米哈林科自2010年开始开发。

有关此库贡献者的完整列表,请参见文件作者

支架

改进此项目的最快方法是提交经过测试和记录的 修补程序或详细的错误报告。

否则你就可以“压扁”我:Flattr the Argh project

许可

argh是免费软件:您可以重新分发和/或修改它 根据已发布的GNU Lesser通用公共许可条款 由自由软件基金会,许可证的第3版,或者 (由您选择)任何更高版本。

argh的发布是希望它能有用, 但没有任何保证;甚至没有 适销性或适合某一特定目的的适销性。见 GNU Lesser通用公共许可证了解更多详细信息。

你应该收到GNU Lesser通用公共许可证的副本 还有阿尔格。如果没有,请参见<;http://gnu.org/licenses/>;。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在创建对话框时设置模糊背景   java支持clojure中的xml和json REST响应   java在Android中通过多个JSON对象循环   java如何创建T类型的新对象   Java应用程序的设计   java使用GridView、适配器和毕加索制作流行电影应用程序   java在映射中交换值   java在同一活动/布局中多次使用同一片段   使用FixedLengthTokenizer使用java Spring FlatFileItemReader   javajavax。xml。ws。WebServiceException:javax。xml。肥皂SOAPException:错误代码QName必须是命名空间限定的!在weblogic server 12c中部署时   当我在构造函数中调用java Autowired属性时,该属性为null   线程“main”java中的linux异常。网BindException:地址已在使用中   java检查两个日期周期是否重叠   有没有办法通过安卓应用程序自动检测java服务器应用程序是否在线?   java检查2D数组中4个连续相同的对角线元素(连接4个游戏)   向Java数组添加数据   java组织。弹性搜索。客户运输NoNodeAvailableException:配置的节点均不可用:[]