@@ -7,7 +7,7 @@ only inside %{ %} sections using clang-format with repository .clang-format
77style, keeping delimiters on their own lines.
88
99Usage:
10- python mccode-clangformat.py [--repo PATH] [--file FILTER] [--clang-format PATH] [--check]
10+ python mccode-clangformat.py [--repo PATH] [--filter FILTER] [--clang-format PATH] [--check]
1111"""
1212import argparse
1313import os
@@ -217,16 +217,21 @@ def main():
217217 ap = argparse .ArgumentParser (
218218 description = 'Format code inside McCode %{ %} blocks using repository .clang-format'
219219 )
220- ap .add_argument ('--repo' , '-r' , default = '.' , help = 'Repo path (default: current dir)' )
221- ap .add_argument ('--file' , '-f' , default = None , help = 'Only process files matching this label' )
220+ ap .add_argument ('--repo' , '-r' , default = None , help = 'Repo path' )
221+ ap .add_argument ('--filter' , '-F' , default = None , help = 'Only process files matching this filtering label' )
222+ ap .add_argument ('--file' , '-f' , default = None , help = 'Process this file only' )
223+ ap .add_argument ('--verbose' , '-v' , action = 'store_true' , help = 'Show progress messages' )
224+
222225 ap .add_argument ('--clang-format' , '-c' , default = None , help = 'Path to clang-format executable' )
223226 ap .add_argument ('--check' , action = 'store_true' , help = "Check only; don't write files" )
224227 args = ap .parse_args ()
225228
226- root = Path (args .repo ).resolve ()
227- if not root .exists ():
228- print (f'Repo path not found: { root } ' , file = sys .stderr )
229- sys .exit (2 )
229+ root = None
230+ if args .repo :
231+ root = Path (args .repo ).resolve ()
232+ if not root .exists ():
233+ print (f'Repo path not found: { root } ' , file = sys .stderr )
234+ sys .exit (2 )
230235
231236 # Warn if no .clang-format file is found in this repo tree
232237
@@ -250,16 +255,42 @@ def main():
250255
251256 clang_path = ensure_clang_format (args .clang_format )
252257
253- files = find_files (root ,args .file )
254- if not files :
255- print ('No .instr or .comp files found.' , file = sys .stderr )
258+ if args .file and root :
259+ print ('Incompatible input args: Please use EITHER -f some/file or -r directory/to/traverse' , file = sys .stderr )
256260 sys .exit (1 )
257261
258262 any_changed = False
259263 failures = []
260264
261- for f in files :
265+ if root :
266+ files = find_files (root ,args .filter )
267+ if not files :
268+ print ('No .instr or .comp files found in ' + root , file = sys .stderr )
269+ sys .exit (1 )
270+
271+ for f in files :
272+ if args .verbose :
273+ print ('Processing ' + str (f ))
274+ try :
275+ changed , msg = process_file (
276+ f , clang_path = clang_path , style_file = style_file , check_only = args .check
277+ )
278+ if msg :
279+ failures .append (msg )
280+ if changed :
281+ any_changed = True
282+ print (f'Changed: { f } ' )
283+ except FileNotFoundError :
284+ failures .append ('clang-format executable not found.' )
285+ break
286+ except Exception as e :
287+ failures .append (f'Error processing { f } : { e } ' )
288+
289+ if args .file :
262290 try :
291+ f = Path (args .file )
292+ if args .verbose :
293+ print ('Processing ' + str (f ))
263294 changed , msg = process_file (
264295 f , clang_path = clang_path , style_file = style_file , check_only = args .check
265296 )
@@ -269,8 +300,7 @@ def main():
269300 any_changed = True
270301 print (f'Changed: { f } ' )
271302 except FileNotFoundError :
272- failures .append ('clang-format executable not found.' )
273- break
303+ failures .append ('file not found.' )
274304 except Exception as e :
275305 failures .append (f'Error processing { f } : { e } ' )
276306
0 commit comments