Anthony Anthony - 2 months ago 9
Groovy Question

How to pass a boolean to a method with a variable name

I have a method such as this

def uploadFile(String fileName, boolean performCleanup = false) {
//upload file
if (performCleanup == true) {
//delete local file

I call this method in two ways:

  1. uploadFile("/some/path/to/file.txt")

  2. uploadFile("/some/path/to/file.txt", true)

In the second case, for better readability, I would like to pass a variable name that would suggest what the significance of passing

Something like,
uploadFile("/some/path/to/file.txt", performCleanup: true")

Is this possible?


By introducing and extra closure you can create a nicely readable micro DSL:

def performCleanup = {
  println "cleanup $it" //delete local file
def upload(fileName) {
  println "upload file $fileName" //upload file
  [then: { action ->

upload "path"
upload "path" then performCleanup