From 9474719eb7a1467119787bb2f15c6befcae2a8f2 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 2 Jun 2024 23:13:11 +0200 Subject: [PATCH] Refactor to keep the code DRY --- codespell_lib/_codespell.py | 66 +++++++++++-------------------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 6b06808cc53..d48fe68d163 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -1062,6 +1062,12 @@ def _script_main() -> int: return main(*sys.argv[1:]) +def _usage_error(parser: argparse.ArgumentParser: message: str) -> int: + parser.print_usage() + print(message, file=sys.stderr) + return EX_USAGE + + def main(*args: str) -> int: """Contains flow control""" try: @@ -1081,30 +1087,22 @@ def main(*args: str) -> int: print(f" {ifile}: {cfg_file}") if options.regex and options.write_changes: - parser.print_usage() - print( - "ERROR: --write-changes cannot be used together with --regex", - file=sys.stderr, + return _usage_error( + "ERROR: --write-changes cannot be used together with --regex" ) - return EX_USAGE word_regex = options.regex or word_regex_def try: word_regex = re.compile(word_regex) except re.error as e: - parser.print_usage() - print(f'ERROR: invalid --regex "{word_regex}" ({e})', file=sys.stderr) - return EX_USAGE + return _usage_error(f'ERROR: invalid --regex "{word_regex}" ({e})') if options.ignore_regex: try: ignore_word_regex = re.compile(options.ignore_regex) except re.error as e: - parser.print_usage() - print( - f'ERROR: invalid --ignore-regex "{options.ignore_regex}" ({e})', - file=sys.stderr, + return _usage_error( + f'ERROR: invalid --ignore-regex "{options.ignore_regex}" ({e})' ) - return EX_USAGE else: ignore_word_regex = None @@ -1117,24 +1115,16 @@ def main(*args: str) -> int: ) for ignore_words_file in ignore_words_files: if not os.path.isfile(ignore_words_file): - parser.print_usage() - print( - f"ERROR: cannot find ignore-words file: {ignore_words_file}", - file=sys.stderr, + return _usage_error( + f"ERROR: cannot find ignore-words file: {ignore_words_file}" ) - return EX_USAGE build_ignore_words(ignore_words_file, ignore_words, ignore_words_cased) uri_regex = options.uri_regex or uri_regex_def try: uri_regex = re.compile(uri_regex) except re.error as e: - parser.print_usage() - print( - f'ERROR: invalid --uri-regex "{uri_regex}" ({e})', - file=sys.stderr, - ) - return EX_USAGE + return _usage_error(f'ERROR: invalid --uri-regex "{uri_regex}" ({e})') uri_ignore_words = set( itertools.chain(*parse_ignore_words_option(options.uri_ignore_words_list)) @@ -1155,20 +1145,10 @@ def main(*args: str) -> int: ) break else: - parser.print_usage() - print( - f"ERROR: Unknown builtin dictionary: {u}", - file=sys.stderr, - ) - return EX_USAGE + return _usage_error(f"ERROR: Unknown builtin dictionary: {u}") else: if not os.path.isfile(dictionary): - parser.print_usage() - print( - f"ERROR: cannot find dictionary file: {dictionary}", - file=sys.stderr, - ) - return EX_USAGE + return _usage_error(f"ERROR: cannot find dictionary file: {dictionary}") use_dictionaries.append(dictionary) misspellings: Dict[str, Misspelling] = {} for dictionary in use_dictionaries: @@ -1182,13 +1162,10 @@ def main(*args: str) -> int: context = None if options.context is not None: if (options.before_context is not None) or (options.after_context is not None): - parser.print_usage() - print( + return _usage_error( "ERROR: --context/-C cannot be used together with " - "--context-before/-B or --context-after/-A", - file=sys.stderr, + "--context-before/-B or --context-after/-A" ) - return EX_USAGE context_both = max(0, options.context) context = (context_both, context_both) elif (options.before_context is not None) or (options.after_context is not None): @@ -1214,13 +1191,10 @@ def main(*args: str) -> int: try: glob_match.match("/random/path") # does not need a real path except re.error: - parser.print_usage() - print( + return _usage_error( "ERROR: --skip/-S has been fed an invalid glob, " - "try escaping special characters", - file=sys.stderr, + "try escaping special characters" ) - return EX_USAGE bad_count = 0 for filename in sorted(options.files):