在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. react 性能优化

    React 最基本的优化方式是使用PureRenderMixin,安装工具 npm i react-addons-pure-render-mixin --save,然后在组件中引用并使用 import ...

  2. 开启vue源码的解析攻略---认识flow

    javascript 是动态类型的代码,有很多的写法很不容易报错,想引入静态类型检查的flow,之前做项目的时候用的typascript的,看的大概的写法和 typescript 类似,因为规范避免了 ...

  3. VS2017动态链接库(.dll)的生成与使用

    转 https://blog.csdn.net/m0_37170593/article/details/76445972 这里以VS2017为例子,讲解一下动态链接库(.dll)的生成与使用. 一.动 ...

  4. HDU 2389 Rain on your Parade 最大匹配(模板题)【HK算法】

    <题目链接> 题目大意:有m个宾客,n把雨伞,预计时间t后将会下大雨,告诉你每个宾客的位置和速度,每把雨伞的位置,问你最多几个宾客能够拿到伞. 解题分析: 本题就是要我们求人与伞之间的最大 ...

  5. UVa11988 Broken Keyboard 损坏的键盘【list】

    题目链接:https://vjudge.net/problem/UVA-11988 题目大意: 键盘的home键和end键出现了问题. 在输入一段文本时,home键或end键可能会自动被按下,home ...

  6. Ajax技术使用之ajax与模态框结合的妙用

    Ajax技术使用之ajax与模态框结合的妙用 要求: 使用ajax的方式提交数据:https://www.cnblogs.com/-wenli/p/10470063.html 使用模态框完成增加数据, ...

  7. [ 高危 ]mt某站SQL注入

    RANK 24 金币    24 等价RMB  240 与上一漏洞同源所以只有24 数据包: GET /check?clientId=64915 HTTP/1.1 Host: xxx.meituan. ...

  8. iOS9 中 alertView 的使用

    "UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerSty ...

  9. flume初识

    一.flume特点 flume是目前大数据领域数据采集的一个利器,当然除了flume还有Fluentd和logstash,其他的目前来说并没有深入的了解,但是我觉得flume能够在大数据繁荣的今天屹立 ...

  10. webpack 入门踩坑

    参考来源:知乎张轩 安装 先装好node和npm,因为webpack是一个基于node的项目.然后 npm install -g webpack 全局安装 还可以在当前项目里面也安装一个webpack ...