功能检查中的简单clis

cmcaine-cli的Python项目详细描述


cli

An extremely easy to use library to generate python CLIs from functions through introspection.

自动生成等效值:

importargparseparser=argparse.ArgumentParser(description="Generate a cryptographic token with a given entropy.")parser.add_argument('method',nargs='?',default='xkcd',choices=('xkcd','short'))parser.add_argument('entropy',nargs='?',default=70,type=int)args=parser.parse_args()ifargs.method=='xkcd':print(xkcd(args.entropy))else:print(alphanumeric(args.entropy))

从这里:

fromcliimportChoice,clideftoken(method:Choice('xkcd','short')='xkcd',entropy=70):"Generate a cryptographic token with a given entropy."ifmethod=='xkcd':returnxkcd(entropy)else:returnalphanumeric(entropy)cli(token)()

显式地,cli(token)创建一个新函数,该函数接受字符串数组,解析,并根据从函数签名派生的规则自动转换它们,将解析的参数应用于原始token,并打印输出。cli(token)()使用默认的sys.argv调用该函数。

但是等等,还有更多!

支持关键字参数(可选或必需),因为每个函数有一个varargs参数。参数将自动转换为其默认参数的类型(如果不是none)或类型注释。

从函数签名到argparse规则的精确映射在generate_parser()的docstring中指定,但其思想是应该相当直观。

您甚至可以为整个模块(或具有函数属性的任何其他对象)生成CLI:

importexamplefromcliimportcli,opportunistic,coerce_number# You can use opportunistic(coerce_number) to convert any string that looks# like a number to a number so you don't have to annotate all the functions in a# module. YMMV.cli(example,default_type=opportunistic(coerce_number))()# If you want one, you can get a reference to the current module with# sys.modules[__name__]

低级API

generate_parser(your_function_here)将返回一个argparse.Parser实例。apply_namespace(your_function_here, namespace)将对函数应用命名空间对象(由parser.parse_args()返回)。

generate_parser_obj(your_module_or_class_here)将返回一个argparse.Parser,在

代码质量

代码很短,内联文档清晰,所有公布的特性都经过测试。

参考

功能签名解释: -(POSITIONAL_ONLYPOSITIONAL_OR_KEYWORD)=位置 -默认位置=可选位置 -KEYWORD_ONLY=选项 -默认值=默认值 -布尔特殊大小写 -如果默认值是TrueFalse,则该选项不接受任何 论据。相反,如果在 命令行,与默认值相反的值被赋予 功能。 -示例: ```Python def rm(*,force=false): 通过

	cli(rm)(['--force']) # ~== rm(force=True)
	cli(rm)([]) # ~== rm(force=False)
	```
- type annotations = type
- If the `type` is callable, it is called by `argparse` on the relevant substring
- If the type is `bool`, it is replaced by `coerce_bool`
- Provide your own custom function or handle the strings in your
  function body if you need something fancier.

特殊类型: -客户选择 -def foo(x:Choice(1,2))解释为add_argument('x', choices=(1,2), type=int)

defexample(positional,arguments):passdefdefaults(normally_one=1):passdeftyped(positional:bool,positional2:int):pass# keyword some-positional-arg --keyword2=foo --keyword1=4defkeyword(positional,*,keyword1=3,keyword2='default_filename'):pass# Mandatory keywords are bad, but you can have them if you want.defmandatory_keywords(positional,*,keyword1,keyword2):passdefvarargs(pos1,pos2,*rest):passdefchoice_from_list(person:Choice('Ann','Bob','Charlie'))pass# `flags --flag` is similar to flags(flag=True)# `flags --inverse_flag` is similar to flags(inverse_flag=False)defflags(*,flag=False,inverse_flag=True):pass

相关工作

其他制作clis的好方法:

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

推荐PyPI第三方库


热门话题
MongoDB,Java:以UTC格式检索日期属性   java无法在Azure表实体中创建字节字段   java创建倒计时但显示(x1):00错误   JavaAnt:如何创建不可变的文件集?   java在存储从txt到ArrayList的行时内存不足   java是否可以基于子接口自动生成构造函数?(有或没有龙目山)   java声明泛型   java如何处理selenium中SafariDriver的警报?   javamysqldatasource中的mysql-JDBC   如果计算机关闭,java应用程序创建的XML文件的内容将消失   selenium中的java启动列表<WebElement>   java使用数据库中的名称创建PDF文件   java以编程方式调用注释处理器   Java 2D游戏优化   arraylist使用Java SimpleXml序列化字符串列表,其中列表中的每个元素都是XML中的一个元素