Skip to content

Commit

Permalink
cmd_runner: allow bool format to pass alternate (false) value (#5647)
Browse files Browse the repository at this point in the history
* allow bool format to pass alternate (false) value

* add changelog fragment
  • Loading branch information
russoz authored Dec 4, 2022
1 parent 23aacc7 commit be22ca0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5647-cmd-runner-as-bool-false.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- cmd_runner module utils - ``cmd_runner_fmt.as_bool()`` can now take an extra parameter to format when value is false (https://github.com/ansible-collections/community.general/pull/5647).
9 changes: 7 additions & 2 deletions plugins/module_utils/cmd_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ def __call__(self, value, ctx_ignore_none):

class _Format(object):
@staticmethod
def as_bool(args):
return _ArgFormat(lambda value: _ensure_list(args) if value else [])
def as_bool(args_true, args_false=None, ignore_none=None):
if args_false is not None:
if ignore_none is None:
ignore_none = False
else:
args_false = []
return _ArgFormat(lambda value: _ensure_list(args_true) if value else _ensure_list(args_false), ignore_none=ignore_none)

@staticmethod
def as_bool_not(args):
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/plugins/module_utils/test_cmd_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
simple_boolean__true=(fmt.as_bool, ("--superflag",), True, ["--superflag"]),
simple_boolean__false=(fmt.as_bool, ("--superflag",), False, []),
simple_boolean__none=(fmt.as_bool, ("--superflag",), None, []),
simple_boolean_both__true=(fmt.as_bool, ("--superflag", "--falseflag"), True, ["--superflag"]),
simple_boolean_both__false=(fmt.as_bool, ("--superflag", "--falseflag"), False, ["--falseflag"]),
simple_boolean_both__none=(fmt.as_bool, ("--superflag", "--falseflag"), None, ["--falseflag"]),
simple_boolean_both__none_ig=(fmt.as_bool, ("--superflag", "--falseflag", True), None, []),
simple_boolean_not__true=(fmt.as_bool_not, ("--superflag",), True, []),
simple_boolean_not__false=(fmt.as_bool_not, ("--superflag",), False, ["--superflag"]),
simple_boolean_not__none=(fmt.as_bool_not, ("--superflag",), None, ["--superflag"]),
Expand Down

0 comments on commit be22ca0

Please sign in to comment.