2323- Some complex nested structures may not be handled perfectly
2424
2525Usage:
26- python check_docstring_formatting.py [directory]
26+ python check_docstring_formatting.py [paths...]
27+ python check_docstring_formatting.py -v [paths...]
2728
28- If no directory is specified, defaults to ../python relative to this script.
29+ If no paths are specified, defaults to ../python relative to this script.
2930"""
3031
32+ import argparse
3133import os
3234import re
3335import ast
@@ -187,24 +189,55 @@ def find_python_files(root_dir):
187189
188190
189191def main ():
190- # Default to checking the python directory relative to this script
191- script_dir = Path (__file__ ).parent
192- python_dir = script_dir .parent / 'python'
193-
194- if len (sys .argv ) > 1 :
195- python_dir = Path (sys .argv [1 ])
196-
197- if not python_dir .exists ():
198- print (f"Error: Directory { python_dir } does not exist" , file = sys .stderr )
199- sys .exit (1 )
200-
201- print (f"Checking Python files in: { python_dir } " )
202- print ("=" * 80 )
192+ parser = argparse .ArgumentParser (
193+ description = 'Check Python docstrings for formatting issues.'
194+ )
195+ parser .add_argument (
196+ 'paths' ,
197+ nargs = '*' ,
198+ help = 'Files or directories to check (default: python/ directory)'
199+ )
200+ parser .add_argument (
201+ '-v' , '--verbose' ,
202+ action = 'store_true' ,
203+ help = 'Show all files being checked, not just files with issues'
204+ )
205+
206+ args = parser .parse_args ()
207+
208+ # Determine what to check
209+ if args .paths :
210+ files_to_check = []
211+ for path_str in args .paths :
212+ path = Path (path_str )
213+ if path .is_dir ():
214+ files_to_check .extend (find_python_files (path ))
215+ elif path .is_file () and path .suffix == '.py' :
216+ files_to_check .append (path )
217+ else :
218+ print (f"Warning: { path_str } is not a valid Python file or directory" , file = sys .stderr )
219+ else :
220+ # Default to checking the python directory relative to this script
221+ script_dir = Path (__file__ ).parent
222+ python_dir = script_dir .parent / 'python'
223+
224+ if not python_dir .exists ():
225+ print (f"Error: Directory { python_dir } does not exist" , file = sys .stderr )
226+ sys .exit (1 )
227+
228+ files_to_check = find_python_files (python_dir )
229+
230+ if args .verbose :
231+ print (f"Checking Python files..." )
232+ print ("=" * 80 )
203233
204234 files_with_issues = 0
205235 total_issues = 0
206236
207- for py_file in sorted (find_python_files (python_dir )):
237+ for py_file in sorted (files_to_check ):
238+ if args .verbose :
239+ print (f"Checking { py_file } ..." , end = '' , flush = True )
240+
208241 docstrings = get_docstrings_from_file (py_file )
209242 file_issues = []
210243
@@ -221,17 +254,27 @@ def main():
221254 files_with_issues += 1
222255 total_issues += len (file_issues )
223256
224- # Make path relative to python_dir for cleaner output
225- rel_path = py_file .relative_to (python_dir .parent )
226- print (f"\n { rel_path } :" )
257+ if args .verbose :
258+ print (f" { len (file_issues )} issue(s) found" )
259+ else :
260+ print (f"{ py_file } : { len (file_issues )} issue(s) found" )
227261
228262 for line_num , issue_desc , node_type in sorted (file_issues ):
229263 print (f" Line ~{ line_num } ({ node_type } ): { issue_desc } " )
230-
231- print ("\n " + "=" * 80 )
232- print (f"Summary: Found { total_issues } issues in { files_with_issues } files" )
233-
234- return 0 if total_issues == 0 else 1
264+ else :
265+ if args .verbose :
266+ print (" OK" )
267+
268+ if total_issues > 0 :
269+ if args .verbose :
270+ print ("=" * 80 )
271+ print (f"\n Found { total_issues } issue(s) in { files_with_issues } file(s)" )
272+ return 1
273+ else :
274+ if args .verbose :
275+ print ("=" * 80 )
276+ print ("No issues found!" )
277+ return 0
235278
236279
237280if __name__ == '__main__' :
0 commit comments