clap是用于命令行和子进程脚本开发的工具。
clasp的Python项目详细描述
关于
clap(命令行和子进程)扩展了click(https://palletsprojects.com/p/click/) 并提供一个函数库,帮助开发命令行 需要频繁调用系统可执行文件的工具。特殊结构 程序[选项]命令参数[选项]已实现 设置读取和写入配置文件和组命令的简单方法 允许有组织地开发命令行工具套件的程序。
安装
pip install clasp
用法
命令行程序具有以下结构:
"""template file.""" from __future__ import print_function import clasp.click as click import clasp.click_ext as clk @click.group() @clk.shared_decs(clk.main_decs) def main(ctx, config, outconfig, configalias, inputalias): """template file for script development.""" clk.get_config(ctx, config, outconfig, configalias, inputalias) @main.command('XXX') @click.argument('arg1') @click.option('--opts','-opts', is_flag=True, help="check parsed options") @click.option('--debug', is_flag=True, help="show traceback on exceptions") @click.pass_context def XXX(ctx, arg1, **kwargs): """ docstring with full help text for command """ if kwargs['opts']: kwargs['opts'] = False clk.echo_args(arg1,**kwargs) else: try: ########## #code body ########## except click.Abort: raise except Exception as ex: clk.print_except(ex, kwargs['debug']) return 'XXX', kwargs, ctx @main.resultcallback() @click.pass_context def printconfig(ctx, opts, **kwargs): """callback to save config file""" try: clk.tmp_clean(opts[2]) except Exception: pass if kwargs['outconfig']: clk.print_config(ctx, opts, kwargs['outconfig'], kwargs['config'], kwargs['configalias'])
在设置中设置入口点:
entry_points={"console_scripts":'program=package.program:main'}
然后执行:
program XXX arg1 [options]
启用选项的自动完成功能:
_PROGRAM_COMPLETE=source program > bash_complete.sh # put this in .bash_profile (with path) source bash_complete.sh
脚本
有关匹配已解析参数的有用函数,请参阅脚本工具文档 使用ipyparallel调用函数、调用子进程和并行处理 本地和通过ssh。
回调
除了click提供的强大的选项解析之外 回调是clap的一部分,它有助于分析常用的参数
文件输入
文件输入可以使用通配符扩展(使用引号,以便回调处理) 使用glob加上以下各项:
- [abc] (one of a, b, or c)
- [!abc] (none of a, b or c)
- ‘-‘ (hyphen) collect the stdin into a temporary file (clasp_tmp*)
- ~ expands user
文件输入回调是:
- parse_file_list: returns list of files (raise error if file not found)
- is_file: check if a single path exists (prompts for user input if file not found)
- are_files: recursively calls parse_file_list and prompts on error
- is_file_iter: use when multiple=True
- are_files_iter: use when mulitple=True
- are_files_or_str: tries to parse as files, then tries split_float, then split_int, then returns string
- are_files_or_str_iter: use when mulitple=True
字符串分析
- split_str: split with shlex.split
- split_str_iter: use when multiple=True
- color_inp: return alphastring, split on whitespace, convert floats and parse tuples on ,
数字解析
- tup_int: parses integer tuples from comma/space seperated string
- tup_float: parses float tuples from comma/space seperated string
- split_float: splits list of floats and extends ranges based on : notation
- split_int: splits list of ints and extends ranges based on : notation
文档
单击和sphinx_单击使帮助和文档变得超级简单,但是 为–help和sphinx格式化docstring时有一些冲突。 clap.sphinx_click_ext尝试解决这些冲突并对选项进行排序 并根据上面显示的脚本模板显示帮助。与狮身人面像一起使用 在conf.py中的扩展名中添加“clap.sphinx_click_ext”
- 热门话题
- java JList侦听器找不到符号 多点触摸跨平台java应用程序(Windows、Mac和Linux(Ubuntu)(可能是Android)) 在UnitTest中找不到java类加载器资源 java当我尝试在Eclipse中运行我的minecraft mod时,我总是遇到这个错误,这是什么意思? 内部有一个同步块的静态方法,在java中阻止整个类? 不支持java操作:具有BillableStatus的费用在采购中应具有AccountBasedExpenseLineDetail行的CustomerRef java运行时常量池GC集合 java无法解析配置“:app:\u debugApk”的所有依赖项。找不到名为“default”的配置 在GCP计算引擎中托管Neo4j的java java如何从BroadcastReceiver更新片段选项卡? 线程“main”组织中的java异常。冬眠注释异常:@org。冬眠注释。表引用未知表:ProductForPractice 在Java/Groovy中嵌入处理 java Android REST教程/示例项目 java我可以在同一测试中在一个模拟器上启动应用程序,在另一个模拟器上启动移动浏览器吗? java如何更改通知横幅的方向? 安卓 Java帮助实现Firebase授权 if else之后的rs.next()中出现java错误?