private void printCommandSpec(CommandSpec spec, String label, PrintWriter pw, String initialIndent, String indent) { pw.printf("%s%n", label); pw.printf("%sname: '%s'%n", initialIndent, spec.name()); pw.printf("%saliases: %s%n", indent, Arrays.toString(spec.aliases())); pw.printf("%suserObject: %s%n", indent, spec.userObject()); pw.printf("%shelpCommand: %s%n", indent, spec.helpCommand()); pw.printf("%sdefaultValueProvider: %s%n", indent, spec.defaultValueProvider()); pw.printf("%sversionProvider: %s%n", indent, spec.versionProvider()); pw.printf("%sversion: %s%n", indent, Arrays.toString(spec.version())); List<OptionSpec> options = new ArrayList<OptionSpec>(spec.options()); Collections.sort(options, new Comparator<OptionSpec>() { public int compare(OptionSpec o1, OptionSpec o2) { return o1.shortestName().compareTo(o2.shortestName()); } }); printOptionList(options, pw, indent); printPositionalList(spec.positionalParameters(), pw, indent); printUnmatchedArgsBindingList(spec.unmatchedArgsBindings(), pw, indent); printMixinList(spec.mixins(), pw, indent); printUsageMessage(spec.usageMessage(), pw, indent); printParser(spec.parser(), pw, indent); printResourceBundle(spec.resourceBundle(), pw, indent); printSubcommandList(spec.subcommands(), pw, indent); }
visitObjectType(spec.versionProvider()); visitObjectType(spec.defaultValueProvider());
/** Adds the specified mixin {@code CommandSpec} object to the map of mixins for this command. * @param name the name that can be used to later retrieve the mixin * @param mixin the mixin whose options and positional parameters and other attributes to add to this command * @return this CommandSpec for method chaining */ public CommandSpec addMixin(String name, CommandSpec mixin) { mixins.put(name, mixin); parser.initSeparator(mixin.parser.separator()); initName(mixin.name()); initVersion(mixin.version()); initHelpCommand(mixin.helpCommand()); initVersionProvider(mixin.versionProvider()); initDefaultValueProvider(mixin.defaultValueProvider()); usageMessage.initFromMixin(mixin.usageMessage, this); for (Map.Entry<String, CommandLine> entry : mixin.subcommands().entrySet()) { addSubcommand(entry.getKey(), entry.getValue()); } for (OptionSpec optionSpec : mixin.options()) { addOption(optionSpec); } for (PositionalParamSpec paramSpec : mixin.positionalParameters()) { addPositional(paramSpec); } return this; }
/** Adds the specified mixin {@code CommandSpec} object to the map of mixins for this command. * @param name the name that can be used to later retrieve the mixin * @param mixin the mixin whose options and positional parameters and other attributes to add to this command * @return this CommandSpec for method chaining */ public CommandSpec addMixin(String name, CommandSpec mixin) { mixins.put(name, mixin); parser.initSeparator(mixin.parser.separator()); initName(mixin.name()); initVersion(mixin.version()); initHelpCommand(mixin.helpCommand()); initVersionProvider(mixin.versionProvider()); initDefaultValueProvider(mixin.defaultValueProvider()); usageMessage.initFromMixin(mixin.usageMessage, this); for (Map.Entry<String, CommandLine> entry : mixin.subcommands().entrySet()) { addSubcommand(entry.getKey(), entry.getValue()); } for (OptionSpec optionSpec : mixin.options()) { addOption(optionSpec); } for (PositionalParamSpec paramSpec : mixin.positionalParameters()) { addPositional(paramSpec); } return this; }
@Test public void testMixinAnnotationWithVersionProvider() { class MyVersionProvider implements IVersionProvider { public String[] getVersion() { return new String[] {"line 1", "line 2"} ; } } @Command(version = "Mixin 1.0", versionProvider = MyVersionProvider.class) class MixMeIn {} class Receiver { @Mixin MixMeIn mixMeIn; } CommandLine commandLine = new CommandLine(new Receiver(), new InnerClassFactory(this)); CommandSpec commandSpec = commandLine.getCommandSpec(); assertTrue(commandSpec.versionProvider() instanceof MyVersionProvider); assertArrayEquals(new String[] {"line 1", "line 2"}, commandSpec.version()); }
@Test public void testCommandSpec_forAnnotatedObjectLenient_returnsEmptyCommandSpec() { CommandSpec spec = CommandSpec.forAnnotatedObjectLenient(new Object()); assertTrue(spec.optionsMap().isEmpty()); assertTrue(spec.posixOptionsMap().isEmpty()); assertTrue(spec.options().isEmpty()); assertTrue(spec.positionalParameters().isEmpty()); assertTrue(spec.unmatchedArgsBindings().isEmpty()); assertTrue(spec.subcommands().isEmpty()); assertTrue(spec.mixins().isEmpty()); assertTrue(spec.requiredArgs().isEmpty()); assertFalse(spec.mixinStandardHelpOptions()); assertFalse(spec.helpCommand()); assertEquals("<main class>", spec.name()); assertArrayEquals(new String[0], spec.version()); assertNull(spec.versionProvider()); }
@Test public void testInitVersionProvider() { IVersionProvider versionProvider1 = new IVersionProvider() { public String[] getVersion() { return new String[0]; } }; IVersionProvider versionProvider2 = new IVersionProvider() { public String[] getVersion() { return new String[0]; } }; CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.versionProvider(versionProvider1); CommandSpec mixin = CommandSpec.wrapWithoutInspection(null); mixin.versionProvider(versionProvider2); spec.addMixin("helper", mixin); assertSame(versionProvider1, spec.versionProvider()); }
@Test public void testVersionHelp_versionProvider() { IVersionProvider provider = new IVersionProvider() { public String[] getVersion() { return new String[] {"2.0", "by provider"}; } }; CommandSpec spec = CommandSpec.create().versionProvider(provider); CommandLine commandLine = new CommandLine(spec); String actual = versionString(commandLine, Ansi.OFF); String expected = String.format("" + "2.0%n" + "by provider%n"); assertEquals(expected, actual); }
@Test public void testFailingVersionProvider() { @Command(versionProvider = FailingVersionProvider.class) class App {} CommandLine cmd = new CommandLine(new App()); try { cmd.printVersionHelp(System.out); fail("Expected exception"); } catch (ExecutionException ex) { assertEquals("Could not get version info from " + cmd.getCommandSpec().versionProvider() + ": java.lang.IllegalStateException: sorry can't give you a version", ex.getMessage()); } }
sep = appendSubcommandClasses(pw, sep, indent, spec.subcommands()); sep = appendStringArray(pw, sep, indent, "version = %s", spec.version(), EMPTY_ARRAY); sep = appendClassName(pw, sep, indent, "versionProvider = %s", spec.versionProvider()); sep = append(pw, sep, indent, "showDefaultValues = %s", spec.usageMessage().showDefaultValues(), false); sep = appendClassName(pw, sep, indent, "defaultValueProvider = %s", spec.defaultValueProvider());
private void updateCommandAttributes(CommandSpec result, Command cmd) { // null factory to prevent // javax.lang.model.type.MirroredTypeException: Attempt to access Class object for TypeMirror picocli.CommandLine.NoVersionProvider result.updateCommandAttributes(cmd, null); try { cmd.versionProvider(); } catch (MirroredTypeException ex) { VersionProviderMetaData provider = new VersionProviderMetaData(ex.getTypeMirror()); if (!provider.isDefault()) { result.versionProvider(provider); } } try { cmd.defaultValueProvider(); } catch (MirroredTypeException ex) { DefaultValueProviderMetaData provider = new DefaultValueProviderMetaData(ex.getTypeMirror()); if (!provider.isDefault()) { result.defaultValueProvider(provider); } } }
@Test public void testCommandSpec_forAnnotatedObjectLenient_returnsEmptyCommandSpec() { CommandSpec spec = CommandSpec.forAnnotatedObjectLenient(new Object()); assertTrue(spec.optionsMap().isEmpty()); assertTrue(spec.posixOptionsMap().isEmpty()); assertTrue(spec.options().isEmpty()); assertTrue(spec.positionalParameters().isEmpty()); assertTrue(spec.unmatchedArgsBindings().isEmpty()); assertTrue(spec.subcommands().isEmpty()); assertTrue(spec.mixins().isEmpty()); assertTrue(spec.requiredArgs().isEmpty()); assertFalse(spec.mixinStandardHelpOptions()); assertFalse(spec.helpCommand()); assertEquals("<main class>", spec.name()); assertArrayEquals(new String[0], spec.version()); assertNull(spec.versionProvider()); }
@Test public void testMixinAnnotationWithVersionProvider() { class MyVersionProvider implements IVersionProvider { public String[] getVersion() { return new String[] {"line 1", "line 2"} ; } } @Command(version = "Mixin 1.0", versionProvider = MyVersionProvider.class) class MixMeIn {} class Receiver { @Mixin MixMeIn mixMeIn; } CommandLine commandLine = new CommandLine(new Receiver(), new InnerClassFactory(this)); CommandSpec commandSpec = commandLine.getCommandSpec(); assertTrue(commandSpec.versionProvider() instanceof MyVersionProvider); assertArrayEquals(new String[] {"line 1", "line 2"}, commandSpec.version()); }
@Test public void testInitVersionProvider() { IVersionProvider versionProvider1 = new IVersionProvider() { public String[] getVersion() { return new String[0]; } }; IVersionProvider versionProvider2 = new IVersionProvider() { public String[] getVersion() { return new String[0]; } }; CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.versionProvider(versionProvider1); CommandSpec mixin = CommandSpec.wrapWithoutInspection(null); mixin.versionProvider(versionProvider2); spec.addMixin("helper", mixin); assertSame(versionProvider1, spec.versionProvider()); }
@Test public void testVersionHelp_versionProvider() { IVersionProvider provider = new IVersionProvider() { public String[] getVersion() { return new String[] {"2.0", "by provider"}; } }; CommandSpec spec = CommandSpec.create().versionProvider(provider); CommandLine commandLine = new CommandLine(spec); String actual = versionString(commandLine, Ansi.OFF); String expected = String.format("" + "2.0%n" + "by provider%n"); assertEquals(expected, actual); }
@Test public void testFailingVersionProvider() { @Command(versionProvider = FailingVersionProvider.class) class App {} CommandLine cmd = new CommandLine(new App()); try { cmd.printVersionHelp(System.out); fail("Expected exception"); } catch (ExecutionException ex) { assertEquals("Could not get version info from " + cmd.getCommandSpec().versionProvider() + ": java.lang.IllegalStateException: sorry can't give you a version", ex.getMessage()); } }