以前用不惯命令行,今天看别人源码,用Python click 库创建一个命令行API,简洁优雅,举个例子:

import click# 首先加上 command 装饰器
@click.command()# 接下来添加几个 option 装饰器, 用法就和 argparse 大体相同, 但是比 argparse 好用多了!!
# 这是字符串参数
@click.option('-i', '--img-path', default='./default.png',type=str, required=True, help='show image demo')
# 这是 int 参数
@click.option('--width', type=int, default=640, help='width')
@click.option('--height', type=int, default=360, help='height')# 这是 枚举 参数
@click.option('-s', '--stage', type=click.Choice(['train', 'val']), default='train', help='data stage')
# 这是 布尔 参数
@click.option('--async-mode/--no-async-mode', default=True, help='async mode or debug mode')
def stupid_scri(img_path, width, height, stage, async_mode):print(img_path, width, height, stage, async_mode)

以上是相当于写了个函数 stupid_scri,接下来要调用一下

if __name__ == "__main__":stupid_scri()

接下里外部调用一下:

$ python3 stupid_scri.py
./default.png 640 360 train True
$ python3 stupid_scri.py --height 3333
./default.png 640 3333 train True

我还找到了click的中文文档,社区开发者翻译的:
https://click-docs-zh-cn.readthedocs.io/zh/latest/

[每日一氵] python创建简单 command-line interface (CLI) API相关推荐

  1. 3DSlicer13:Command Line Interface(CLI)

    1.ExtensionWizard创建初始框架 1.1 ExtensionWizard综述 ExtensionWizard是一个工具的集合,既可以利用Slicer中的图像用户接口(GUI)访问,也可以 ...

  2. python编程例子 输入 输出-推荐 :手把手教你用Python创建简单的神经网络(附代码)...

    原标题:推荐 :手把手教你用Python创建简单的神经网络(附代码) 作者:Michael J.Garbade:翻译:陈之炎:校对:丁楠雅 本文共2000字,9分钟. 本文将为你演示如何创建一个神经网 ...

  3. SAP Fiori Tools 和对应的 CLI (Command Line Interface)

    帮助文档地址:https://help.sap.com/viewer/product/SAP_FIORI_tools/Latest/en-US SAP Fiori 工具提供了许多功能来提高使用 SAP ...

  4. mysql: [Warning] Using a password on the command line interface can be insecure.(using password:YES)

    前段时间不知道什么原因在Linux终端中出现无法启动Mysql的情况,在Linux终端中输入 mysql -uroot -p密码 的时候出现了下面这个错误: mysql: [Warning] Usin ...

  5. Using a password on the command line interface can be insecure,mysql报错解决办法

    mysql数据库升级后原先的数据库取数脚本使用 mysql -uname -ppassword 的方式登录数据库,但是现在报错'Using a password on the command line ...

  6. MySQL 5.6 Warning - Using a password on the command line interface can be insecur 解决方案

    MySQL 5.6 Warning - Using a password on the command line interface can be insecur 解决方案 参考文章: (1)MySQ ...

  7. 命令行导出和导入数据库 How to export and import MySQL database using command line Interface...

    导出 : To export MySQL database, open putty on Windows or Terminal in either MAC or Linux OS, Go to th ...

  8. mysql备份时过滤掉某些库 以及 去掉Warning: Using a password on the command line interface can be insecure.提示信息...

    在对mysql进行完整备份时使用--all-database参数 # mysqldump -u root -h localhost -p --all-database > /root/all.s ...

  9. MySQL 基准测试(mysqlslap)出现 Using a password on the command line interface can be insecure 警告

    问题描述:在使用 mysqlslap 进行基准测试时,出现警告 mysqlslap: [Warning] Using a password on the command line interface ...

  10. 已解决:mysql: [Warning] Using a password on the command line interface can be insecure.

    在执行命令时候报错 mysql: [Warning] Using a password on the command line interface can be insecure. 解决 将密码改为后 ...

最新文章

  1. 250相当于什么显卡_GTX1660Ti显卡搭配知识:GTX1660Ti配什么CPU和主板?
  2. [数分提高]2014-2015-2第4教学周第1次课
  3. 处理js事件时,获取键盘数字注意
  4. [shiro] - 怎样使用shiro?
  5. C语言 编写程序:请将Fibonacci数列前30项中的偶数值找出来,存储到一维数组中。其中,Fibonacci数列如下:1,1,2,3,5,8,13,21,34...该数列除前两项之外,其他任意
  6. 续流二极管原理及应用
  7. spring对JDBC的支持
  8. 若川诚邀你加源码共读群,帮助更多人学会看源码~
  9. java bitset_Java BitSet and()方法与示例
  10. 采用Docker集成jquery-file-upload组件到WEB应用
  11. python编程(你的电脑能够执行多少线程和进程)
  12. js高级编号笔记[新]-访问文档对象
  13. 使用spring的事务的三种方法
  14. 推荐:网站SEO内链详细操作指南
  15. like语法 mysql_mysql中like语法拼接4种方式
  16. IE浏览器打开网页之后,右键,选择“查看源文件”后打开桌面窗口的问题
  17. 微信公众号怎么集赞服务器,微信公众号分享集赞吸粉方案,人人可复制
  18. 学习spark的网站
  19. 用scratch编写游戏-数字华容道(不移动块法)
  20. 设置Chrome新标签页为自定义地址页面

热门文章

  1. 微软五笔 v1.0 下载
  2. 计算机专业英语朱龙主编,计算机专业英语(高职高专计算机系列)
  3. 微信公众平台消息接口开发(8)小黄鸡(小贱鸡)机器人
  4. ue4 相机移动到指定actor位置 SetViewTargetWithBlend
  5. python 期货策略_Python版商品期货跨期布林对冲策略.md
  6. 显示器色彩精度测试软件,色域及色彩精准度测试_液晶显示器_液晶显示器评测-中关村在线...
  7. 岁月让人从批判走向了建设
  8. 在MAME里如何设置组合键
  9. 计算机老师写对联给新人,写给教师结婚幽默对联【值得收藏】
  10. 裤子尺码对照表eur40_裤子的欧码32 34 36 38 40代表什么意思