@@ -1943,13 +1943,15 @@ def get_config_file_list(start_dir):
19431943 dir = parent
19441944 return config_file_list
19451945
1946- arguments = {'prog' : argv [0 ],
1946+ def get_argparse_arguments ():
1947+ arguments = {'prog' : argv [0 ],
19471948 'description' : 'Auto-format modern Fortran source files.' ,
19481949 'formatter_class' : argparse .ArgumentDefaultsHelpFormatter }
19491950
1950- if argparse .__name__ == "configargparse" :
1951- arguments ['args_for_setting_config_path' ] = ['-c' , '--config-file' ]
1952- arguments ['description' ] = arguments ['description' ] + " Config files ('.fprettify.rc') in the home (~) directory and any such files located in parent directories of the input file will be used. When the standard input is used, the search is started from the current directory."
1951+ if argparse .__name__ == "configargparse" :
1952+ arguments ['args_for_setting_config_path' ] = ['-c' , '--config-file' ]
1953+ arguments ['description' ] = arguments ['description' ] + " Config files ('.fprettify.rc') in the home (~) directory and any such files located in parent directories of the input file will be used. When the standard input is used, the search is started from the current directory."
1954+ return arguments
19531955
19541956 def get_arg_parser (args ):
19551957 """helper function to create the parser object"""
@@ -2027,9 +2029,17 @@ def get_arg_parser(args):
20272029 version = '%(prog)s 0.3.7' )
20282030 return parser
20292031
2030- parser = get_arg_parser (arguments )
2031-
2032- args = parser .parse_args (argv [1 :])
2032+ def pars_args_with_config_file (directory ):
2033+ """
2034+ Parse arguments together with the config file.
2035+ Requires configargparse package.
2036+ """
2037+ filearguments = get_argparse_arguments ()
2038+ filearguments ['default_config_files' ] = ['~/.fprettify.rc' ] \
2039+ + get_config_file_list (directory if directory != '-' else os .getcwd ())
2040+ file_argparser = get_arg_parser (filearguments )
2041+ file_args = file_argparser .parse_args (argv [1 :])
2042+ return file_args
20332043
20342044 def build_ws_dict (args ):
20352045 """helper function to build whitespace dictionary"""
@@ -2046,6 +2056,19 @@ def build_ws_dict(args):
20462056 ws_dict ['intrinsics' ] = args .whitespace_intrinsics
20472057 return ws_dict
20482058
2059+ def build_case_dict (args ):
2060+ """helper function to build case dictionary"""
2061+ return {
2062+ 'keywords' : file_args .case [0 ],
2063+ 'procedures' : file_args .case [1 ],
2064+ 'operators' : file_args .case [2 ],
2065+ 'constants' : file_args .case [3 ]
2066+ }
2067+
2068+ parser = get_arg_parser (get_argparse_arguments ())
2069+
2070+ args = parser .parse_args (argv [1 :])
2071+
20492072 # support legacy input:
20502073 if 'stdin' in args .path and not os .path .isfile ('stdin' ):
20512074 args .path = ['-' if _ == 'stdin' else _ for _ in args .path ]
@@ -2064,6 +2087,7 @@ def build_ws_dict(args):
20642087 sys .stderr .write ("%s is a directory. Use --recursive option\n " % directory )
20652088 sys .exit (1 )
20662089
2090+
20672091 if not args .recursive :
20682092 filenames = [directory ]
20692093 else :
@@ -2077,46 +2101,34 @@ def build_ws_dict(args):
20772101
20782102 for dirpath , dirnames , files in os .walk (directory ,topdown = True ):
20792103
2104+ file_args = args
2105+ if argparse .__name__ == "configargparse" :
2106+ file_args = pars_args_with_config_file (directory )
2107+
20802108 # Prune excluded patterns from list of child directories
20812109 # https://stackoverflow.com/a/19859907
20822110 dirnames [:] = [dirname for dirname in dirnames if not any (
2083- [ fnmatch (dirname ,exclude_pattern ) or fnmatch (os .path .join (dirpath ,dirname ),exclude_pattern )
2084- for exclude_pattern in args .exclude ]
2111+ fnmatch (dirname ,exclude_pattern ) or fnmatch (os .path .join (dirpath ,dirname ),exclude_pattern )
2112+ for exclude_pattern in file_args .exclude
20852113 )]
20862114
20872115 for ffile in [os .path .join (dirpath , f ) for f in files
20882116 if any (f .endswith (_ ) for _ in ext )
2089- and not any ([
2117+ and not any (
20902118 fnmatch (f ,exclude_pattern )
2091- for exclude_pattern in args .exclude ] )]:
2119+ for exclude_pattern in file_args .exclude )]:
20922120 filenames .append (ffile )
20932121
20942122 for filename in filenames :
20952123
20962124 # reparse arguments using the file's list of config files
2097- filearguments = arguments
2125+ file_args = args
20982126 if argparse .__name__ == "configargparse" :
2099- filearguments ['default_config_files' ] = ['~/.fprettify.rc' ] \
2100- + get_config_file_list (os .path .dirname (os .path .abspath (filename )) if filename != '-' else os .getcwd ())
2101- file_argparser = get_arg_parser (filearguments )
2102- file_args = file_argparser .parse_args (argv [1 :])
2103-
2104- # Skip excluded files but not if they we specified explicitly
2105- # on the command line.
2106- if filename != directory and any (
2107- fnmatch (os .path .basename (filename ), exclude_pattern )
2108- for exclude_pattern in file_args .exclude ):
2109- if file_args .debug :
2110- sys .stderr .write ("Skipping excluded file %s\n " % filename )
2111- continue
2127+ dirname = os .path .dirname (os .path .abspath (filename ))
2128+ file_args = pars_args_with_config_file (dirname )
21122129
21132130 ws_dict = build_ws_dict (file_args )
2114- case_dict = {
2115- 'keywords' : file_args .case [0 ],
2116- 'procedures' : file_args .case [1 ],
2117- 'operators' : file_args .case [2 ],
2118- 'constants' : file_args .case [3 ]
2119- }
2131+ case_dict = build_case_dict (file_args )
21202132
21212133 stdout = file_args .stdout or directory == '-'
21222134 diffonly = file_args .diff
0 commit comments