Skip to content

Add support for multi-value defaultValue and fallbackValue for parameters and options #879

@deining

Description

@deining

Somehow related to #877:

Currently, it is possible to annotate a defaultValue for options and parameters. However, one can specify a string/value only, not multiple values. Fortunately, one can workaround this by using split on the option/positional parameter definition, this solution has some limitations, however.

Code example that illustrates the issue:

public class DefaultValueMultipleOptions {

    public static void main(String... args) {
        CommandLine cmd = new CommandLine(new MyFileCommand());
        cmd.execute(args);
    }
}

@Command( name="myCommand", mixinStandardHelpOptions = true)
class MyFileCommand implements Runnable {

    @Parameters(defaultValue = { "default file 1", "default file 2" }) // does not work (yet)
    // @Parameters(defaultValue = "default file 1,default file 2", split = ",") // this is workaround
    String[] files = { "file 1", "file 2" };

    public void run() {
        for (String file : files)
          System.out.println("File: " + file);
        
        System.out.println(String.format("%s files", files.length));
    }
}

As said, this is a low priority enhacement as we have a workaround here.
I just wanted to get the author's opinion on this prior to authoring a PR ;-)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions