Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
43 changes: 29 additions & 14 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
EX_DATAERR,
EX_OK,
EX_USAGE,
_builtin_dictionaries,
uri_regex_def,
)

Expand Down Expand Up @@ -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"
Expand Down