Created
June 15, 2018 11:24
-
-
Save crevier/8ccb2680e068dbf324167891cfa7cd1c to your computer and use it in GitHub Desktop.
simple stdin option selection in groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void displayOptions(String name, List<String> options) { | |
print("Select $name ::") | |
options.eachWithIndex { String entry, int i -> | |
print(" $entry [${i + 1}]${i + 1 == options.size() ? ": " : ","}") | |
} | |
} | |
int readUserInput(List<String> options) { | |
displayOptions(options) | |
def input = ++new Scanner(System.in) | |
if (input.isInteger() && input.toInteger() - 1 in options.indices) { | |
return input.toInteger() - 1 | |
} | |
println("\nPlease select a valid input !") | |
return readUserInput(options) | |
} | |
def environements = [ | |
"dev", "tst", "preprod", "prod" | |
] | |
def input = readUserInput("env",environements) | |
println("User selected = (${environements[input]})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment