-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: interactive cli input (#947)
Co-authored-by: rahul <raxhvl@users.noreply.github.com> Co-authored-by: danceratopz <danceratopz@gmail.com>
- Loading branch information
1 parent
61aad9e
commit e69aae5
Showing
6 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
A standard interface for interactive CLI inputs. | ||
""" | ||
|
||
from .questionary_input_repository import QuestionaryInputRepository | ||
|
||
# Instantiate the input repository | ||
input_repository = QuestionaryInputRepository() | ||
|
||
|
||
def input_text(question: str) -> str: | ||
""" | ||
Ask a simple text input question. | ||
Args: | ||
question (str): The question to ask. | ||
Returns: | ||
str: The user's response. | ||
""" | ||
return input_repository.input_text(question) | ||
|
||
|
||
def input_password(question: str) -> str: | ||
""" | ||
Ask a password input question (hidden text). | ||
Args: | ||
question (str): The question to ask. | ||
Returns: | ||
str: The user's response (password). | ||
""" | ||
return input_repository.input_password(question) | ||
|
||
|
||
def input_select(question: str, choices: list) -> str: | ||
""" | ||
Ask a single-choice question from a list of options. | ||
Args: | ||
question (str): The question to ask. | ||
choices (list): A list of options for the user to choose from. | ||
Returns: | ||
str: The selected choice. | ||
""" | ||
return input_repository.input_select(question, choices) | ||
|
||
|
||
def input_checkbox(question: str, choices: list) -> list: | ||
""" | ||
Ask a multi-choice question and return a list of selected choices. | ||
Args: | ||
question (str): The question to ask. | ||
choices (list): A list of options for the user to choose from. | ||
Returns: | ||
list: The list of selected choices. | ||
""" | ||
return input_repository.input_checkbox(question, choices) | ||
|
||
|
||
def input_confirm(question: str) -> bool: | ||
""" | ||
Ask a yes/no confirmation question. | ||
Args: | ||
question (str): The question to ask. | ||
Returns: | ||
bool: True for 'yes', False for 'no'. | ||
""" | ||
return input_repository.input_confirm(question) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
An abstract base class for handling interactive CLI inputs. | ||
""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import List | ||
|
||
|
||
class InputRepository(ABC): | ||
""" | ||
Abstract base class for input handling. | ||
This class defines the interface for different input types that can be swapped out. | ||
""" | ||
|
||
@abstractmethod | ||
def input_text(self, question: str) -> str: | ||
"""Ask a text input question.""" | ||
pass | ||
|
||
@abstractmethod | ||
def input_password(self, question: str) -> str: | ||
"""Ask a password input question (hidden).""" | ||
pass | ||
|
||
@abstractmethod | ||
def input_select(self, question: str, choices: List[str]) -> str: | ||
"""Ask a single-choice selection question.""" | ||
pass | ||
|
||
@abstractmethod | ||
def input_checkbox(self, question: str, choices: List[str]) -> List[str]: | ||
"""Ask a multi-choice question.""" | ||
pass | ||
|
||
@abstractmethod | ||
def input_confirm(self, question: str) -> bool: | ||
"""Ask a yes/no confirmation question.""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Interactive CLI inputs using questionary library. | ||
See: https://questionary.readthedocs.io/ | ||
""" | ||
|
||
from questionary import checkbox, confirm, password, select, text | ||
|
||
from .input_repository import InputRepository | ||
|
||
|
||
class QuestionaryInputRepository(InputRepository): | ||
"""Repository for handling various types of user inputs using the Questionary library.""" | ||
|
||
def input_text(self, question: str) -> str: | ||
"""Ask a text input question. | ||
See: https://questionary.readthedocs.io/en/stable/api.html#questionary.text | ||
""" | ||
return text(message=question).ask() | ||
|
||
def input_password(self, question: str) -> str: | ||
"""Ask a password input question (hidden). | ||
See: https://questionary.readthedocs.io/en/stable/api.html#questionary.password | ||
""" | ||
return password(message=question).ask() | ||
|
||
def input_select(self, question: str, choices: list) -> str: | ||
"""Ask a single-choice selection question. | ||
See: https://questionary.readthedocs.io/en/stable/api.html#questionary.select | ||
""" | ||
return select(message=question, choices=choices).ask() | ||
|
||
def input_checkbox(self, question: str, choices: list) -> list: | ||
"""Ask a multi-choice question. | ||
See: https://questionary.readthedocs.io/en/stable/api.html#questionary.checkbox | ||
""" | ||
return checkbox(message=question, choices=choices).ask() | ||
|
||
def input_confirm(self, question: str) -> bool: | ||
"""Ask a yes/no confirmation question. | ||
See: https://questionary.readthedocs.io/en/stable/api.html#questionary.confirm | ||
""" | ||
return confirm(message=question).ask() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,7 @@ Pytest | |
pytest's | ||
pytestArgs | ||
qGpsxSA | ||
questionary | ||
quickstart | ||
radd | ||
randao | ||
|