private void printParametersAnnotation(PrintWriter pw, PositionalParamSpec spec, String indent) { pw.printf("%s@%s", indent, importer.getImportedName(Parameters.class.getCanonicalName())); indent = String.format(",%n%s ", indent); String sep = "("; sep = append(pw, sep, indent, "index = \"%s\"", spec.index().toString(), spec.index().isUnspecified()); sep = appendStringArray(pw, sep, indent, "description = %s", spec.description(), EMPTY_ARRAY); sep = append(pw, sep, indent, "arity = \"%s\"", spec.arity().toString(), spec.arity().isUnspecified() ? spec.arity().toString() : ""); sep = append(pw, sep, indent, "paramLabel = \"%s\"", spec.paramLabel(), "<" + argElementName(spec) + ">"); sep = append(pw, sep, indent, "hideParamSyntax = %s", spec.hideParamSyntax(), false); sep = appendTypeInfo(pw, sep, indent, spec.typeInfo()); sep = appendTypeConverter(pw, sep, indent, spec.converters()); sep = append(pw, sep, indent, "split = \"%s\"", spec.splitRegex(), ""); sep = append(pw, sep, indent, "hidden = %s", spec.hidden(), false); sep = append(pw, sep, indent, "defaultValue = \"%s\"", spec.defaultValue() == null ? "__no_default_value__" : spec.defaultValue(), "__no_default_value__"); sep = append(pw, sep, indent, "showDefaultValue = %s", spec.showDefaultValue(), CommandLine.Help.Visibility.ON_DEMAND); sep = appendCompletionCandidates(pw, sep, indent, spec); sep = append(pw, sep, indent, "interactive = %s", spec.interactive(), false); sep = append(pw, sep, indent, "descriptionKey = \"%s\"", spec.descriptionKey(), ""); if (!"(".equals(sep)) { pw.print(")"); } }
@SuppressWarnings("deprecation") private void printOptionAnnotation(PrintWriter pw, OptionSpec spec, String indent) { pw.printf("%s@%s", indent, importer.getImportedName(Option.class.getCanonicalName())); indent = String.format(",%n%s ", indent); String sep = "("; sep = appendStringArray(pw, sep, indent, "names = %s", spec.names(), EMPTY_ARRAY); sep = append(pw, sep, indent, "required = %s", spec.required(), false); sep = append(pw, sep, indent, "help = %s", spec.help(), false); sep = append(pw, sep, indent, "usageHelp = %s", spec.usageHelp(), false); sep = append(pw, sep, indent, "versionHelp = %s", spec.versionHelp(), false); sep = appendStringArray(pw, sep, indent, "description = %s", spec.description(), EMPTY_ARRAY); sep = append(pw, sep, indent, "arity = \"%s\"", spec.arity().toString(), spec.arity().isUnspecified() ? spec.arity().toString() : ""); sep = append(pw, sep, indent, "paramLabel = \"%s\"", spec.paramLabel(), "<" + argElementName(spec) + ">"); sep = append(pw, sep, indent, "hideParamSyntax = %s", spec.hideParamSyntax(), false); sep = appendTypeInfo(pw, sep, indent, spec.typeInfo()); sep = appendTypeConverter(pw, sep, indent, spec.converters()); sep = append(pw, sep, indent, "split = \"%s\"", spec.splitRegex(), ""); sep = append(pw, sep, indent, "hidden = %s", spec.hidden(), false); sep = append(pw, sep, indent, "defaultValue = %s", spec.defaultValue() == null ? "__no_default_value__" : spec.defaultValue(), "__no_default_value__"); sep = append(pw, sep, indent, "showDefaultValue = \"%s\"", spec.showDefaultValue(), CommandLine.Help.Visibility.ON_DEMAND); sep = appendCompletionCandidates(pw, sep, indent, spec); sep = append(pw, sep, indent, "interactive = %s", spec.interactive(), false); sep = append(pw, sep, indent, "descriptionKey = \"%s\"", spec.descriptionKey(), ""); if (!"(".equals(sep)) { pw.print(")"); } }
@Test public void testArityForOption_listFieldImplicitArity1() throws Exception { class ImplicitList { @Option(names = "-a") List<Integer> listIntegers; } Range arity = Range.optionArity(ImplicitList.class.getDeclaredField("listIntegers")); assertEquals(Range.valueOf("1"), arity); assertEquals("1", arity.toString()); } @Test
@Test public void testArityForOption_listFieldImplicitArity1() throws Exception { class ImplicitList { @Option(names = "-a") List<Integer> listIntegers; } Range arity = Range.optionArity(ImplicitList.class.getDeclaredField("listIntegers")); assertEquals(Range.valueOf("1"), arity); assertEquals("1", arity.toString()); } @Test
@Test public void testArityForParameters_listFieldImplicitArity0_1() throws Exception { class Params { @Parameters(type = Integer.class) List<Integer> list; } Range arity = Range.parameterArity(Params.class.getDeclaredField("list")); assertEquals(Range.valueOf("0..1"), arity); assertEquals("0..1", arity.toString()); } @Test
@Test public void testArityForOption_arrayFieldImplicitArity1() throws Exception { class ImplicitList { @Option(names = "-a") int[] intArray; } Range arity = Range.optionArity(ImplicitList.class.getDeclaredField("intArray")); assertEquals(Range.valueOf("1"), arity); assertEquals("1", arity.toString()); }
@Test public void testArityForParameters_listFieldImplicitArity0_1() throws Exception { class Params { @Parameters(type = Integer.class) List<Integer> list; } Range arity = Range.parameterArity(Params.class.getDeclaredField("list")); assertEquals(Range.valueOf("0..1"), arity); assertEquals("0..1", arity.toString()); } @Test
@Test public void testArityForOption_arrayFieldImplicitArity1() throws Exception { class ImplicitList { @Option(names = "-a") int[] intArray; } Range arity = Range.optionArity(ImplicitList.class.getDeclaredField("intArray")); assertEquals(Range.valueOf("1"), arity); assertEquals("1", arity.toString()); }
@Test public void testArityForOption_intFieldImplicitArity1() throws Exception { Range arity = Range.optionArity(SupportedTypes2.class.getDeclaredField("intField")); assertEquals(Range.valueOf("1"), arity); assertEquals("1", arity.toString()); } @Test
@Test public void testArityConstructor_variableRange() { Range arity = new Range(1, Integer.MAX_VALUE, true, false, null); assertEquals("min", 1, arity.min); assertEquals("max", Integer.MAX_VALUE, arity.max); assertEquals("1..*", arity.toString()); assertEquals(Range.valueOf("1..*"), arity); }
@Test public void testOptionArity_forNonAnnotatedField() throws Exception { Range arity = Range.optionArity(SupportedTypes2.class.getDeclaredField("nonOptionField")); assertEquals(0, arity.max); assertEquals(0, arity.min); assertEquals(false, arity.isVariable); assertEquals("0", arity.toString()); } @Test
@Test public void testArityConstructor_fixedRange() { Range arity = new Range(1, 23, false, false, null); assertEquals("min", 1, arity.min); assertEquals("max", 23, arity.max); assertEquals("1..23", arity.toString()); assertEquals(Range.valueOf("1..23"), arity); } @Test
@Test public void testArityForParameters_arrayFieldImplicitArity0_1() throws Exception { class Args { @Parameters File[] inputFiles; } Range arity = Range.parameterArity(Args.class.getDeclaredField("inputFiles")); assertEquals(Range.valueOf("0..1"), arity); assertEquals("0..1", arity.toString()); } @Test
@Test public void testArityForParameters_intFieldImplicitArity1() throws Exception { class ImplicitSingleField { @Parameters int intSingleValue; } Range arity = Range.parameterArity(ImplicitSingleField.class.getDeclaredField("intSingleValue")); assertEquals(Range.valueOf("1"), arity); assertEquals("1", arity.toString()); } @Test
@Test public void testArityForParameters_booleanFieldImplicitArity1() throws Exception { class ImplicitBoolField { @Parameters boolean boolSingleValue; } Range arity = Range.parameterArity(ImplicitBoolField.class.getDeclaredField("boolSingleValue")); assertEquals(Range.valueOf("1"), arity); assertEquals("1", arity.toString()); } @Test
@Test public void testParameterIndex_WhenDefined() throws Exception { class ImplicitBoolField { @Parameters(index = "2..3") boolean boolSingleValue; } Range arity = Range.parameterIndex(ImplicitBoolField.class.getDeclaredField("boolSingleValue")); assertEquals(3, arity.max); assertEquals(2, arity.min); assertEquals(false, arity.isVariable); assertEquals("2..3", arity.toString()); }
@Test public void testArityForOption_isExplicitlyDeclaredValue() throws Exception { class Params { @Option(names = "-timeUnitList", type = TimeUnit.class, arity = "3") List<TimeUnit> timeUnitList; } Range arity = Range.optionArity(Params.class.getDeclaredField("timeUnitList")); assertEquals(Range.valueOf("3"), arity); assertEquals("3", arity.toString()); } @Test
@Test public void testArityForOption_booleanFieldImplicitArity0() throws Exception { Range arity = Range.optionArity(SupportedTypes2.class.getDeclaredField("booleanField")); assertEquals(Range.valueOf("0"), arity); assertEquals("0", arity.toString()); } @Test
@Test public void testParameterIndex_WhenUndefined() throws Exception { class ImplicitBoolField { @Parameters boolean boolSingleValue; } Range arity = Range.parameterIndex(ImplicitBoolField.class.getDeclaredField("boolSingleValue")); assertEquals(Integer.MAX_VALUE, arity.max); assertEquals(0, arity.min); assertEquals(true, arity.isVariable); assertEquals("0..*", arity.toString()); }
@Test public void testParameterArityWithOptionMember() throws Exception { class ImplicitBoolField { @Option(names = "-x") boolean boolSingleValue; } Range arity = Range.parameterArity(ImplicitBoolField.class.getDeclaredField("boolSingleValue")); assertEquals(0, arity.max); assertEquals(0, arity.min); assertEquals(false, arity.isVariable); assertEquals("0", arity.toString()); }