Mercurial > hg > graal-compiler
diff mxtool/mx.py @ 11511:3110bea9a6b0
mx: add helper method for yes/no question
author | Gilles Duboscq <duboscq@ssw.jku.at> |
---|---|
date | Mon, 02 Sep 2013 11:40:24 +0200 |
parents | 2d228d304247 |
children | dc3c8df55905 |
line wrap: on
line diff
--- 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): """