在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. poi设置打印页页脚和页数设置

    之前在网上搜了很久,也没有搜到具体页脚页数的答案,最后还是在官方api文档上找到了答案: HSSFPrintSetup printSetup = (HSSFPrintSetup) sheet.getP ...

  2. 队列queue实现线程的消费者和生产者

    import threading import queue import random import time qq = queue.Queue(4) #实例化一个队列,因为是一个进程的线程,所以共资 ...

  3. jQuery获得页面绝对和相对的位置

    获得某一元素绝对x,y位置,可以用offset方法 var X = $('#DivID').offset().top; var y=$("#divid").offset().lef ...

  4. Codeforces 998D. Roman Digits 【打表找规律】

    <题目链接> 题目大意: 现在有无限个 1,5,10,50这四个数字,从中恰好挑选n个数字,问你这些数字的和总共有多少种不同的情况. 解题分析: 由于此题 n 的范围特别大,达到了1e9, ...

  5. JVM 调优-给你的java应用看看病

    目录 java 应用 1 cpu 负载过高 1.1 分析问题 1.2 解决方案 2 内存占用过多 2.1 从内存回收方面 2.2 从代码层面 java 应用 1 cpu 负载过高 1.1 分析问题 首 ...

  6. webpack搭建vue项目,实现脚手架功能

    本文基于node.js开发环境,安装完node之后新建项目,通过webpack配置,实现vue-cli脚手架功能 对于刚刚接触编程的人来说,最难的可能并不是学习一种新语法或者框架,而是编程思维,这种思 ...

  7. sqldeveloper 设置快捷

  8. SpringMVC(十三) RequestMapping 使用servlet原生API作为参数

    SpringMVC支持以下Servlet方法: HttpServletRequest HttpServletResponse HttpSession Writer Reader Locale Inpu ...

  9. maven 构建spring boot + mysql 的基础项目

    一.maven 依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  10. 因数表进阶:1--x的因数和

    紧接着上一个文章,进阶一个因数表,来自牛客网一道比赛题: 打从1到n所有因数的和 代码如下: #include<cstdio> #define ll long long using nam ...