考虑以下示例:
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") + "*");
}
}
结果将是
* ping*
* ping*
真的没有办法自动删除前导空格吗?我本来希望这是一个常见的用例。