private void printParser(ParserSpec parser, PrintWriter pw, String indent) { pw.printf("%sParserSpec:%n", indent); indent += " "; pw.printf("%sseparator: '%s'%n", indent, parser.separator()); pw.printf("%sendOfOptionsDelimiter: '%s'%n", indent, parser.endOfOptionsDelimiter()); pw.printf("%sexpandAtFiles: %s%n", indent, parser.expandAtFiles()); pw.printf("%satFileCommentChar: '%s'%n", indent, parser.atFileCommentChar()); pw.printf("%soverwrittenOptionsAllowed: %s%n", indent, parser.overwrittenOptionsAllowed()); pw.printf("%sunmatchedArgumentsAllowed: %s%n", indent, parser.unmatchedArgumentsAllowed()); pw.printf("%sunmatchedOptionsArePositionalParams: %s%n", indent, parser.unmatchedOptionsArePositionalParams()); pw.printf("%sstopAtUnmatched: %s%n", indent, parser.stopAtUnmatched()); pw.printf("%sstopAtPositional: %s%n", indent, parser.stopAtPositional()); pw.printf("%sposixClusteredShortOptionsAllowed: %s%n", indent, parser.posixClusteredShortOptionsAllowed()); pw.printf("%saritySatisfiedByAttachedOptionParam: %s%n", indent, parser.aritySatisfiedByAttachedOptionParam()); pw.printf("%scaseInsensitiveEnumValuesAllowed: %s%n", indent, parser.caseInsensitiveEnumValuesAllowed()); pw.printf("%scollectErrors: %s%n", indent, parser.collectErrors()); pw.printf("%slimitSplit: %s%n", indent, parser.limitSplit()); pw.printf("%stoggleBooleanFlags: %s%n", indent, parser.toggleBooleanFlags()); }
String[] splitValue(String value, ParserSpec parser, Range arity, int consumed) { if (splitRegex().length() == 0) { return new String[] {value}; } int limit = parser.limitSplit() ? Math.max(arity.max - consumed, 0) : 0; if (parser.splitQuotedStrings()) { return debug(value.split(splitRegex(), limit), "Split (ignoring quotes)", value); } return debug(splitRespectingQuotedStrings(value, limit, parser, this, splitRegex()), "Split", value); } private String[] debug(String[] result, String msg, String value) {
@Test public void testMapFieldHelpSplit_with_limitSplit() { class App { @Parameters(arity = "2", split = "\\|", paramLabel = "FIXTAG=VALUE", description = "Exactly two lists of vertical bar '|'-separated FIXTAG=VALUE pairs.") Map<Integer,String> message; @Option(names = {"-P", "-map"}, split = ",", paramLabel = "TIMEUNIT=VALUE", description = "Any number of TIMEUNIT=VALUE pairs. These may be specified separately (-PTIMEUNIT=VALUE) or as a comma-separated list.") Map<TimeUnit, String> map; } CommandSpec spec = CommandSpec.forAnnotatedObject(new App()); spec.parser().limitSplit(true); String actual = usageString(new CommandLine(spec), Help.Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-P=TIMEUNIT=VALUE[,TIMEUNIT=VALUE]...]...%n" + " (FIXTAG=VALUE\\|FIXTAG=VALUE)...%n" + " (FIXTAG=VALUE\\|FIXTAG=VALUE)...%n" + " Exactly two lists of vertical bar '|'-separated FIXTAG=VALUE pairs.%n" + " -P, -map=TIMEUNIT=VALUE[,TIMEUNIT=VALUE]...%n" + " Any number of TIMEUNIT=VALUE pairs. These may be specified separately%n" + " (-PTIMEUNIT=VALUE) or as a comma-separated list.%n"); assertEquals(expected, actual); }
String optionalSep = empty(split) ? " [" : "[" + split; boolean unlimitedSplit = !empty(split) && !commandSpec.parser().limitSplit(); boolean limitedSplit = !empty(split) && commandSpec.parser().limitSplit(); Text repeating = paramName; int paramCount = 1;
spec.parser().limitSplit(true); String actual = usageString(new CommandLine(spec), Help.Ansi.OFF); String expected = String.format("" +
@Test public void testMapFieldHelpSplit_with_limitSplit() { class App { @Parameters(arity = "2", split = "\\|", paramLabel = "FIXTAG=VALUE", description = "Exactly two lists of vertical bar '|'-separated FIXTAG=VALUE pairs.") Map<Integer,String> message; @Option(names = {"-P", "-map"}, split = ",", paramLabel = "TIMEUNIT=VALUE", description = "Any number of TIMEUNIT=VALUE pairs. These may be specified separately (-PTIMEUNIT=VALUE) or as a comma-separated list.") Map<TimeUnit, String> map; } CommandSpec spec = CommandSpec.forAnnotatedObject(new App()); spec.parser().limitSplit(true); String actual = usageString(new CommandLine(spec), Help.Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-P=TIMEUNIT=VALUE[,TIMEUNIT=VALUE]...]...%n" + " (FIXTAG=VALUE\\|FIXTAG=VALUE)...%n" + " (FIXTAG=VALUE\\|FIXTAG=VALUE)...%n" + " Exactly two lists of vertical bar '|'-separated FIXTAG=VALUE pairs.%n" + " -P, -map=TIMEUNIT=VALUE[,TIMEUNIT=VALUE]...%n" + " Any number of TIMEUNIT=VALUE pairs. These may be specified separately%n" + " (-PTIMEUNIT=VALUE) or as a comma-separated list.%n"); assertEquals(expected, actual); }
private boolean splitFirst() { return limitSplit(); } /** Returns true if arguments should be split first before any further processing and the number of
private <T> T parseCommonsCliCompatible(T obj, String[] args) { CommandLine cmd = new CommandLine(obj); cmd.getCommandSpec().parser() .limitSplit(true) .aritySatisfiedByAttachedOptionParam(true); cmd.parseArgs(args); return obj; }
String[] splitValue(String value, ParserSpec parser, Range arity, int consumed) { if (splitRegex().length() == 0) { return new String[] {value}; } int limit = parser.limitSplit() ? Math.max(arity.max - consumed, 0) : 0; if (parser.splitQuotedStrings()) { return debug(value.split(splitRegex(), limit), "Split (ignoring quotes)", value); } return debug(splitRespectingQuotedStrings(value, limit, parser), "Split", value); } private String[] debug(String[] result, String msg, String value) {
String optionalSep = empty(split) ? " [" : "[" + split; boolean unlimitedSplit = !empty(split) && !commandSpec.parser().limitSplit(); boolean limitedSplit = !empty(split) && commandSpec.parser().limitSplit(); Text repeating = paramName; int paramCount = 1;
spec.parser().limitSplit(true); String actual = usageString(new CommandLine(spec), Help.Ansi.OFF); String expected = String.format("" +
private boolean splitFirst() { return limitSplit(); } /** Returns true if arguments should be split first before any further processing and the number of
private <T> T parseCommonsCliCompatible(T obj, String[] args) { CommandLine cmd = new CommandLine(obj); cmd.getCommandSpec().parser() .limitSplit(true) .aritySatisfiedByAttachedOptionParam(true); cmd.parseArgs(args); return obj; }