Skip to content

Commit

Permalink
refactor: improve user input handling in get_answer_yes_or_no function
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed Nov 5, 2024
1 parent 98b500c commit 0c76701
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/zcmds/cmds/common/codeup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ def check_environment() -> Path:
def get_answer_yes_or_no(question: str, default: Optional[str] = None) -> bool:
"""Ask a yes/no question and return the answer."""
while True:
answer = input(question + " [y/n]: ").lower()
if answer in ["y", "yes"]:
answer = input(question + " [y/n]: ").lower().strip()
print(f"answer was {answer}")
if "y" in answer:
print("returning True")
return True
if answer in ["n", "no"]:
if "n" in answer:
print("returning False")
return False
if answer == "" and default is not None:
print(f"returning default {default}")
return default == "y"
print("Please answer 'yes' or 'no'.")

Expand All @@ -92,14 +96,12 @@ def main() -> int:
has_untracked = len(repo.untracked_files) > 0
if has_untracked:
print("There are untracked files.")
answer_yes = get_answer_yes_or_no("Continue? [y/N] ", "y")
if answer_yes:
answer_yes = get_answer_yes_or_no("Continue?", "y")
if not answer_yes:
print("Aborting.")
return 1
for untracked_file in repo.untracked_files:
answer_yes = get_answer_yes_or_no(
f" Add {untracked_file}? [y/N] ", "y"
)
answer_yes = get_answer_yes_or_no(f" Add {untracked_file}?", "y")
if answer_yes:
_exec(f"git add {untracked_file}")
else:
Expand Down

0 comments on commit 0c76701

Please sign in to comment.