mirror of
https://github.com/caelestia-dots/cli.git
synced 2026-06-16 05:49:59 -05:00
feat: retry on invalid input instead of exiting
This commit is contained in:
@@ -130,29 +130,30 @@ class Command:
|
|||||||
for i, comp in enumerate(comp_arr):
|
for i, comp in enumerate(comp_arr):
|
||||||
print(format_msg(PROMPT_COLOUR, f" {i + 1:<{max_idx_w}}\t{comp}"))
|
print(format_msg(PROMPT_COLOUR, f" {i + 1:<{max_idx_w}}\t{comp}"))
|
||||||
print(format_msg(PROMPT_COLOUR, "[A]ll or (1 2 3, 1-3, ^4)"))
|
print(format_msg(PROMPT_COLOUR, "[A]ll or (1 2 3, 1-3, ^4)"))
|
||||||
ans = prompt("", end="").lower().strip()
|
|
||||||
|
|
||||||
def _valid_v(v: str) -> int:
|
def _valid_v(v: str) -> int:
|
||||||
try:
|
try:
|
||||||
i_v = int(v, base=10) - 1 # -1 to translate to 0 index
|
i_v = int(v, base=10) - 1 # -1 to translate to 0 index
|
||||||
except ValueError:
|
except ValueError:
|
||||||
fatal(f'Invalid input. Given value "{v}" must be an integer.')
|
raise ValueError(f'Given value "{v}" must be an integer.')
|
||||||
if i_v < 0 or i_v >= len(comp_arr):
|
if i_v < 0 or i_v >= len(comp_arr):
|
||||||
fatal(f'Invalid input. Given value "{v}" must be between 1 and {len(comp_arr)} inclusive.')
|
raise ValueError(f'Given value "{v}" must be between 1 and {len(comp_arr)} inclusive.')
|
||||||
return i_v
|
return i_v
|
||||||
|
|
||||||
|
def _parse(ans: str) -> list[str] | None:
|
||||||
if ans in ("a", "all"):
|
if ans in ("a", "all"):
|
||||||
manifest.resolve_components(enable=list(manifest.components))
|
return list(manifest.components)
|
||||||
elif ans:
|
if not ans:
|
||||||
|
return None
|
||||||
|
|
||||||
enabled: list[str] = []
|
enabled: list[str] = []
|
||||||
toks = ans.split()
|
for tok in ans.split():
|
||||||
for tok in toks:
|
|
||||||
fr, sep, to = tok.partition("-")
|
fr, sep, to = tok.partition("-")
|
||||||
if sep:
|
if sep:
|
||||||
fr = _valid_v(fr)
|
fr = _valid_v(fr)
|
||||||
to = _valid_v(to)
|
to = _valid_v(to)
|
||||||
if fr > to:
|
if fr > to:
|
||||||
fatal(f'Invalid input. Given range "{tok}" must be lo-hi.')
|
raise ValueError(f'Given range "{tok}" must be lo-hi.')
|
||||||
enabled += comp_arr[fr : to + 1]
|
enabled += comp_arr[fr : to + 1]
|
||||||
elif tok.startswith("^"):
|
elif tok.startswith("^"):
|
||||||
t = _valid_v(tok[1:])
|
t = _valid_v(tok[1:])
|
||||||
@@ -160,7 +161,19 @@ class Command:
|
|||||||
else:
|
else:
|
||||||
t = _valid_v(tok)
|
t = _valid_v(tok)
|
||||||
enabled.append(comp_arr[t])
|
enabled.append(comp_arr[t])
|
||||||
manifest.resolve_components(enable=list(set(enabled)))
|
return list(set(enabled))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
ans = prompt("", end="").lower().strip()
|
||||||
|
try:
|
||||||
|
enabled = _parse(ans)
|
||||||
|
except ValueError as e:
|
||||||
|
warn(f"invalid input. {e} Please try again.")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if enabled is not None:
|
||||||
|
manifest.resolve_components(enable=enabled)
|
||||||
|
return
|
||||||
|
|
||||||
def deploy_configs(self, source: DotsSource, manifest: Manifest) -> None:
|
def deploy_configs(self, source: DotsSource, manifest: Manifest) -> None:
|
||||||
print()
|
print()
|
||||||
|
|||||||
Reference in New Issue
Block a user