Skip to content

Commit

Permalink
Support optional leading : in factor arg values.
Browse files Browse the repository at this point in the history
This aids reading configs and command lines.
  • Loading branch information
jsirois committed Jan 17, 2025
1 parent 10b474c commit 1873ef2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes

## 0.13.0

Support an optional leading `:` in factor argument values to parallel factor parameter default
syntax and better visually separate factor values from factor names.

## 0.12.0

This change adds support for command parametrization via environment variables, Python interpreter
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ as `test-py3.12`, the `-py3.12` factor will be defined. The value of `3.12` coul
the `{-py}` factor parameter placeholder in the command arguments or env values. The factor name
prefix will be stripped from the factor argument to produce the substituted value. As a consequence,
you want to ensure the factor names you use are non-overlapping or else an error will be raised due
to ambiguity in which factor argument should be applied.
to ambiguity in which factor argument should be applied. An optional leading `:` can proceed the
factor argument value, and it will be stripped. So both `test-py:3.12` and `test-py3.12` pass `3.12`
as the value for the `-py` factor parameter. The colon-prefix helps distinguish factor name from
factor value, paralleling the default value syntax that can be used at factor parameter declaration
sites.

### Tasks

Expand Down
2 changes: 1 addition & 1 deletion dev_cmd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2024 John Sirois.
# Licensed under the Apache License, Version 2.0 (see LICENSE).

__version__ = "0.12.0"
__version__ = "0.13.0"
2 changes: 2 additions & 0 deletions dev_cmd/placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def substitution(self, text: str, section: slice, state: State) -> None:
)
if matching_factors:
value = matching_factors[0][len(factor_name) :]
if value.startswith(":"):
value = value[1:]
state.used_factors.append(matching_factors[0])
else:
value = default
Expand Down
8 changes: 4 additions & 4 deletions tests/test_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def test_substitute_markers(env) -> None:


def test_substitute_factors(env: Environment) -> None:
factors = Factor("a1"), Factor("b2")
factors = Factor("a1"), Factor("b:2")
assert ("1", (Factor("a1"),)) == env.substitute("{-a}", *factors)
assert ("2", (Factor("b2"),)) == env.substitute("{-b}", *factors)
assert ("12", (Factor("a1"), Factor("b2"))) == env.substitute("{-a}{-b}", *factors)
assert ("123", (Factor("a1"), Factor("b2"))) == env.substitute("{-a}{-b}{-c:3}", *factors)
assert ("2", (Factor("b:2"),)) == env.substitute("{-b}", *factors)
assert ("12", (Factor("a1"), Factor("b:2"))) == env.substitute("{-a}{-b}", *factors)
assert ("123", (Factor("a1"), Factor("b:2"))) == env.substitute("{-a}{-b}{-c:3}", *factors)

with pytest.raises(ValueError, match=re.escape("The factor parameter '-c' is not set.")):
env.substitute("{-c}", *factors)
Expand Down

0 comments on commit 1873ef2

Please sign in to comment.