Python写的简易代码统计工具(2)
本文介绍代码统计工具控制台界面部分。
控制台界面就是用命令行形式执行程序。主要内容是输入参数和结果输出。Python有一个命令行解析模块getopt。它能解析两种选项格式:短格式,长格式。
短格式:"-"号后面要紧跟一个选项字母。如果还有此选项的附加参数,可以用空格分开,也可以不分开。长度任意,可以用引号。
-v [正确]
-omyfile.py [正确]
-o myfile.py [正确]
-o "myfile.py" [正确]
长格式:"--"号后面要跟一个单词。如果还有些选项的附加参数,后面要紧跟"=",再加上参数。"="号前后不能有空格。
--output=myfile.py [正确]
-- output=myfile.py [不正确]
--output= myfile.py [不正确]
--output = myfile.py [不正确]
getopt模块中函数介绍
opts, args=getopt(args, shortopts, longopts=[])
参数args:需要解析的全部字符串
参数shortopt:短格式选项,如'hf:',h表示-h,f:表示-f string,冒号表示后面跟有参数。
参数longopt:长格式选项,如['help', 'output='],help表示--help,output=表示output=string,等号表示选项需要赋值。
返回值opts:选项和参数对。
返回值args:没有匹配上的选项的参数。
注:-f filename.py 中‘-f’是选项,‘filename’是参数。
如果匹配不符就会抛出异常,如格式是'hf:',则(假设程序名为program)
program -s [不适合的选项]
program -f [此选项需要跟参数]
都会抛出异常。
此程序制裁采用了短格式,它需要调用counter.py的函数。
下面是CodeCounter.py中的代码:
运行如下:
D:\Work\stat>CodeCounter.py -h
统计文件或目录的代码和注释
usage: CodeCounter [-hfdlmto] [string|n]
-h 显示帮助信息
-f string 统计string文件
-d string 统计string目录
-l n 统计n层子目录,-1表示所有子目录
-m string 要统计文件的后缀名,格式如:.c,.h,.cpp
-t string 代码类型,c表示C/C++语言,py表示Python
-o n 输出格式,0表示简易输出,1表示全部输出
D:\Work\stat>CodeCounter.py -d . -o 1
全部 代码 注释 空行 文件名
46 40 15 3 .\stat.
46 40 15 3 总计
待续
控制台界面就是用命令行形式执行程序。主要内容是输入参数和结果输出。Python有一个命令行解析模块getopt。它能解析两种选项格式:短格式,长格式。
短格式:"-"号后面要紧跟一个选项字母。如果还有此选项的附加参数,可以用空格分开,也可以不分开。长度任意,可以用引号。
-v [正确]
-omyfile.py [正确]
-o myfile.py [正确]
-o "myfile.py" [正确]
长格式:"--"号后面要跟一个单词。如果还有些选项的附加参数,后面要紧跟"=",再加上参数。"="号前后不能有空格。
--output=myfile.py [正确]
-- output=myfile.py [不正确]
--output= myfile.py [不正确]
--output = myfile.py [不正确]
getopt模块中函数介绍
opts, args=getopt(args, shortopts, longopts=[])
参数args:需要解析的全部字符串
参数shortopt:短格式选项,如'hf:',h表示-h,f:表示-f string,冒号表示后面跟有参数。
参数longopt:长格式选项,如['help', 'output='],help表示--help,output=表示output=string,等号表示选项需要赋值。
返回值opts:选项和参数对。
返回值args:没有匹配上的选项的参数。
注:-f filename.py 中‘-f’是选项,‘filename’是参数。
如果匹配不符就会抛出异常,如格式是'hf:',则(假设程序名为program)
program -s [不适合的选项]
program -f [此选项需要跟参数]
都会抛出异常。
此程序制裁采用了短格式,它需要调用counter.py的函数。
下面是CodeCounter.py中的代码:
#
-*- coding: cp936 -*-
'''
统计文件或目录的代码和注释
usage: CodeCounter [-hfdlmto] [string|n]
-h 显示帮助信息
-f string 统计string文件
-d string 统计string目录
-l n 统计n层子目录,-1表示所有子目录
-m string 要统计文件的后缀名,格式如:.c,.h,.cpp
-t string 代码类型,c表示C/C++语言,py表示Python
-o n 输出格式,0表示简易输出,1表示全部输出
'''
import sys
import getopt
# 统计工具的工作部分,见couter.py
from counter import CodeCounter
def usage():
print __doc__
def errmsg():
print " 参数不对,请参考帮助信息. \nCodeCounter -h显示帮助信息. "
def print_result(result, out):
print " 全部\t代码\t注释\t空行\t文件名 "
total = [0,0,0,0]
for ele in result:
total[0] += ele[ 1 ][ 4 ]
total[ 1 ] += ele[ 1 ][ 1 ] + ele[ 1 ][ 3 ]
total[ 2 ] += ele[ 1 ][ 2 ] + ele[ 1 ][ 3 ]
total[ 3 ] += ele[ 1 ][0]
if out == 1 :
for ele in result:
print " %d\t%d\t%d\t%d\t%s " % (ele[ 1 ][ 4 ], ele[ 1 ][ 1 ] + ele[ 1 ][ 3 ],
ele[ 1 ][ 2 ] + ele[ 1 ][ 3 ], ele[ 1 ][0], ele[0])
print " %d\t%d\t%d\t%d\t总计 " % (total[0], total[ 1 ], total[ 2 ], total[ 3 ])
def main(argv):
# 解析参数
try :
opts, args = getopt.getopt(argv[ 1 :], ' hf:d:m:l:t:o: ' )
except getopt.GetoptError:
errmsg()
return
(counter, out, result) = (CodeCounter(), 0, [])
# 设置参数
for opt, arg in opts:
if opt in [ ' -h ' ]:
usage()
return
elif opt in [ ' -f ' ]:
counter.AddCodeFiles( ' f ' , arg)
elif opt in [ ' -d ' ]:
counter.AddCodeFiles( ' d ' , arg)
elif opt in [ ' -m ' ]:
counter.SetModes(arg)
elif opt in [ ' -l ' ]:
counter.SetLevel(int(arg))
elif opt in [ ' -t ' ]:
counter.SetCodeType(arg)
elif opt in [ ' -o ' ]:
out = int(arg)
# 统计和输出结果
counter.Count(result)
print_result(result, out)
if __name__ == ' __main__ ' :
try :
main(sys.argv)
except :
errmsg()
'''
统计文件或目录的代码和注释
usage: CodeCounter [-hfdlmto] [string|n]
-h 显示帮助信息
-f string 统计string文件
-d string 统计string目录
-l n 统计n层子目录,-1表示所有子目录
-m string 要统计文件的后缀名,格式如:.c,.h,.cpp
-t string 代码类型,c表示C/C++语言,py表示Python
-o n 输出格式,0表示简易输出,1表示全部输出
'''
import sys
import getopt
# 统计工具的工作部分,见couter.py
from counter import CodeCounter
def usage():
print __doc__
def errmsg():
print " 参数不对,请参考帮助信息. \nCodeCounter -h显示帮助信息. "
def print_result(result, out):
print " 全部\t代码\t注释\t空行\t文件名 "
total = [0,0,0,0]
for ele in result:
total[0] += ele[ 1 ][ 4 ]
total[ 1 ] += ele[ 1 ][ 1 ] + ele[ 1 ][ 3 ]
total[ 2 ] += ele[ 1 ][ 2 ] + ele[ 1 ][ 3 ]
total[ 3 ] += ele[ 1 ][0]
if out == 1 :
for ele in result:
print " %d\t%d\t%d\t%d\t%s " % (ele[ 1 ][ 4 ], ele[ 1 ][ 1 ] + ele[ 1 ][ 3 ],
ele[ 1 ][ 2 ] + ele[ 1 ][ 3 ], ele[ 1 ][0], ele[0])
print " %d\t%d\t%d\t%d\t总计 " % (total[0], total[ 1 ], total[ 2 ], total[ 3 ])
def main(argv):
# 解析参数
try :
opts, args = getopt.getopt(argv[ 1 :], ' hf:d:m:l:t:o: ' )
except getopt.GetoptError:
errmsg()
return
(counter, out, result) = (CodeCounter(), 0, [])
# 设置参数
for opt, arg in opts:
if opt in [ ' -h ' ]:
usage()
return
elif opt in [ ' -f ' ]:
counter.AddCodeFiles( ' f ' , arg)
elif opt in [ ' -d ' ]:
counter.AddCodeFiles( ' d ' , arg)
elif opt in [ ' -m ' ]:
counter.SetModes(arg)
elif opt in [ ' -l ' ]:
counter.SetLevel(int(arg))
elif opt in [ ' -t ' ]:
counter.SetCodeType(arg)
elif opt in [ ' -o ' ]:
out = int(arg)
# 统计和输出结果
counter.Count(result)
print_result(result, out)
if __name__ == ' __main__ ' :
try :
main(sys.argv)
except :
errmsg()
运行如下:
D:\Work\stat>CodeCounter.py -h
统计文件或目录的代码和注释
usage: CodeCounter [-hfdlmto] [string|n]
-h 显示帮助信息
-f string 统计string文件
-d string 统计string目录
-l n 统计n层子目录,-1表示所有子目录
-m string 要统计文件的后缀名,格式如:.c,.h,.cpp
-t string 代码类型,c表示C/C++语言,py表示Python
-o n 输出格式,0表示简易输出,1表示全部输出
D:\Work\stat>CodeCounter.py -d . -o 1
全部 代码 注释 空行 文件名
46 40 15 3 .\stat.
46 40 15 3 总计
待续