@@ -1062,6 +1062,12 @@ def _script_main() -> int:
10621062 return main (* sys .argv [1 :])
10631063
10641064
1065+ def _usage_error (parser : argparse .ArgumentParser , message : str ) -> int :
1066+ parser .print_usage ()
1067+ print (message , file = sys .stderr )
1068+ return EX_USAGE
1069+
1070+
10651071def main (* args : str ) -> int :
10661072 """Contains flow control"""
10671073 try :
@@ -1081,30 +1087,27 @@ def main(*args: str) -> int:
10811087 print (f" { ifile } : { cfg_file } " )
10821088
10831089 if options .regex and options .write_changes :
1084- print (
1090+ return _usage_error (
1091+ parser ,
10851092 "ERROR: --write-changes cannot be used together with --regex" ,
1086- file = sys .stderr ,
10871093 )
1088- parser .print_help ()
1089- return EX_USAGE
10901094 word_regex = options .regex or word_regex_def
10911095 try :
10921096 word_regex = re .compile (word_regex )
10931097 except re .error as e :
1094- print (f'ERROR: invalid --regex "{ word_regex } " ({ e } )' , file = sys .stderr )
1095- parser .print_help ()
1096- return EX_USAGE
1098+ return _usage_error (
1099+ parser ,
1100+ f'ERROR: invalid --regex "{ word_regex } " ({ e } )' ,
1101+ )
10971102
10981103 if options .ignore_regex :
10991104 try :
11001105 ignore_word_regex = re .compile (options .ignore_regex )
11011106 except re .error as e :
1102- print (
1107+ return _usage_error (
1108+ parser ,
11031109 f'ERROR: invalid --ignore-regex "{ options .ignore_regex } " ({ e } )' ,
1104- file = sys .stderr ,
11051110 )
1106- parser .print_help ()
1107- return EX_USAGE
11081111 else :
11091112 ignore_word_regex = None
11101113
@@ -1117,24 +1120,20 @@ def main(*args: str) -> int:
11171120 )
11181121 for ignore_words_file in ignore_words_files :
11191122 if not os .path .isfile (ignore_words_file ):
1120- print (
1123+ return _usage_error (
1124+ parser ,
11211125 f"ERROR: cannot find ignore-words file: { ignore_words_file } " ,
1122- file = sys .stderr ,
11231126 )
1124- parser .print_help ()
1125- return EX_USAGE
11261127 build_ignore_words (ignore_words_file , ignore_words , ignore_words_cased )
11271128
11281129 uri_regex = options .uri_regex or uri_regex_def
11291130 try :
11301131 uri_regex = re .compile (uri_regex )
11311132 except re .error as e :
1132- print (
1133+ return _usage_error (
1134+ parser ,
11331135 f'ERROR: invalid --uri-regex "{ uri_regex } " ({ e } )' ,
1134- file = sys .stderr ,
11351136 )
1136- parser .print_help ()
1137- return EX_USAGE
11381137
11391138 uri_ignore_words = set (
11401139 itertools .chain (* parse_ignore_words_option (options .uri_ignore_words_list ))
@@ -1155,20 +1154,16 @@ def main(*args: str) -> int:
11551154 )
11561155 break
11571156 else :
1158- print (
1157+ return _usage_error (
1158+ parser ,
11591159 f"ERROR: Unknown builtin dictionary: { u } " ,
1160- file = sys .stderr ,
11611160 )
1162- parser .print_help ()
1163- return EX_USAGE
11641161 else :
11651162 if not os .path .isfile (dictionary ):
1166- print (
1163+ return _usage_error (
1164+ parser ,
11671165 f"ERROR: cannot find dictionary file: { dictionary } " ,
1168- file = sys .stderr ,
11691166 )
1170- parser .print_help ()
1171- return EX_USAGE
11721167 use_dictionaries .append (dictionary )
11731168 misspellings : Dict [str , Misspelling ] = {}
11741169 for dictionary in use_dictionaries :
@@ -1182,13 +1177,11 @@ def main(*args: str) -> int:
11821177 context = None
11831178 if options .context is not None :
11841179 if (options .before_context is not None ) or (options .after_context is not None ):
1185- print (
1180+ return _usage_error (
1181+ parser ,
11861182 "ERROR: --context/-C cannot be used together with "
11871183 "--context-before/-B or --context-after/-A" ,
1188- file = sys .stderr ,
11891184 )
1190- parser .print_help ()
1191- return EX_USAGE
11921185 context_both = max (0 , options .context )
11931186 context = (context_both , context_both )
11941187 elif (options .before_context is not None ) or (options .after_context is not None ):
@@ -1214,12 +1207,11 @@ def main(*args: str) -> int:
12141207 try :
12151208 glob_match .match ("/random/path" ) # does not need a real path
12161209 except re .error :
1217- print (
1210+ return _usage_error (
1211+ parser ,
12181212 "ERROR: --skip/-S has been fed an invalid glob, "
12191213 "try escaping special characters" ,
1220- file = sys .stderr ,
12211214 )
1222- return EX_USAGE
12231215
12241216 bad_count = 0
12251217 for filename in sorted (options .files ):
0 commit comments