一个“牛逼”的 Python 命令行解析库

在 Python 中,命令行解析库有非常多的选择方案,系统自带的 模块有 argparse,有 Flask 作者写的 click,但是 argparse 使用起来啰嗦, 要经历解析器初始化、参数定义、解析一套流程。例如:

Python
# -*- coding: utf-8 -*- # arg_test.py import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/argparse" title="View all posts in argparse" target="_blank">argparse</a></span> def counter(file_type=None): return {file_type: 100} # 初始化解析器 parser = <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/argparse" title="View all posts in argparse" target="_blank">argparse</a></span>.ArgumentParser() # 定义参数 parser.add_argument(&quot;-f&quot;, &quot;--file&quot;, help=&quot;统计指定文件类型&quot;) # 解析 args = parser.parse_args() print(counter(args.file))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-
# arg_test.py
 
import argparse
 
def counter ( file_type = None ) :
     return { file_type : 100 }
 
# 初始化解析器
parser = argparse . ArgumentParser ( )
# 定义参数
parser . add_argument ( & quot ; - f & quot ; , & quot ; -- file & quot ; , help =& quot ;统计指定文件类型 & quot ; )
# 解析
args = parser . parse_args ( )
print ( counter ( args . file ) )
 

运行:

Python
$ <span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span> arg_test.py -f <span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span> {'<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span>': 100}
1
2
3
$ python arg_test . py - f python
{ 'python' : 100 }
 

click 则是以装饰器的形式作用在目标函数中,要侵入函数本身,本质上还是对 argparse 的一种改进,并没有太多创新,算是一种改良作品

Python
# -*- coding: utf-8 -*- import argparse import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/click" title="View all posts in click" target="_blank">click</a></span> @click.command() @click.option(&quot;-f&quot;, &quot;--file&quot;, help=&quot;统计制定文件类型&quot;) def counter(file=None): click.echo({file: 100}) if __name__ == '__main__': print(counter())
1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- coding: utf-8 -*-
 
import argparse
import click
 
@ click . command ( )
@ click . option ( & quot ; - f & quot ; , & quot ; -- file & quot ; , help =& quot ;统计制定文件类型 & quot ; )
def counter ( file = None ) :
     click . echo ( { file : 100 } )
 
if __name__ == '__main__' :
     print ( counter ( ) )
 

运行:

Python
$ python firetest.py -f python {u'python': 100}
1
2
3
$ python firetest . py - f python
{ u 'python' : 100 }
 

接下来要介绍的这个命令行解析工具非常牛逼,把命令行工具做到了极致,算是一种颠覆式创新,一行代码能把函数导出到命令行窗口,这个工具叫Python-fire,可能经常写命令行工具的人知道,该项目是 Google 开源的 Python 库(可能是 Google 工程师的 Side Project,因为项目主页申明说它不是 Google 官方项目),名字就像一团&#x1f525;。

安装

Python
pip install fire
1
2
pip install fire
 

用法1: fire.Fire()

直接在程序中调用 fire.Fire(),不需要修改目标函数,fire 会把当前模块下的所有对象导出到命令行中

Python
# firetest.py import fire def foo(name): return 'foo {name}!'.format(name=name) def bar(name): return &quot;bar {name}&quot;.format(name=name) if __name__ == '__main__': fire.Fire()
1
2
3
4
5
6
7
8
9
10
11
12
13
# firetest.py
 
import fire
 
def foo ( name ) :
     return 'foo {name}!' . format ( name = name )
 
def bar ( name ) :
     return & quot ; bar { name } & quot ; . format ( name = name )
 
if __name__ == '__main__' :
     fire . Fire ( )
 

运行:

Python
# 调用方式:python [文件名] [函数名] [参数] # 函数名后面直接跟参数值 $ python firetest.py foo hello foo hello! # 也可以显示地先指定参数名,再跟参数值 $ python firetest.py bar --name hello bar hello!
1
2
3
4
5
6
7
8
9
# 调用方式:python [文件名] [函数名] [参数]
 
# 函数名后面直接跟参数值
$ python firetest . py foo hello
foo hello !
# 也可以显示地先指定参数名,再跟参数值
$ python firetest . py bar -- name hello
bar hello !
 

用法2: fire.Fire(<fn>)

Fire 可以指定某个函数导出到命令行

Python
import fire def foo(name): return 'foo {name}!'.format(name=name) if __name__ == '__main__': fire.Fire(foo)
1
2
3
4
5
6
7
8
import fire
 
def foo ( name ) :
     return 'foo {name}!' . format ( name = name )
 
if __name__ == '__main__' :
     fire . Fire ( foo )
 

运行:

Python
# 调用方式:python [函数名] [参数] $ python firetest.py hello foo hello!
1
2
3
4
# 调用方式:python [函数名] [参数]
$ python firetest . py hello
foo hello !
 

当 Fire 接收函数 foo 作为参数时,只加载 foo 函数到命令行中,此时,在命令行中运行时也无需再指定函数名字,只需要指定参数就好。

用法3:fire.Fire(<dict>)

Fire 不仅可以接收函数,还可以接收字典对象作为参数,可在字典中配置那些函数需要导出到命令行中。例如,有加减乘3个函数,我们可以选择性的选择其中2个导出到命令行:

Python
import fire def add(x, y): return x + y def multiply(x, y): return x * y def subtract(x, y): return x - y if __name__ == '__main__': fire.Fire({ 'add': add, 'subtract': subtract, })
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import fire
 
def add ( x , y ) :
     return x + y
 
def multiply ( x , y ) :
     return x * y
 
def subtract ( x , y ) :
     return x - y
 
if __name__ == '__main__' :
     fire . Fire ( {
         'add' : add ,
         'subtract' : subtract ,
     } )
 

运行:

Python
$ python firetest.py add 1 4 5 $ python firetest.py subtract 1 4 -3 $ python firetest.py multiply 1 4 # multiply 会报错,因为没有导出
1
2
3
4
5
6
7
$ python firetest . py add 1 4
5
$ python firetest . py subtract 1 4
- 3
$ python firetest . py multiply 1 4
# multiply 会报错,因为没有导出
 

用法4:fire.Fire(<object>)

Fire 还可以接收类的实例对象

Python
import fire class Calculator(object): def add(self, x, y): return x + y def multiply(self, x, y): return x * y if __name__ == '__main__': calculator = Calculator() fire.Fire(calculator)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import fire
 
class Calculator ( object ) :
 
   def add ( self , x , y ) :
     return x + y
 
   def multiply ( self , x , y ) :
     return x * y
 
if __name__ == '__main__' :
   calculator = Calculator ( )
   fire . Fire ( calculator )
 

使用方式还是和前面的一样

Python
$ python firetest.py add 10 20 30 $ python firetest.py multiply 10 20 200
1
2
3
4
5
$ python firetest . py add 10 20
30
$ python firetest . py multiply 10 20
200
 

更多用法可以参考python-fire的文档:https://github.com/google/python-fire/blob/master/docs/guide.md

> https://mp.weixin.qq.com/s/lMOieL_RFRHsh5OW4G4axg




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值