@@ -1062,6 +1062,12 @@ def _script_main() -> int:
1062
1062
return main (* sys .argv [1 :])
1063
1063
1064
1064
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
+
1065
1071
def main (* args : str ) -> int :
1066
1072
"""Contains flow control"""
1067
1073
try :
@@ -1081,30 +1087,27 @@ def main(*args: str) -> int:
1081
1087
print (f" { ifile } : { cfg_file } " )
1082
1088
1083
1089
if options .regex and options .write_changes :
1084
- print (
1090
+ return _usage_error (
1091
+ parser ,
1085
1092
"ERROR: --write-changes cannot be used together with --regex" ,
1086
- file = sys .stderr ,
1087
1093
)
1088
- parser .print_help ()
1089
- return EX_USAGE
1090
1094
word_regex = options .regex or word_regex_def
1091
1095
try :
1092
1096
word_regex = re .compile (word_regex )
1093
1097
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
+ )
1097
1102
1098
1103
if options .ignore_regex :
1099
1104
try :
1100
1105
ignore_word_regex = re .compile (options .ignore_regex )
1101
1106
except re .error as e :
1102
- print (
1107
+ return _usage_error (
1108
+ parser ,
1103
1109
f'ERROR: invalid --ignore-regex "{ options .ignore_regex } " ({ e } )' ,
1104
- file = sys .stderr ,
1105
1110
)
1106
- parser .print_help ()
1107
- return EX_USAGE
1108
1111
else :
1109
1112
ignore_word_regex = None
1110
1113
@@ -1117,24 +1120,20 @@ def main(*args: str) -> int:
1117
1120
)
1118
1121
for ignore_words_file in ignore_words_files :
1119
1122
if not os .path .isfile (ignore_words_file ):
1120
- print (
1123
+ return _usage_error (
1124
+ parser ,
1121
1125
f"ERROR: cannot find ignore-words file: { ignore_words_file } " ,
1122
- file = sys .stderr ,
1123
1126
)
1124
- parser .print_help ()
1125
- return EX_USAGE
1126
1127
build_ignore_words (ignore_words_file , ignore_words , ignore_words_cased )
1127
1128
1128
1129
uri_regex = options .uri_regex or uri_regex_def
1129
1130
try :
1130
1131
uri_regex = re .compile (uri_regex )
1131
1132
except re .error as e :
1132
- print (
1133
+ return _usage_error (
1134
+ parser ,
1133
1135
f'ERROR: invalid --uri-regex "{ uri_regex } " ({ e } )' ,
1134
- file = sys .stderr ,
1135
1136
)
1136
- parser .print_help ()
1137
- return EX_USAGE
1138
1137
1139
1138
uri_ignore_words = set (
1140
1139
itertools .chain (* parse_ignore_words_option (options .uri_ignore_words_list ))
@@ -1155,20 +1154,16 @@ def main(*args: str) -> int:
1155
1154
)
1156
1155
break
1157
1156
else :
1158
- print (
1157
+ return _usage_error (
1158
+ parser ,
1159
1159
f"ERROR: Unknown builtin dictionary: { u } " ,
1160
- file = sys .stderr ,
1161
1160
)
1162
- parser .print_help ()
1163
- return EX_USAGE
1164
1161
else :
1165
1162
if not os .path .isfile (dictionary ):
1166
- print (
1163
+ return _usage_error (
1164
+ parser ,
1167
1165
f"ERROR: cannot find dictionary file: { dictionary } " ,
1168
- file = sys .stderr ,
1169
1166
)
1170
- parser .print_help ()
1171
- return EX_USAGE
1172
1167
use_dictionaries .append (dictionary )
1173
1168
misspellings : Dict [str , Misspelling ] = {}
1174
1169
for dictionary in use_dictionaries :
@@ -1182,13 +1177,11 @@ def main(*args: str) -> int:
1182
1177
context = None
1183
1178
if options .context is not None :
1184
1179
if (options .before_context is not None ) or (options .after_context is not None ):
1185
- print (
1180
+ return _usage_error (
1181
+ parser ,
1186
1182
"ERROR: --context/-C cannot be used together with "
1187
1183
"--context-before/-B or --context-after/-A" ,
1188
- file = sys .stderr ,
1189
1184
)
1190
- parser .print_help ()
1191
- return EX_USAGE
1192
1185
context_both = max (0 , options .context )
1193
1186
context = (context_both , context_both )
1194
1187
elif (options .before_context is not None ) or (options .after_context is not None ):
@@ -1214,12 +1207,11 @@ def main(*args: str) -> int:
1214
1207
try :
1215
1208
glob_match .match ("/random/path" ) # does not need a real path
1216
1209
except re .error :
1217
- print (
1210
+ return _usage_error (
1211
+ parser ,
1218
1212
"ERROR: --skip/-S has been fed an invalid glob, "
1219
1213
"try escaping special characters" ,
1220
- file = sys .stderr ,
1221
1214
)
1222
- return EX_USAGE
1223
1215
1224
1216
bad_count = 0
1225
1217
for filename in sorted (options .files ):
0 commit comments