Python command line 交互式框架

Python做为一种脚本语言,做为Perl的对手,在操做文件系统上面颇有一套, 因为语言的推广,在web方面也出现了不少Python的框架,最有名的莫过于Django了,其实不太熟悉Django,可是近些年来Python在web方面没有太多的进展,反而back end javascript,例如nodejs的崛起,带动了javascript做为一个后端语言风潮,和之前的PHP,以及基于Java的J2ee, SpringMVC(曾经占有40%的web框架)竞争,我的以为Python并不占优点。javascript

Python须要找到本身的发展方向,无疑Shellscript做为linux娘胎里带来的语言,其蹩脚性显而易见,而Perl is ugly, everyone knows。因此python能够在Command line这条路上多走一些。java

Prerequisite, I'm newbie in Python, so I just to share my learning python feelings. So Let's start.node

Python and Pip

我是用maven用习惯了,因此没有build工具,以及做为3rd party包查找工具,我确定要找一个,python确定要提供一个,我第一步找到的是Pip,easy_install好像也能够可是重复了,我就熟悉了pip就够用了。pip的安装比较简单,执行python的一个远程get.pip.py的脚本就能够(这个方式显得有些不专业不得不说,并且没有和linux apt-get, Mac brew等等结合,显得有些low).并且,pip在开发中也不是必须的,这点让人有些沮丧,说明在开发大型项目的时候,python的标准化,尚未十分完备(我的意见)。

python

Run pip list will get you have already install python packages. sys.path you'll get the directory which python 3rd party packages location.linux

提及来,npm和bower这种安装工具提供本地安装,可是pip没有,是让人沮丧的,npm安装默认是本地,若是全局要加-g。ios

npm install -g bower

Python command line framework

说到今天的主题了,一直以来,我须要一个交互式的命令行程序,动态的申请虚拟机,删除虚拟机,安装一些软件等等,我但愿都经过命令行来实现,而不是经过制做一个web端(之前,我一直是怎么作的)。因此我找到了python,找到了一些不错的,激动人心的python框架。
http://docs.python-guide.org/en/latest/scenarios/cli/
这篇文章中提到了几个。git

Clint

没有好的文档,确实,这点比较致命,并且无法知道框架的全貌,不过几个功能仍是不错的。https://github.com/kennethreitz/clint Github地址,由于是开发中,因此不完善是必须的,能够理解了。github

  • 能够打印color,有前置的提示符
from clint.textui import puts, indent, colored

puts(colored.red('this is a text'))

with indent(4,">>> "):
        puts(colored.yellow("hello?"))

puts(colored.green('this is the end'))

  • 处理输入参数
from clint import arguments
args = arguments.Args()
print args.get(0)

Run python test.py 123 will print 123.web

  • 处理输入流
from clint import piped_in
if __name__ == '__main__':
        in_data = piped_in()
        print in_data

Run python test.py < 1.txt will print 1.txt content.npm

  • 进度条打印
from time import sleep
from random import random
from clint.textui import progress

for i in progress.bar(range(100)):
        sleep(random()*0.2)

  • 提示框
from clint.textui import prompt,puts,colored,validators
name = prompt.query("What's your name?")
puts(name)
language = prompt.query("Your favorite tool (optional)?", validators=[])
puts(language)

Click

http://click.pocoo.org/3/ 更成熟的工具,研究一些高级功能。 格式化打印? 循环的询问? ……

相关文章
相关标签/搜索