Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created August 25, 2014 16:34
Show Gist options
  • Save bdkosher/2d423dc3f3e65e3dee78 to your computer and use it in GitHub Desktop.
Save bdkosher/2d423dc3f3e65e3dee78 to your computer and use it in GitHub Desktop.
Pattern for assigning and transforming String args to target File type, plus validating their existence--all in one compact piece of code
def (f1, f2) = [new File(args[0]), new File(args[1])].each { f ->
if (!f.exists() || f.isDirectory()) {
println "$f does not exist or is a directory"
System.exit(0);
} else {
f
}
}
println "$f1 and $f2 provided"
@bdkosher
Copy link
Author

TODO: Generalize this code to deal with type conversion, validation, and expected number of arguments. Instead of exiting upon validation failure, we could return a processed argument back that represents the specific error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment