Consider the following MWE:
import picocli.CommandLine;
import static picocli.CommandLine.*;
import java.util.*;
import java.io.*;
public class MWE {
static class Tar {
@Option(names = { "-f", "--file" }, paramLabel = "ARCHIVE", description = "the archive file")
Optional<File> archive;
public Tar() {
archive = Optional.of(new File("helloworld"));
}
}
public static void main(String[] args) throws Exception{
Tar tar = new Tar();
System.out.println(tar.archive);
new CommandLine(tar).parseArgs(args);
System.out.println(tar.archive);
}
}
The picocli documentation claims that defaultValue
has a default value of "__no_default_value__"
, which I am interpreting to imply that it will not change the value of any field if there is no flag given on the command line.
Thus, when I invoke this program with no arguments, I expect the following output:
Optional[helloworld]
Optional[helloworld]
However, the actual output I get is:
Optional[helloworld]
Optional.empty
It appears that the parseArguments
call has set archive
to Optional.empty
despite the lack of arguments given on the command line and the lack of explicit setting for the default value of this option.
Is there a way to prevent this behavior and get the expected output?
Note: I'm asking about this because I want to be able to set overridable default values in the subclass of an options class, and subclass constructor seems like a reasonable place to put it.
I raised an issue and the author ended up changing this behavior in the latest main
branch, so the example now has the expected behavior described in the question.