Is there a way to automatically strip leading space from Apache commons-cli option value?

Viewed 383

Consider the following example:

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class ApacheCommonsCliWhitespaceTest {

  public static void main(String[] args) throws ParseException {
    Options options = new Options();
    Option option = new Option("t", "test", true, "test description");
    options.addOption(option);

    CommandLineParser parser = new DefaultParser();

    String[] parameters = new String[1];

    parameters[0] = "-t ping";
    CommandLine result = parser.parse(options, parameters);
    System.out.println("*" + result.getOptionValue("t") + "*");

    parameters[0] = "-test ping";
    result = parser.parse(options, parameters);
    System.out.println("*" + result.getOptionValue("t") + "*");
  }
}

The result will be

* ping*
* ping*

Is there really no way to remove the leading space automatically? I would have expected that to be a common use case.

2 Answers
Related