为 @Option 注释提供默认设置,java

Giving an @Option annotation a default setting, java

@Option(name = "-readFromFolder",usage = "specifies a folder containing files to read")
    private String folderName;

现在的工作方式是用户指定了文件夹-readFromFolder=/home

我希望他们能够说 -readFromFolder 并设置要查看的默认值,例如 ${user.home}

类似这样的东西,但我搞不懂语法

@Option(name = "-readFromFolder",usage = "specifies a folder containing files to read")
        private String folderName (default=${user.home});

我假设您正在使用 args4j 库 - 它不支持默认参数值。你必须自己实现它:

private final static String DEFAULT_FOLDER_NAME = "${user.home}";
@Option(name = "-readFromFolder",usage = "specifies a folder containing files to read")
private String folderName;

public String getFolderName() {
    return null == folderName ? DEFAULT_FOLDER_NAME : folderName ;
}

在成员变量声明本身设置默认值是可行的

@Option(name = "-o", aliases = "--output", usage = "Fully qualified path and name of output JSON file.", required = false)
private String output = "./examples/sample.json"

从命令行对此进行了测试。似乎按预期工作。