forked from Mirror/pmbootstrap
Fix #242: Validate input in pmb.helpers.cli
* Validate all inputs from `pmbootstrap init` * Add a new `confirm()` function, that validates input of yes/no questions properly * All questions loop until they have a valid answer now
This commit is contained in:
parent
21228a14d1
commit
a49187c6e4
5 changed files with 91 additions and 42 deletions
|
@ -17,23 +17,51 @@ You should have received a copy of the GNU General Public License
|
|||
along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import datetime
|
||||
import logging
|
||||
import re
|
||||
|
||||
|
||||
def ask(args, question="Continue?", choices=['y', 'n'], default='n',
|
||||
lowercase_answer=True):
|
||||
date = datetime.datetime.now().strftime("%H:%M:%S")
|
||||
question = "[" + date + "] " + question
|
||||
if choices:
|
||||
question += " (" + str.join("/", choices) + ")"
|
||||
if default:
|
||||
question += " [" + str(default) + "]"
|
||||
def ask(args, question="Continue?", choices=["y", "n"], default="n",
|
||||
lowercase_answer=True, validation_regex=None):
|
||||
"""
|
||||
Ask a question on the terminal. When validation_regex is set, the user gets
|
||||
asked until the answer matches the regex.
|
||||
:returns: the user's answer
|
||||
"""
|
||||
while True:
|
||||
date = datetime.datetime.now().strftime("%H:%M:%S")
|
||||
question_full = "[" + date + "] " + question
|
||||
if choices:
|
||||
question_full += " (" + str.join("/", choices) + ")"
|
||||
if default:
|
||||
question_full += " [" + str(default) + "]"
|
||||
|
||||
ret = input(question + ": ")
|
||||
if lowercase_answer:
|
||||
ret = ret.lower()
|
||||
if ret == "":
|
||||
ret = str(default)
|
||||
ret = input(question_full + ": ")
|
||||
if lowercase_answer:
|
||||
ret = ret.lower()
|
||||
if ret == "":
|
||||
ret = str(default)
|
||||
|
||||
args.logfd.write(question + " " + ret + "\n")
|
||||
args.logfd.flush()
|
||||
return ret
|
||||
args.logfd.write(question_full + " " + ret + "\n")
|
||||
args.logfd.flush()
|
||||
|
||||
# Validate with regex
|
||||
if not validation_regex:
|
||||
return ret
|
||||
|
||||
pattern = re.compile(validation_regex)
|
||||
if pattern.match(ret):
|
||||
return ret
|
||||
|
||||
logging.fatal("ERROR: Input did not pass validation (regex: " +
|
||||
validation_regex + "). Please try again.")
|
||||
|
||||
|
||||
def confirm(args, question="Continue?", default=False):
|
||||
"""
|
||||
Convenience wrapper around ask for simple yes-no questions with validation.
|
||||
:returns: True for "y", False for "n"
|
||||
"""
|
||||
default_str = "y" if default else "n"
|
||||
answer = ask(args, question, ["y", "n"], default_str, True, "(y|n)")
|
||||
return answer == "y"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue