private void printArg(ArgSpec arg, PrintWriter pw, String indent) { pw.printf("%sdescription: %s%n", indent, Arrays.toString(arg.description())); pw.printf("%sdescriptionKey: '%s'%n", indent, arg.descriptionKey()); pw.printf("%stypeInfo: %s%n", indent, arg.typeInfo()); pw.printf("%sarity: %s%n", indent, arg.arity()); pw.printf("%ssplitRegex: '%s'%n", indent, arg.splitRegex()); pw.printf("%sinteractive: %s%n", indent, arg.interactive()); pw.printf("%srequired: %s%n", indent, arg.required()); pw.printf("%shidden: %s%n", indent, arg.hidden()); pw.printf("%shideParamSyntax: %s%n", indent, arg.hideParamSyntax()); pw.printf("%sdefaultValue: '%s'%n", indent, arg.defaultValue()); pw.printf("%sshowDefaultValue: %s%n", indent, arg.showDefaultValue()); pw.printf("%shasInitialValue: %s%n", indent, arg.hasInitialValue()); pw.printf("%sinitialValue: '%s'%n", indent, arg.initialValue()); pw.printf("%sparamLabel: '%s'%n", indent, arg.paramLabel()); pw.printf("%sconverters: %s%n", indent, Arrays.toString(arg.converters())); pw.printf("%scompletionCandidates: %s%n", indent, iter(arg.completionCandidates())); pw.printf("%sgetter: %s%n", indent, arg.getter()); pw.printf("%ssetter: %s%n", indent, arg.setter()); }
@Test public void testIssue402() { class AppWithEnum { @Parameters(type = Choices.class) private Choices choice; } AppWithEnum app; try { app = CommandLine.populateCommand(new AppWithEnum(), "CHOICE3"); } catch (ParameterException e) { assertEquals("<choice>", e.getArgSpec().paramLabel()); assertEquals(2, e.getArgSpec().type().getEnumConstants().length); assertEquals(Choices.CHOICE1, e.getArgSpec().type().getEnumConstants()[0]); assertEquals(Choices.CHOICE2, e.getArgSpec().type().getEnumConstants()[1]); assertEquals("CHOICE3", e.getValue()); } } @Test
@Test public void testMissingRequiredParams() { class Example { @Parameters(index = "1", arity = "0..1") String optional; @Parameters(index = "0") String mandatory; } try { CommandLine.populateCommand(new Example(), new String[] {"mandatory"}); } catch (MissingParameterException ex) { fail(); } try { CommandLine.populateCommand(new Example(), new String[0]); fail("Should not accept missing mandatory parameter"); } catch (MissingParameterException ex) { assertEquals("Missing required parameter: <mandatory>", ex.getMessage()); assertEquals(1, ex.getMissing().size()); assertEquals("<mandatory>", ex.getMissing().get(0).paramLabel()); } } @Test
public Text renderParameterLabel(ArgSpec argSpec, Ansi ansi, List<IStyle> styles) { Range capacity = argSpec.isOption() ? argSpec.arity() : ((PositionalParamSpec)argSpec).capacity(); if (capacity.max == 0) { return ansi.new Text(""); } if (argSpec.hideParamSyntax()) { return ansi.apply((argSpec.isOption() ? separator() : "") + argSpec.paramLabel(), styles); } Text paramName = ansi.apply(argSpec.paramLabel(), styles); String split = argSpec.splitRegex(); String mandatorySep = empty(split) ? " " : split;
@Test public void testMissingRequiredParams() { class Example { @Parameters(index = "1", arity = "0..1") String optional; @Parameters(index = "0") String mandatory; } try { CommandLine.populateCommand(new Example(), new String[] {"mandatory"}); } catch (MissingParameterException ex) { fail(); } try { CommandLine.populateCommand(new Example(), new String[0]); fail("Should not accept missing mandatory parameter"); } catch (MissingParameterException ex) { assertEquals("Missing required parameter: <mandatory>", ex.getMessage()); assertEquals(1, ex.getMissing().size()); assertEquals("<mandatory>", ex.getMissing().get(0).paramLabel()); } } @Test
@Test public void testIssue402() { class AppWithEnum { @Parameters(type = Choices.class) private Choices choice; } AppWithEnum app; try { app = CommandLine.populateCommand(new AppWithEnum(), "CHOICE3"); } catch (ParameterException e) { assertEquals("<choice>", e.getArgSpec().paramLabel()); assertEquals(2, e.getArgSpec().type().getEnumConstants().length); assertEquals(Choices.CHOICE1, e.getArgSpec().type().getEnumConstants()[0]); assertEquals(Choices.CHOICE2, e.getArgSpec().type().getEnumConstants()[1]); assertEquals("CHOICE3", e.getValue()); } } @Test
private String optionDescription(String prefix, ArgSpec argSpec, int index) { String desc = ""; if (argSpec.isOption()) { desc = prefix + "option '" + ((OptionSpec) argSpec).longestName() + "'"; if (index >= 0) { if (argSpec.arity().max > 1) { desc += " at index " + index; } desc += " (" + argSpec.paramLabel() + ")"; } } else { desc = prefix + "positional parameter at index " + ((PositionalParamSpec) argSpec).index() + " (" + argSpec.paramLabel() + ")"; } return desc; }
public String defaultValue(ArgSpec argSpec) throws Exception { return "DURATION".equals(argSpec.paramLabel()) ? "1200" : null; } }
public Text renderParameterLabel(ArgSpec argSpec, Ansi ansi, List<IStyle> styles) { return ansi.apply(argSpec.paramLabel(), styles); } public String separator() { return ""; }
private static String describe(ArgSpec argSpec, String separator) { String prefix = (argSpec.isOption()) ? ((OptionSpec) argSpec).longestName() + separator : "params[" + ((PositionalParamSpec) argSpec).index() + "]" + separator; return prefix + argSpec.paramLabel(); } }
public Text renderParameterLabel(ArgSpec argSpec, Ansi ansi, List<IStyle> styles) { Range capacity = argSpec.isOption() ? argSpec.arity() : ((PositionalParamSpec)argSpec).capacity(); if (capacity.max == 0) { return ansi.new Text(""); } if (argSpec.hideParamSyntax()) { return ansi.apply((argSpec.isOption() ? separator() : "") + argSpec.paramLabel(), styles); } Text paramName = ansi.apply(argSpec.paramLabel(), styles); String split = argSpec.splitRegex(); String mandatorySep = empty(split) ? " " : split;
private String optionDescription(String prefix, ArgSpec argSpec, int index) { String desc = ""; if (argSpec.isOption()) { desc = prefix + "option '" + ((OptionSpec) argSpec).longestName() + "'"; if (index >= 0) { if (argSpec.arity().max > 1) { desc += " at index " + index; } desc += " (" + argSpec.paramLabel() + ")"; } } else { desc = prefix + "positional parameter at index " + ((PositionalParamSpec) argSpec).index() + " (" + argSpec.paramLabel() + ")"; } return desc; }
public Text renderParameterLabel(ArgSpec argSpec, Ansi ansi, List<IStyle> styles) { return ansi.apply(argSpec.paramLabel(), styles); } public String separator() { return ""; }
private static String describe(ArgSpec argSpec, String separator) { String prefix = (argSpec.isOption()) ? ((OptionSpec) argSpec).longestName() + separator : "params[" + ((PositionalParamSpec) argSpec).index() + "]" + separator; return prefix + argSpec.paramLabel(); } }