Skip to content

Instantly share code, notes, and snippets.

@crevier
Created June 15, 2018 11:24
Show Gist options
  • Save crevier/8ccb2680e068dbf324167891cfa7cd1c to your computer and use it in GitHub Desktop.
Save crevier/8ccb2680e068dbf324167891cfa7cd1c to your computer and use it in GitHub Desktop.
simple stdin option selection in groovy
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