Skip to content

Commit 29598ac

Browse files
committed
Automatically process choices and add 'with_overwrite option
1 parent aaceef5 commit 29598ac

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

66
## [Unreleased]
7+
### Added
8+
- Automatically process choices with a `RUN_ALL` option into a list.
9+
- Add `with_overwrite` cli option.
710

811
## [1.0.12] - 2024-11-13
912
### Fixed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,6 @@ disallow_any_unimported = true
138138
module = [
139139
"deep_translator.*",
140140
"pathos.*",
141+
"mkdocs_gen_files.*",
141142
]
142143
ignore_missing_imports = true

scripts/gen_ref_pages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import mkdocs_gen_files
66

7-
nav = mkdocs_gen_files.Nav() # type: ignore[attr-defined, no-untyped-call]
7+
nav = mkdocs_gen_files.Nav()
88

99
root = Path(__file__).parent.parent
1010
src = root / "src"

src/rra_tools/cli_tools/options.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@
1313
RUN_ALL = "ALL"
1414

1515

16+
def convert_choice(value: str, choices: Sequence[str]) -> list[str]:
17+
"""Convert a choice to a list of choices, handling the special 'All' choice.
18+
19+
Parameters
20+
----------
21+
value
22+
The choice to convert.
23+
choices
24+
The set of choices to choose from.
25+
26+
Returns
27+
-------
28+
list[str]
29+
The list of choices.
30+
"""
31+
if value == RUN_ALL:
32+
return list(choices)
33+
return [value]
34+
35+
1636
def process_choices(
1737
allow_all: bool, # noqa: FBT001
1838
choices: Sequence[str] | None,
@@ -62,7 +82,7 @@ def process_choices(
6282
choices.append(RUN_ALL)
6383
default = RUN_ALL
6484
else:
65-
default = choices[-1]
85+
default = None
6686
option_type = click.Choice(choices)
6787
show_default = default is not None
6888
return option_type, default, show_default
@@ -74,6 +94,7 @@ def with_choice(
7494
*,
7595
allow_all: bool = True,
7696
choices: Sequence[str] | None = None,
97+
convert: bool = True,
7798
**kwargs: Any,
7899
) -> ClickOption[_P, _T]:
79100
"""Create an option with a set of choices.
@@ -88,8 +109,12 @@ def with_choice(
88109
Whether to allow the special value "ALL", which represents all choices.
89110
choices
90111
The set of choices to allow.
112+
convert
113+
Whether to convert the provided argument to a list, resolving the special
114+
value "ALL" to all choices.
91115
92116
"""
117+
93118
names = [f"--{name.replace('_', '-')}"]
94119
if short_name is not None:
95120
if len(short_name) != 1:
@@ -98,6 +123,23 @@ def with_choice(
98123
names.append(f"-{short_name}")
99124
option_type, default, show_default = process_choices(allow_all, choices)
100125

126+
if convert:
127+
if not allow_all:
128+
msg = "Conversion is only supported when allow_all is True."
129+
raise ValueError(msg)
130+
if choices is None:
131+
msg = "Conversion is only supported when choices are provided."
132+
raise ValueError(msg)
133+
134+
def convert_to_list_callback(
135+
ctx: click.Context, # noqa: ARG001
136+
param: click.Parameter, # noqa: ARG001
137+
value: str,
138+
) -> list[str]:
139+
return convert_choice(value, choices)
140+
141+
kwargs["callback"] = convert_to_list_callback
142+
101143
return click.option(
102144
*names,
103145
type=option_type,
@@ -182,3 +224,11 @@ def with_dry_run() -> ClickOption[_P, _T]:
182224
is_flag=True,
183225
help="Don't actually run the workflow.",
184226
)
227+
228+
229+
def with_overwrite() -> ClickOption[_P, _T]:
230+
return click.option(
231+
"--overwrite",
232+
is_flag=True,
233+
help="Overwrite existing files.",
234+
)

0 commit comments

Comments
 (0)