在python中,命令行接口常用的argparse 和click,但是相对于python-fire 来说灵活度太缺了,fire可以直接将python中的函数,以命令行显示.

简单的介绍几个例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#filename: fire-demo.py 
__author__ = 'xijun1'
import fire def demo(param):
"""
just a demo
:param param: any str
:return:
"""
print "hello {} !".format(param)
return; if __name__ == '__main__':
fire.Fire()

执行的格式为:  python 如果fire.Fire()没有指定名称时:

需要在执行命令行时,带入函数名:   python  fire-demo.py  demo ,同时,如果函数有参数是,需要输入参数,不然会不错,并提示:

 python_demo python fire-demo.py demo
Fire trace:
1. Initial component
2. Accessed property "demo"
3. ('The function received no value for the required argument:', 'param') Type: function
String form: <function demo at 0x10e424c80>
File: ~/github/python_demo/fire-demo.py
Line: 20
Docstring: just a demo
:param param: any str
:return: Usage: fire-demo.py demo PARAM
fire-demo.py demo --param PARAM

如果带入参数: python  fire-demo.py  demo “google”

  python_demo python fire-demo.py demo  "google"
hello google !

或者我们也可以这样:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'xijun1'
import fire def demo(param):
"""
just a demo
:param param: any str
:return:
"""
print "hello {} !".format(param)
return; if __name__ == '__main__':
fire.Fire(demo)

只需要执行:

python_demo python fire-demo.py google
hello google !

当我们面对一个类Class的时候:

class ho:
def __init__(self,type):
self.type = type
pass def hodemo(self, param):
""" :param param: any string
:return:
"""
print "{} hello {} !".format( self.type,param)
return;

我们只需要执行:

python_demo python fire-demo.py ho --type "gxjun"  hodemo "google"
gxjun hello google !

当然我们也可以将类写入Fire中:

python_demo python fire-demo.py --type  gxjun hodemo google
gxjun hello google !

生成命令行接口--google开源的fire使用体验【python-fire】的更多相关文章

  1. Google 开源的 Python 命令行库:初探 fire

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  2. Google 开源的 Python 命令行库:深入 fire(二)

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  3. 利用中文数据跑Google开源项目word2vec

    一直听说word2vec在处理词与词的相似度的问题上效果十分好,最近自己也上手跑了跑Google开源的代码(https://code.google.com/p/word2vec/). 1.语料 首先准 ...

  4. Google开源的Deep-Learning项目word2vec

    用中文把玩Google开源的Deep-Learning项目word2vec   google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算te ...

  5. breakpad是Google开源的一套跨平台工具

    windows下捕获dump之Google breakpad_client的理解   breakpad是Google开源的一套跨平台工具,用于dump的处理.很全的一套东西,我这里只简单涉及break ...

  6. [转]Google开源Leak Finder—用于检测内存泄漏的JavaScript工具-----可惜,暂时打不开google的网站,下载不了

    近日,Google开源了Leak Finder,这款工具可以查看JavaScript应用的堆,进而发现内存泄漏. 作为一门垃圾收集语言,JavaScript并不会出现常见的内存泄露情况,特别是像C++ ...

  7. Google开源项目风格指南

    Google开源项目风格指南 来源 https://github.com/zh-google-styleguide/zh-google-styleguide Google 开源项目风格指南 (中文版) ...

  8. Google 开源的依赖注入库,比 Spring 更小更快!

    Google开源的一个依赖注入类库Guice,相比于Spring IoC来说更小更快.Elasticsearch大量使用了Guice,本文简单的介绍下Guice的基本概念和使用方式. 学习目标 概述: ...

  9. [Android]Google 开源的 Android 排版库:FlexboxLayout

    最近Google开源了一个项目叫「FlexboxLayout」. 1.什么是 Flexbox 简单来说 Flexbox 是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方 ...

随机推荐

  1. Linux下C语言的socket网络编程

    关于详细的服务器建立的步骤以及相关的socket套接字的知识我已经在python socket编程的文章中提到过了,大家可以参看那一篇博客来历接socket套接字编程的内容,由于要是用C相关的API所 ...

  2. Webmin详细安装过程及问题解决

    管理系统是件艰巨的任务,创建用户账户,配置服务,检查日志,还有系统管理员必须面对的所有其他的职责,都使系统管理工作成为一个不小的负担.下面介绍一个叫webmin的软件,webmin软件安装后能让读者从 ...

  3. 解决资源id冲突

    --摘自<android插件化开发指南> 1.一套完整的Android App打包流程(Gradle方案) 第一步:aapt.为res目录下的资源生成R.java文件,同时为Android ...

  4. Mysql数据库报错:Cannot add or update a child row: a foreign key constraint fails(添加多对多关系)

    #创建班级表 class Classes(models.Model): title = models.CharField(max_length=32) n=models.ManyToManyField ...

  5. AOP编程-理论篇

    本节内容主要讲解AOP编程理念,包括概念讲解,使用AOP的优势,如何实现AOP,常见的实现AOP的方法. 1.AOP的概念 AOP是Aspect Oriented Programming的缩写,意思是 ...

  6. 机器学习实战笔记-k-近邻算法

    机器学习实战笔记-k-近邻算法 目录 1. k-近邻算法概述 2. 示例:使用k-近邻算法改进约会网站的配对效果 3. 示例:手写识别系统 4. 小结 本章介绍了<机器学习实战>这本书中的 ...

  7. 大数据环境完全分布式搭建linux(centos)中安装zookeeper

    切记 要关闭防火墙   chkconfig iptables off(关闭防火墙的命令) 1.解压安装包 tar -zxvf zookeeper-3.4.5.tar.gz 2.在conf文件夹下 修改 ...

  8. CodeForces round 967 div2 题解(A~E)

    本来准备比完赛就写题解的, 但是一拖拖了一星期, 唉 最后一题没搞懂怎么做,恳请大神指教 欢迎大家在评论区提问. A Mind the Gap 稳定版题面 https://cn.vjudge.net/ ...

  9. C#中的集合之ArryList

    List泛型集合 集合是OOP中的一个重要概念,C#中对集合的全面支持更是该语言的精华之一. 为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList 直接 ...

  10. 潭州课堂25班:Ph201805201 django 项目 第四十课 后台 文章发布,更新实现,热门新闻管理,轮播图管理(课堂笔记)

    把图片上传到 七牛云,必须经过后台的许可, 在虚拟机中安装七牛云所需模块pip install qiniu # 创建utils/secrets/qiniu_secret_info.py文件 # 从七牛 ...