27
27
# 2021-03-06 Meco Man 增加人工介入处理交互功能
28
28
# 2021-03-07 Meco Man 增加将RT-Thread版权信息的截至年份修改至今年功能
29
29
# 2021-03-14 Meco Man 增加将上海睿赛德版权信息的截至年份修改至今年功能
30
+ # 2021-06-07 iysheng Add support with format single file and parse command line parameters
30
31
31
32
# 本文件会自动对指定路径下的所有文件包括子文件夹的文件(.c/.h/.cpp)进行扫描
32
33
# 1)将源文件编码统一为UTF-8
40
41
# 欢迎对本文件的功能继续做出补充,欢迎提交PR
41
42
42
43
import os
44
+ import sys
43
45
import re
44
46
import chardet
45
47
import datetime
@@ -176,6 +178,10 @@ def convert_to_utf_8(path):
176
178
print ("编码失败,该文件处理失败" + path )
177
179
return False
178
180
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 ) #再对这个文件进行格式整理
179
185
180
186
# 递归扫描目录下的所有文件
181
187
def traversalallfile (path ):
@@ -185,18 +191,18 @@ def traversalallfile(path):
185
191
if os .path .isdir (filepath ):
186
192
traversalallfile (filepath )
187
193
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 )
195
195
196
196
def 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 )
200
206
201
207
if __name__ == '__main__' :
202
208
formatfiles ()
0 commit comments