Search code examples
javacommand-lineannotationsargs4j

Giving an @Option annotation a default setting, java


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

The way it works now is that the user has specifies the folder -readFromFolder=/home

I want them to be able to say -readFromFolder and set a default for which to look into, like ${user.home}

Something that works like this, but I ca't figure out the syntax

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

Solution

  • I assume you are using args4j library - it does not support default parameter values. You have to implement it yourself:

    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 ;
    }