生成命令行接口--google开源的fire使用体验【python-fire】
在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】的更多相关文章
- Google 开源的 Python 命令行库:初探 fire
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- Google 开源的 Python 命令行库:深入 fire(二)
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- 利用中文数据跑Google开源项目word2vec
一直听说word2vec在处理词与词的相似度的问题上效果十分好,最近自己也上手跑了跑Google开源的代码(https://code.google.com/p/word2vec/). 1.语料 首先准 ...
- Google开源的Deep-Learning项目word2vec
用中文把玩Google开源的Deep-Learning项目word2vec google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算te ...
- breakpad是Google开源的一套跨平台工具
windows下捕获dump之Google breakpad_client的理解 breakpad是Google开源的一套跨平台工具,用于dump的处理.很全的一套东西,我这里只简单涉及break ...
- [转]Google开源Leak Finder—用于检测内存泄漏的JavaScript工具-----可惜,暂时打不开google的网站,下载不了
近日,Google开源了Leak Finder,这款工具可以查看JavaScript应用的堆,进而发现内存泄漏. 作为一门垃圾收集语言,JavaScript并不会出现常见的内存泄露情况,特别是像C++ ...
- Google开源项目风格指南
Google开源项目风格指南 来源 https://github.com/zh-google-styleguide/zh-google-styleguide Google 开源项目风格指南 (中文版) ...
- Google 开源的依赖注入库,比 Spring 更小更快!
Google开源的一个依赖注入类库Guice,相比于Spring IoC来说更小更快.Elasticsearch大量使用了Guice,本文简单的介绍下Guice的基本概念和使用方式. 学习目标 概述: ...
- [Android]Google 开源的 Android 排版库:FlexboxLayout
最近Google开源了一个项目叫「FlexboxLayout」. 1.什么是 Flexbox 简单来说 Flexbox 是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方 ...
随机推荐
- nginx重启命令
service nginx restart nginx -s re
- Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)
An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...
- POJ 2001 Shortest Prefixes 【Trie树】
<题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...
- HDU 4027 Can you answer these queries【线段树】
<题目链接> 题目大意: 给定一段序列,现在对指定区间进行两种操作:一是对指定区间进行修改,对其中的每个数字都开根号(开根号后的数字仍然取整):二是对指定区间进行查询,查询这段区间所有数字 ...
- Python多重继承顺序---C3算法
什么是多重继承C3算法 MRO即 method resolution order (方法解释顺序),主要用于在多继承时判断属性的路径(来自于哪个类). 在python2.2版本中,算法基本思想是根据每 ...
- [译] Go数据结构-接口
原文 Go Data Structures: Interfaces 作者 Russ Cox 声明:本文目的仅仅作为个人mark,所以在翻译的过程中参杂了自己的思想甚至改变了部分内容.但由于译者水平有限 ...
- bootstrap panel如何实现可拖动并排序
Draggable Panels Bootstrap test 如果是使用bootstrap的panel请严重注意class = row 与class = col-....等的嵌套,要不然排序会出问 ...
- XamarinAndroid组件教程RecylerView动画组件使用动画(3)
XamarinAndroid组件教程RecylerView动画组件使用动画(3) (8)打开Main.axml文件,构建主界面.代码如下: <?xml version="1.0&quo ...
- vue注册和简单使用
组件的出现就是为了解决页面布局等等一些列的问题. vue中的组件分为两种,全局组件和局部组件. 一 . 注册全局组件 通过Vue.component()创建一个全局组件之后,我们可以在一个通过 ...
- 深入理解原型链(Prototype chain) __proto__
原型链(Prototype chain) 原型对象也是普通的对象,并且也有可能有自己的原型,如果一个原型对象的原型不为null的话,我们就称之为原型链(prototype chain). A prot ...