From 99ea68219e7dc3545a4bd66f7341953d9cab53c6 Mon Sep 17 00:00:00 2001 From: AlightSoulmate <2314297572@qq.com> Date: Thu, 16 Apr 2026 15:10:58 +0800 Subject: [PATCH 1/3] Allow use --builtin=all to use every builtin dictionary --- README.rst | 4 +++- codespell_lib/_codespell.py | 6 ++++++ codespell_lib/tests/test_basic.py | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index a9562e8f2b..ef5f5ac6c7 100644 --- a/README.rst +++ b/README.rst @@ -114,7 +114,9 @@ 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..aa8cae170b 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.", ) @@ -1339,6 +1340,11 @@ def main(*args: str) -> int: if dictionary == "-": # figure out which builtin dictionaries to use use = sorted(set(options.builtin.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)) for u in use: for builtin in _builtin_dictionaries: if builtin[0] == u: diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 5120e1e8a1..215245e360 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -16,6 +16,7 @@ import codespell_lib as cs_ from codespell_lib._codespell import ( + _builtin_dictionaries, EX_CONFIG, EX_DATAERR, EX_OK, @@ -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" From 2578bfdf576393b4b6c0285c583511ebf41bc41c Mon Sep 17 00:00:00 2001 From: AlightSoulmate <2314297572@qq.com> Date: Thu, 16 Apr 2026 15:25:34 +0800 Subject: [PATCH 2/3] fix typo --- README.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.rst b/README.rst index ef5f5ac6c7..c707d302df 100644 --- a/README.rst +++ b/README.rst @@ -114,9 +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 builtin dictionary with the ``--builtin`` option. Use -``--builtin=all`` to enable them all. This only works without custom ``-D`` -dictionaries. +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 -------------- From 6668a624073b0508096c2d3840abb259326c5a02 Mon Sep 17 00:00:00 2001 From: AlightSoulmate <2314297572@qq.com> Date: Thu, 16 Apr 2026 15:45:16 +0800 Subject: [PATCH 3/3] resolve ruff lint errs --- codespell_lib/_codespell.py | 47 ++++++++++++++++++------------- codespell_lib/tests/test_basic.py | 2 +- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index aa8cae170b..f5418689fd 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -1245,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: @@ -1338,25 +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(","))) - if "all" in use: - use = [u for u in use if u != "all"] + [ - builtin[0] for builtin in _builtin_dictionaries - ] - use = sorted(set(use)) - 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 215245e360..8887df0d99 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -16,11 +16,11 @@ import codespell_lib as cs_ from codespell_lib._codespell import ( - _builtin_dictionaries, EX_CONFIG, EX_DATAERR, EX_OK, EX_USAGE, + _builtin_dictionaries, uri_regex_def, )