CLI prompt: don't default to yes for truly dangerous

This commit is contained in:
Doug Blank 2016-04-17 08:26:46 -04:00
parent bae1417021
commit 42a14b6aae
3 changed files with 26 additions and 11 deletions

View File

@ -288,7 +288,7 @@ class ArgHandler(object):
"WARNING: It will be overwritten:\n %s" "WARNING: It will be overwritten:\n %s"
) % fullpath ) % fullpath
accepted = self.user.prompt(_('OK to overwrite?'), message, accepted = self.user.prompt(_('OK to overwrite?'), message,
_('yes'), _('no')) _('yes'), _('no'), default_label=_('yes'))
if accepted: if accepted:
self.__error(_("Will overwrite the existing file: %s") self.__error(_("Will overwrite the existing file: %s")
% fullpath) % fullpath)

View File

@ -419,10 +419,10 @@ class CLIDbManager(object):
"No matching family tree found: '%s'" % dbname) "No matching family tree found: '%s'" % dbname)
# now delete them: # now delete them:
for (name, directory) in match_list: for (name, directory) in match_list:
if user is None or not user.prompt( if user is None or user.prompt(
_('Remove family tree warning'), _('Remove family tree warning'),
_('Are you sure you want to remove the family tree named\n"%s"?' % name), _('Are you sure you want to remove the family tree named\n"%s"?' % name),
_('no'), _('yes')): _('yes'), _('no'), default_label=_('no')):
try: try:
for (top, dirs, files) in os.walk(directory): for (top, dirs, files) in os.walk(directory):
for filename in files: for filename in files:

View File

@ -60,7 +60,7 @@ class User(user.User):
self.current_step = 0; self.current_step = 0;
self._input = input self._input = input
def yes(*args): def yes(*args, **kwargs):
return True return True
if auto_accept: if auto_accept:
@ -109,7 +109,8 @@ class User(user.User):
""" """
self._fileout.write("\r100%\n") self._fileout.write("\r100%\n")
def prompt(self, title, message, accept_label, reject_label, parent=None): def prompt(self, title, message, accept_label, reject_label,
parent=None, default_label=None):
""" """
Prompt the user with a message to select an alternative. Prompt the user with a message to select an alternative.
@ -124,21 +125,35 @@ class User(user.User):
:type accept_label: str :type accept_label: str
:param reject_label: what to call the negative choice, e.g.: "Stop" :param reject_label: what to call the negative choice, e.g.: "Stop"
:type reject_label: str :type reject_label: str
:param default_label: the label of the default
:type default_label: str or None
:returns: the user's answer to the question :returns: the user's answer to the question
:rtype: bool :rtype: bool
""" """
accept_label = accept_label.replace("_", "") accept_text = accept_label.replace("_", "")
reject_label = reject_label.replace("_", "") reject_text = reject_label.replace("_", "")
text = "{t}\n{m} ([{y}]/{n}): ".format( if default_label is None or default_label == accept_label:
accept_text = "[%s]" % accept_text
default = True
else:
reject_text = "[%s]" % reject_text
default = False
text = "{t}\n{m} ({y}/{n}): ".format(
t = title, t = title,
m = message, m = message,
y = accept_label, y = accept_text,
n = reject_label) n = reject_text)
print (text, file = self._fileout) # TODO python3 add flush=True print (text, file = self._fileout) # TODO python3 add flush=True
try: try:
reply = self._input() reply = self._input()
return reply == "" or reply == accept_label
except EOFError: except EOFError:
reply = ""
### Trun response into True/False:
if reply == "":
return default
elif reply == accept_label:
return True
else:
return False return False
def warn(self, title, warning=""): def warn(self, title, warning=""):