Skip to content

Commit f6b4803

Browse files
authored
Rename --write option to --fix (#3748)
1 parent 66378f6 commit f6b4803

File tree

6 files changed

+39
-32
lines changed

6 files changed

+39
-32
lines changed

.ansible-lint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ warn_list:
7676
# - yaml[document-start] # you can also use sub-rule matches
7777

7878
# Some rules can transform files to fix (or make it easier to fix) identified
79-
# errors. `ansible-lint --write` will reformat YAML files and run these transforms.
79+
# errors. `ansible-lint --fix` will reformat YAML files and run these transforms.
8080
# By default it will run all transforms (effectively `write_list: ["all"]`).
8181
# You can disable running transforms by setting `write_list: ["none"]`.
8282
# Or only enable a subset of rule transforms by listing rules/tags here.

src/ansiblelint/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def main(argv: list[str] | None = None) -> int:
274274

275275
if Version(ruamel_safe_version) > Version(ruamel_yaml_version_str):
276276
_logger.warning(
277-
"We detected use of `--write` feature with a buggy ruamel-yaml %s library instead of >=%s, upgrade it before reporting any bugs like dropped comments.",
277+
"We detected use of `--fix` feature with a buggy ruamel-yaml %s library instead of >=%s, upgrade it before reporting any bugs like dropped comments.",
278278
ruamel_yaml_version_str,
279279
ruamel_safe_version,
280280
)

src/ansiblelint/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def report_outcome(
223223

224224
if self.options.write_list and "yaml" in self.options.skip_list:
225225
_logger.warning(
226-
"You specified '--write', but no files can be modified "
226+
"You specified '--fix', but no files can be modified "
227227
"because 'yaml' is in 'skip_list'.",
228228
)
229229

src/ansiblelint/cli.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def __call__(
145145

146146

147147
class WriteArgAction(argparse.Action):
148-
"""Argparse action to handle the --write flag with optional args."""
148+
"""Argparse action to handle the --fix flag with optional args."""
149149

150150
_default = "__default__"
151151

@@ -174,8 +174,8 @@ def __init__( # pylint: disable=too-many-arguments,redefined-builtin
174174
super().__init__(
175175
option_strings=option_strings,
176176
dest=dest,
177-
nargs="?", # either 0 (--write) or 1 (--write=a,b,c) argument
178-
const=self._default, # --write (no option) implicitly stores this
177+
nargs="?", # either 0 (--fix) or 1 (--fix=a,b,c) argument
178+
const=self._default, # --fix (no option) implicitly stores this
179179
default=default,
180180
type=type,
181181
choices=choices,
@@ -194,8 +194,8 @@ def __call__(
194194
lintables = getattr(namespace, "lintables", None)
195195
if not lintables and isinstance(values, str):
196196
# args are processed in order.
197-
# If --write is after lintables, then that is not ambiguous.
198-
# But if --write comes first, then it might actually be a lintable.
197+
# If --fix is after lintables, then that is not ambiguous.
198+
# But if --fix comes first, then it might actually be a lintable.
199199
maybe_lintable = Path(values)
200200
if maybe_lintable.exists():
201201
namespace.lintables = [values]
@@ -211,19 +211,19 @@ def __call__(
211211
setattr(namespace, self.dest, values)
212212

213213
@classmethod
214-
def merge_write_list_config(
214+
def merge_fix_list_config(
215215
cls,
216216
from_file: list[str],
217217
from_cli: list[str],
218218
) -> list[str]:
219-
"""Combine the write_list from file config with --write CLI arg.
219+
"""Combine the write_list from file config with --fix CLI arg.
220220
221221
Handles the implicit "all" when "__default__" is present and file config is empty.
222222
"""
223223
if not from_file or "none" in from_cli:
224-
# --write is the same as --write=all
224+
# --fix is the same as --fix=all
225225
return ["all" if value == cls._default else value for value in from_cli]
226-
# --write means use the config from the config file
226+
# --fix means use the config from the config file
227227
from_cli = [value for value in from_cli if value != cls._default]
228228
return from_file + from_cli
229229

@@ -338,7 +338,7 @@ def get_cli_parser() -> argparse.ArgumentParser:
338338
help="Return non-zero exit code on warnings as well as errors",
339339
)
340340
parser.add_argument(
341-
"--write",
341+
"--fix",
342342
dest="write_list",
343343
# this is a tri-state argument that takes an optional comma separated list:
344344
action=WriteArgAction,
@@ -347,12 +347,12 @@ def get_cli_parser() -> argparse.ArgumentParser:
347347
"A rule transform can fix or simplify fixing issues identified by that rule). "
348348
"You can limit the effective rule transforms (the 'write_list') by passing a "
349349
"keywords 'all' or 'none' or a comma separated list of rule ids or rule tags. "
350-
"YAML reformatting happens whenever '--write' or '--write=' is used. "
351-
"'--write' and '--write=all' are equivalent: they allow all transforms to run. "
350+
"YAML reformatting happens whenever '--fix' or '--fix=' is used. "
351+
"'--fix' and '--fix=all' are equivalent: they allow all transforms to run. "
352352
"The effective list of transforms comes from 'write_list' in the config file, "
353-
"followed whatever '--write' args are provided on the commandline. "
354-
"'--write=none' resets the list of transforms to allow reformatting YAML "
355-
"without running any of the transforms (ie '--write=none,rule-id' will "
353+
"followed whatever '--fix' args are provided on the commandline. "
354+
"'--fix=none' resets the list of transforms to allow reformatting YAML "
355+
"without running any of the transforms (ie '--fix=none,rule-id' will "
356356
"ignore write_list in the config file and only run the rule-id transform).",
357357
)
358358
parser.add_argument(
@@ -533,7 +533,7 @@ def merge_config(file_config: dict[Any, Any], cli_config: Options) -> Options:
533533
setattr(
534534
cli_config,
535535
entry,
536-
WriteArgAction.merge_write_list_config(
536+
WriteArgAction.merge_fix_list_config(
537537
from_file=file_config.pop(entry, []),
538538
from_cli=getattr(cli_config, entry, []) or [],
539539
),
@@ -557,6 +557,13 @@ def merge_config(file_config: dict[Any, Any], cli_config: Options) -> Options:
557557
def get_config(arguments: list[str]) -> Options:
558558
"""Extract the config based on given args."""
559559
parser = get_cli_parser()
560+
# translate deprecated options
561+
for i, value in enumerate(arguments):
562+
if arguments[i].startswith("--write"):
563+
arguments[i] = value.replace("--write", "--fix")
564+
_logger.warning(
565+
"Replaced deprecated '--write' option with '--fix', change you call to avoid future regressions when we remove old option.",
566+
)
560567
options = Options(**vars(parser.parse_args(arguments)))
561568

562569
# docs is not document, being used for internal documentation building

src/ansiblelint/rules/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
254254
class TransformMixin:
255255
"""A mixin for AnsibleLintRule to enable transforming files.
256256
257-
If ansible-lint is started with the ``--write`` option, then the ``Transformer``
257+
If ansible-lint is started with the ``--fix`` option, then the ``Transformer``
258258
will call the ``transform()`` method for every MatchError identified if the rule
259259
that identified it subclasses this ``TransformMixin``. Only the rule that identified
260260
a MatchError can do transforms to fix that match.

test/test_cli.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,34 +68,34 @@ def test_ensure_config_are_equal(
6868
@pytest.mark.parametrize(
6969
("with_base", "args", "config"),
7070
(
71-
(True, ["--write"], "test/fixtures/config-with-write-all.yml"),
72-
(True, ["--write=all"], "test/fixtures/config-with-write-all.yml"),
73-
(True, ["--write", "all"], "test/fixtures/config-with-write-all.yml"),
74-
(True, ["--write=none"], "test/fixtures/config-with-write-none.yml"),
75-
(True, ["--write", "none"], "test/fixtures/config-with-write-none.yml"),
71+
(True, ["--fix"], "test/fixtures/config-with-write-all.yml"),
72+
(True, ["--fix=all"], "test/fixtures/config-with-write-all.yml"),
73+
(True, ["--fix", "all"], "test/fixtures/config-with-write-all.yml"),
74+
(True, ["--fix=none"], "test/fixtures/config-with-write-none.yml"),
75+
(True, ["--fix", "none"], "test/fixtures/config-with-write-none.yml"),
7676
(
7777
True,
78-
["--write=rule-tag,rule-id"],
78+
["--fix=rule-tag,rule-id"],
7979
"test/fixtures/config-with-write-subset.yml",
8080
),
8181
(
8282
True,
83-
["--write", "rule-tag,rule-id"],
83+
["--fix", "rule-tag,rule-id"],
8484
"test/fixtures/config-with-write-subset.yml",
8585
),
8686
(
8787
True,
88-
["--write", "rule-tag", "--write", "rule-id"],
88+
["--fix", "rule-tag", "--fix", "rule-id"],
8989
"test/fixtures/config-with-write-subset.yml",
9090
),
9191
(
9292
False,
93-
["--write", "examples/playbooks/example.yml"],
93+
["--fix", "examples/playbooks/example.yml"],
9494
"test/fixtures/config-with-write-all.yml",
9595
),
9696
(
9797
False,
98-
["--write", "examples/playbooks/example.yml", "non-existent.yml"],
98+
["--fix", "examples/playbooks/example.yml", "non-existent.yml"],
9999
"test/fixtures/config-with-write-all.yml",
100100
),
101101
),
@@ -106,7 +106,7 @@ def test_ensure_write_cli_does_not_consume_lintables(
106106
args: list[str],
107107
config: str,
108108
) -> None:
109-
"""Check equality of the CLI --write options to config files."""
109+
"""Check equality of the CLI --fix options to config files."""
110110
cli_parser = cli.get_cli_parser()
111111

112112
command = base_arguments + args if with_base else args
@@ -115,7 +115,7 @@ def test_ensure_write_cli_does_not_consume_lintables(
115115

116116
file_value = file_config.get("write_list")
117117
orig_cli_value = options.write_list
118-
cli_value = cli.WriteArgAction.merge_write_list_config(
118+
cli_value = cli.WriteArgAction.merge_fix_list_config(
119119
from_file=[],
120120
from_cli=orig_cli_value,
121121
)

0 commit comments

Comments
 (0)