I'm developing a custom gradle task and i'd like to have an option which does act like a flag and doesn't require a value.
I just want to check if it is set
Basically : I can use plugin either gradle my-task
or gradle my-task --flag
and be able to check if --flag
is present or not to define plugin processing.
I cannot find any thing in the official documentation
Custom command line options for tasks are available since Gradle 4.6 via @Option
annotation on task property setters. Documentation link: Declaring and Using Command Line Options.
According to the documentation, value-less command line options are supported via boolean
properties.
boolean
,Boolean
,Property<Boolean>
Describes an option with the value
true
orfalse
. Passing the option on the command line treats the value astrue
. For example--enabled
equates totrue
. The absence of the option uses the default value of the property.
(Untested) Example:
import org.gradle.api.tasks.options.Option;
public class MyTask extends DefaultTask {
private boolean flag;
@Option(option = "flag", description = "Sets the flag")
public void setFlag(boolean flag) {
this.flag = flag;
}
@Input
public boolean isFlag() {
return flag;
}
@TaskAction
public void doWork() {
if (flag) {
getLogger().quiet("Flag is present");
}
}
}