#########################################################################
#
# Copyright (C) 2018 OSGeo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
[docs]
def confirm(prompt=None, resp=False):
"""
Prompts the user for a yes or no response. Returns True for ``yes`` and False for ``no``.
``resp`` should be set to the default value expected by the caller when the user presses ENTER without providing an input.
>>> confirm(prompt='Create Directory?', resp=True)
Create Directory? [y]|n:
True
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y:
False
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y: y
True
"""
if prompt is None:
prompt = 'Confirm'
if resp:
prompt = f'{prompt} [y]|n: '
else:
prompt = f'{prompt} [n]|y: '
while True:
ans = input(prompt)
if not ans:
return resp
if ans not in {'y', 'Y', 'n', 'N'}:
print('please enter y or n.')
continue
if ans == 'y' or ans == 'Y':
return True
if ans == 'n' or ans == 'N':
return False