Ask a yes/no question via input() and get answer from the user. Inspired by the following implementation: https://code.activestate.com/recipes/577058/ Parameters ---------- question : str The question presented to the user. default : str, default "yes"
(question, default="yes")
| 1755 | |
| 1756 | |
| 1757 | def query_yes_no(question, default="yes"): |
| 1758 | """ |
| 1759 | Ask a yes/no question via input() and get answer from the user. |
| 1760 | |
| 1761 | Inspired by the following implementation: |
| 1762 | |
| 1763 | https://code.activestate.com/recipes/577058/ |
| 1764 | |
| 1765 | Parameters |
| 1766 | ---------- |
| 1767 | question : str |
| 1768 | The question presented to the user. |
| 1769 | default : str, default "yes" |
| 1770 | The presumed answer if the user just hits <Enter>. It must be "yes", |
| 1771 | "no", or None (means an answer is required of the user). |
| 1772 | |
| 1773 | Returns |
| 1774 | ------- |
| 1775 | yes : Whether or not the user replied yes to the question. |
| 1776 | """ |
| 1777 | |
| 1778 | valid = {"yes": "yes", "y": "yes", "ye": "yes", "no": "no", "n": "no"} |
| 1779 | prompt = {None: " [y/n] ", "yes": " [Y/n] ", "no": " [y/N] "}.get(default, None) |
| 1780 | |
| 1781 | if not prompt: |
| 1782 | raise ValueError("invalid default answer: '%s'" % default) |
| 1783 | |
| 1784 | reply = None |
| 1785 | |
| 1786 | while not reply: |
| 1787 | sys.stdout.write(colorize(question, Colors.PROMPT) + prompt) |
| 1788 | |
| 1789 | choice = input().lower() |
| 1790 | reply = None |
| 1791 | |
| 1792 | if default and not choice: |
| 1793 | reply = default |
| 1794 | elif choice in valid: |
| 1795 | reply = valid[choice] |
| 1796 | else: |
| 1797 | print_failure("Please respond with 'yes' or 'no' (or 'y' or 'n').\n") |
| 1798 | |
| 1799 | return reply == "yes" |
| 1800 | |
| 1801 | |
| 1802 | def is_valid_user_provided_domain_format(domain): |