diff --git a/README.rst b/README.rst index a9562e8f2b..c707d302df 100644 --- a/README.rst +++ b/README.rst @@ -114,7 +114,7 @@ Want to know if a word you're proposing exists in codespell already? It is possi echo "word" | codespell - echo "1stword,2ndword" | codespell - -You can select the optional dictionaries with the ``--builtin`` option. +You can select the optional builtin dictionary with the ``--builtin`` option. Use ``--builtin=all`` to enable them all. This only works without custom ``-D`` dictionaries. Ignoring words -------------- diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index c5b5b4e21b..f5418689fd 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -459,6 +459,7 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]: metavar="BUILTIN-LIST", help="comma-separated list of builtin dictionaries " 'to include (when "-D -" or no "-D" is passed). ' + "Use 'all' to include every builtin dictionary. " "Current options are:" + builtin_opts + "\n" "The default is %(default)r.", ) @@ -1244,6 +1245,27 @@ def _usage_error(parser: argparse.ArgumentParser, message: str) -> int: return EX_USAGE +def _select_builtin_dictionary(builtin_option: str) -> list[str]: + use = sorted(set(builtin_option.split(","))) + if "all" in use: + use = [u for u in use if u != "all"] + [ + builtin[0] for builtin in _builtin_dictionaries + ] + use = sorted(set(use)) + + use_dictionaries = [] + for u in use: + for builtin in _builtin_dictionaries: + if builtin[0] == u: + use_dictionaries.append( + os.path.join(_data_root, f"dictionary{builtin[2]}.txt") + ) + break + else: + raise KeyError(u) + return use_dictionaries + + def main(*args: str) -> int: """Contains flow control""" try: @@ -1337,20 +1359,13 @@ def main(*args: str) -> int: use_dictionaries = [] for dictionary in dictionaries: if dictionary == "-": - # figure out which builtin dictionaries to use - use = sorted(set(options.builtin.split(","))) - for u in use: - for builtin in _builtin_dictionaries: - if builtin[0] == u: - use_dictionaries.append( - os.path.join(_data_root, f"dictionary{builtin[2]}.txt") - ) - break - else: - return _usage_error( - parser, - f"ERROR: Unknown builtin dictionary: {u}", - ) + try: + use_dictionaries.extend(_select_builtin_dictionary(options.builtin)) + except KeyError as e: + return _usage_error( + parser, + f"ERROR: Unknown builtin dictionary: {e.args[0]}", + ) else: if not os.path.isfile(dictionary): return _usage_error( diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 5120e1e8a1..8887df0d99 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -20,6 +20,7 @@ EX_DATAERR, EX_OK, EX_USAGE, + _builtin_dictionaries, uri_regex_def, ) @@ -116,6 +117,11 @@ def test_basic( f.write("tim\ngonna\n") assert cs.main(fname) == 2, "with a name" assert cs.main("--builtin", "clear,rare,names,informal", fname) == 4 + assert cs.main("--builtin", "all", fname) == cs.main( + "--builtin", + ",".join(d[0] for d in _builtin_dictionaries), + fname, + ) with fname.open("w") as f: # overwrite the file f.write("var = 'nwe must check codespell likes escapes nin strings'\n") assert cs.main(fname) == 1, "checking our string escape test word is bad"