# HG changeset patch # User Gilles Duboscq # Date 1378114824 -7200 # Node ID 3110bea9a6b01e2ef1db167def2b6f035d7cebae # Parent 231958c9ddf9f46e908c099fef37dc73451e6869 mx: add helper method for yes/no question diff -r 231958c9ddf9 -r 3110bea9a6b0 mx/commands.py --- a/mx/commands.py Mon Sep 02 20:44:49 2013 +0200 +++ b/mx/commands.py Mon Sep 02 11:40:24 2013 +0200 @@ -95,8 +95,7 @@ items = [k for k in _vmChoices.keys() if _vmChoices[k] is not None] descriptions = [_vmChoices[k] for k in _vmChoices.keys() if _vmChoices[k] is not None] vm = mx.select_items(items, descriptions, allowMultiple=False) - answer = raw_input('Persist this choice by adding "DEFAULT_VM=' + vm + '" to ' + envPath + '? [Yn]: ') - if not answer.lower().startswith('n'): + if mx.ask_yes_no('Persist this choice by adding "DEFAULT_VM=' + vm + '" to ' + envPath, 'y'): with open(envPath, 'a') as fp: print >> fp, 'DEFAULT_VM=' + vm _vm = vm @@ -281,8 +280,7 @@ def _handle_missing_VM(bld, vm): mx.log('The ' + bld + ' ' + vm + ' VM has not been created') if sys.stdout.isatty(): - answer = raw_input('Build it now? [Yn]: ') - if not answer.lower().startswith('n'): + if mx.ask_yes_no('Build it now', 'y'): with VM(vm, bld): build([]) return diff -r 231958c9ddf9 -r 3110bea9a6b0 mxtool/mx.py --- a/mxtool/mx.py Mon Sep 02 20:44:49 2013 +0200 +++ b/mxtool/mx.py Mon Sep 02 11:40:24 2013 +0200 @@ -919,8 +919,7 @@ break envPath = join(_mainSuite.dir, 'mx', 'env') - answer = raw_input('Persist this setting by adding "JAVA_HOME=' + javaHome + '" to ' + envPath + '? [Yn]: ') - if not answer.lower().startswith('n'): + if ask_yes_no('Persist this setting by adding "JAVA_HOME=' + javaHome + '" to ' + envPath, 'y'): with open(envPath, 'a') as fp: print >> fp, 'JAVA_HOME=' + javaHome @@ -2928,7 +2927,7 @@ projectConfigFiles = frozenset(['.classpath', 'nbproject']) indicators = projectConfigFiles.intersection(files) if len(indicators) != 0: - if not sys.stdout.isatty() or raw_input(currentDir + ' looks like a removed project -- delete it? [yn]: ') == 'y': + if not sys.stdout.isatty() or ask_yes_no(currentDir + ' looks like a removed project -- delete it', 'n'): shutil.rmtree(currentDir) log('Deleted ' + currentDir) @@ -3409,6 +3408,22 @@ log(projectsFile) for p in s.projects: log('\t' + p.name) + +def ask_yes_no(question, default=None): + """""" + assert not default or default == 'y' or default == 'n' + if not sys.stdout.isatty(): + if default: + return default + else: + abort("Can not answer '" + question + "?' if stdout is not a tty") + questionMark = '? [yn]: ' + if default: + questionMark = questionMark.replace(default, default.upper()) + answer = raw_input(question + questionMark) or default + while not answer: + answer = raw_input(question + questionMark) + return answer.lower().startswith('y') def add_argument(*args, **kwargs): """