函数注释>cli
clii的Python项目详细描述
克利
从python3函数注释生成参数解析器 样板。在
#!/usr/bin/env python3.8fromcliiimportApp,ArgfrompathlibimportPathfromsubprocessimportruncli=App()@cli.cmddefadd(a:int,b:int=3):print(a+b)@cli.cmddefsubtract(path:Path):run(f'rm -rf {path}')if__name__=='__main__':cli.run()
- 在
无依赖项。此库没有依赖项,是一个单独的文件。 你想卖吗?供应商it。在
在 - 在
Short implementation.花10分钟,浏览一下实现,说服 你自己,我不会把你的
在id_rsa
漏掉,然后把这只小狗卖了,永远不要 再想一想。在 - 在
无需学习。如果您知道如何使用Python函数注释,则 已经知道98%的图书馆了。在
在 - 在
针对常见情况进行了优化。查看
在test_bad_git.py
。我知道吗 你想做(创建git的一个subpar复制),我已经做到了 简洁的。在
好吧,你我都知道每个人最不需要的就是 生成命令行接口。添加附加依赖项的想法 为了让你能学到另一个 只是稍微比stdlib接口更符合人体工程学,用于解析args是正确的 在那里重写了你所有的makefile,不管是哪种风格 基于Javascript的构建系统。我明白了。在
是的,与其写这个库,我应该做点什么 有用的比如找个生活伴侣或者看看我能喝多少谷类酒 在《X档案》一集里喝酒,但每次我都会打出来一些 过于冗长的咒语docs.python.org 这是一年中第十六次,这是仅存的几块童心碎片之一 我心中充满了咖啡因,对计算机的好奇让我左右为难 自焚。在
Click相当于调用 在建筑师那里修理你的厨房水槽。它有很多代码和接口 冗长乏味。Docopt很整洁 但是它很慢,很新奇,也有大量的代码,我必须读3个例子 每次我用它之前。 Argparse是好的 builtin是这个库的高贵鼻祖,但它过于冗长 连接调用函数的子数组的常见任务是一种痛苦。在
不要牺牲你孩子般的奇迹。使用函数注释。用这个 愚蠢的图书馆。
安装
^{pr2}$实际使用示例
#!/usr/bin/env python3.8"""A really lame version of git."""frompathlibimportPathimporttypingastfromcliiimportApp,Argcli=App(description=__doc__)cli.add_arg('--verbose','-v',action='store_true',default=False)@cli.cmddefclone(url:str,target:Path,branch:t.Optional[str]=None):"""Clone the branch so you can melt your computer."""branch=f' -b {branch}'ifbranchelse''# We can reference global args since all parsed arguments are attached# to `cli.args` after parsing.ifcli.args.verbose:print(f'git clone{branch}{url}{target}')@cli.cmddefpush(remote:str,branch:str,force:bool=False):force_flag=' -f'ifforceelse''ifcli.args.verbose:print(f'git push{force_flag}{remote}{branch}')@cli.cmddefcommit(all:Arg('-a',bool)=False,message:Arg('-m',str)=None):# Arguments are --all, -a and --message, -mprint(all)print(message)@cli.cmddefadd(*files,updated:Arg('-u',bool)=False):# `files` will be a variadic positional arg, while --updated/-u is a bool# flag.ifcli.args.verbose:print(f"adding files: {files}")if__name__=='__main__':cli.run()
然后你就得到了
% ./test_bad_git.py --help
usage: test_bad_git.py [-h] [--verbose] {clone,push,commit,add} ...
A really lame version of git.
positional arguments:
{clone,push,commit,add}
optional arguments:
-h, --help show this help message and exit
--verbose, -v
% ./test_bad_git.py clone --help
usage: test_bad_git.py clone [-h] [--branch BRANCH] url target
Clone the branch so you can melt your computer.
positional arguments:
url
target
optional arguments:
-h, --help show this help message and exit
--branch BRANCH default: None
使用说明
docstrings
clii将从docstring中提取参数帮助文本,其格式如下:
importcliicli=clii.App()@cli.cmddeffoo(bar:str):""" Args: bar: some kind of helpful docstring. """cli.run()
具体来说,在docstring中搜索“[参数名称]:”—如果 模式,冒号后的内容用作帮助文本。在
store_true
和{}推断
将推断声明为bool类型并给定默认值的参数
作为store_true
或{
例如,在
@cli.cmddefcommit(force:bool=False):...
如果给定--force
,则函数将用force=True
调用,但是
否则force
将保持为False。在
如果你喜欢使用这个图书馆,可以考虑给我寄一些神奇的网络钱:
bc1qlj36t63qlgkywg83gwslcu3ehl76h2k2d6hcv2
- 项目
标签: