# _*_ coding: utf-8 _*_
import sys,cmd
from cdctools import *
class PyCDC(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.CDROM='G:/'
self.CDDIR='cdc/'
self.prompt="(PyCDC)>-->>"
self.intro='''PyCDC0.5 manual:
dir dir #used to save and search the dir ,default is "cdc"
walk filename #used to traverse the cd infos,using ".cdc"
find keyword #used to save and search all the .cdc files to output the file with the keyword in it
? #query
EOF #exit the program
'''
def help_EOF(self):
print "Quits the program"
def do_EOF(self,line):
sys.exit()
def help_walk(self):
print "walk cd and export into *.cdc"
def do_walk(self,filename):
if filename == "":filename=raw_input("input the cdc`s name:")
print "Walk the cd and save the infos to :'%s'" %filename
cdWalker(self.CDROM,self.CDDIR+filename)
def help_dir(self):
print "This command is used to indentify the dir for saving/searching"
def do_dir(self,pathname):
if pathname=="": pathname=raw_input("Input the dir for saving/searching:" )
self.CDDIR=pathname
print "So the path for saving is : '%s'"%self.CDDIR
def help_find(self):
print "Search for the keyword"
def do_find(self,keyword):
if keyword == "": keyword=raw_input("Pls input the keyword: ")
print "Searching for the keyword :'%s' " % keyword
if __name__=='__main__':
cdc=PyCDC()
cdc.cmdloop()
实现点:
1. from cdctools import * 可以引入已有脚本中所有函式
2. self. 在类声明中应该是代表自个儿,代表实体化后的那个自个儿。
3. 类的定量应该在__init__(self)初始化时声明。
4. cmd的prompt就是命令行环境的前缀字串。
5. cmd的intro 就是命令行环境的最初提示。
6. 程序中高亮成红色的部分通过修改为self.CDDIR=pathname+'/' 就可以不用加/