2727# 2021-03-06 Meco Man 增加人工介入处理交互功能
2828# 2021-03-07 Meco Man 增加将RT-Thread版权信息的截至年份修改至今年功能
2929# 2021-03-14 Meco Man 增加将上海睿赛德版权信息的截至年份修改至今年功能
30+ # 2021-06-07 iysheng Add support with format single file and parse command line parameters
3031
3132# 本文件会自动对指定路径下的所有文件包括子文件夹的文件(.c/.h/.cpp)进行扫描
3233# 1)将源文件编码统一为UTF-8
4041# 欢迎对本文件的功能继续做出补充,欢迎提交PR
4142
4243import os
44+ import sys
4345import re
4446import chardet
4547import datetime
@@ -176,6 +178,10 @@ def convert_to_utf_8(path):
176178 print ("编码失败,该文件处理失败" + path )
177179 return False
178180
181+ def formatfile (file ):
182+ if file .endswith (".c" ) == True or file .endswith (".h" ) == True : #只处理.c和.h文件
183+ if convert_to_utf_8 (file ) == True : #先把这个文件转为UTF-8编码,1成功
184+ format_codes (file ) #再对这个文件进行格式整理
179185
180186# 递归扫描目录下的所有文件
181187def traversalallfile (path ):
@@ -185,18 +191,18 @@ def traversalallfile(path):
185191 if os .path .isdir (filepath ):
186192 traversalallfile (filepath )
187193 elif os .path .isfile (filepath ):
188- if filepath .endswith (".c" ) == True or \
189- filepath .endswith (".cpp" ) == True or \
190- filepath .endswith (".h" ) == True :
191- # 若为.c/.h/.cpp文件,则开始进行处理
192- if convert_to_utf_8 (filepath ) == True : # 先把这个文件转为UTF-8编码
193- format_codes (filepath ) # 再对这个文件进行格式整理
194-
194+ formatfile (filepath )
195195
196196def formatfiles ():
197- workpath = input ('请输入扫描路径: ' )
198- traversalallfile (workpath )
199-
197+ if len (sys .argv ) > 1 :
198+ worktarget = sys .argv [1 ] # use the first command line parameter as worktarget
199+ else :
200+ worktarget = input ('Please enter work path or file to format: ' )
201+
202+ if os .path .isdir (worktarget ):
203+ traversalallfile (worktarget )
204+ else :
205+ formatfile (worktarget )
200206
201207if __name__ == '__main__' :
202208 formatfiles ()
0 commit comments