/** * Returns a map of user objects whose options and positional parameters were added to ("mixed in" with) this command. * @return a new Map containing the user objects mixed in with this command. If {@code CommandSpec} objects without * user objects were programmatically added, use the {@link CommandSpec#mixins() underlying model} directly. * @since 3.0 */ public Map<String, Object> getMixins() { Map<String, CommandSpec> mixins = getCommandSpec().mixins(); Map<String, Object> result = new LinkedHashMap<String, Object>(); for (String name : mixins.keySet()) { result.put(name, mixins.get(name).userObject); } return result; }
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); }
private static boolean isMixedIn(PositionalParamSpec positional, CommandSpec spec) { for (CommandSpec mixin : spec.mixins().values()) { for (PositionalParamSpec mixedIn : mixin.positionalParameters()) { if (mixedIn.equals(positional)) { return true; } } } return false; }
@Test public void testAnnotateMethod_mixinParameterFirst() { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinParameters.class, "mixinFirst").get(0); CommandLine cmd = new CommandLine(m); CommandSpec spec = cmd.getCommandSpec(); assertEquals(1, spec.mixins().size()); assertEquals(1, spec.positionalParameters().size()); assertEquals(3, spec.options().size()); spec = spec.mixins().get("arg0"); assertEquals(SomeMixin.class, spec.userObject().getClass()); }
@Test public void testAnnotateMethod_positionalAndMixinParameter() { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinParameters.class, "posAndMixin").get(0); CommandLine cmd = new CommandLine(m); CommandSpec spec = cmd.getCommandSpec(); assertEquals(1, spec.mixins().size()); assertEquals(1, spec.positionalParameters().size()); spec = spec.mixins().get("arg1"); assertEquals(SomeMixin.class, spec.userObject().getClass()); }
/** * Returns a map of user objects whose options and positional parameters were added to ("mixed in" with) this command. * @return a new Map containing the user objects mixed in with this command. If {@code CommandSpec} objects without * user objects were programmatically added, use the {@link CommandSpec#mixins() underlying model} directly. * @since 3.0 */ public Map<String, Object> getMixins() { Map<String, CommandSpec> mixins = getCommandSpec().mixins(); Map<String, Object> result = new LinkedHashMap<String, Object>(); for (String name : mixins.keySet()) { result.put(name, mixins.get(name).userObject); } return result; }
String indent2 = indent + INDENT_INCREMENT; for (String mixinName : spec.mixins().keySet()) { CommandSpec mixin = spec.mixins().get(mixinName); if ("picocli.CommandLine.AutoHelpMixin".equals(mixin.userObject().getClass().getCanonicalName())) { continue; for (String mixinName : spec.mixins().keySet()) { CommandSpec mixin = spec.mixins().get(mixinName); if (isNestedCommand(mixin, spec) && !isBuiltInMixin(mixin)) { pw.println(); for (String mixinName : spec.mixins().keySet()) { CommandSpec mixin = spec.mixins().get(mixinName); if (isNested(mixin.userObject(), surroundingElement) && !isBuiltInMixin(mixin)) { pw.println();
visitArgSpec(positional); for (CommandSpec mixin : spec.mixins().values()) { visitCommandSpec(mixin);
@Test public void testAddMixinParsesOptionsAndParameters() throws UnsupportedEncodingException { @Command(sortOptions = false) class Receiver { @Option(names = {"-b", "--beta"}, description = "Receiver option") private int beta; @Parameters(description = "parameters from receiver") File[] receiverFiles; } CommandLine commandLine = new CommandLine(new Receiver()); InjectsOptionsAndParameters.MixMeIn mixin = new InjectsOptionsAndParameters.MixMeIn(); commandLine.addMixin("mixin", mixin); commandLine.parse("-a", "111", "-b", "222", "a", "b"); Receiver receiver = commandLine.getCommand(); assertEquals(222, receiver.beta); assertEquals(111, mixin.alpha); assertArrayEquals(new File[] {new File("a"), new File("b")}, receiver.receiverFiles); assertArrayEquals(new File[] {new File("a"), new File("b")}, mixin.files); assertSame(mixin, commandLine.getMixins().get("mixin")); assertSame(mixin, commandLine.getCommandSpec().mixins().get("mixin").userObject()); }
@Test public void testMixinStandardHelpOptions_SettingToTrueAddsHelpOptions() { CommandSpec spec = CommandSpec.create(); assertTrue(spec.mixins().isEmpty()); assertTrue(spec.optionsMap().isEmpty()); assertTrue(spec.posixOptionsMap().isEmpty()); assertTrue(spec.options().isEmpty()); spec.mixinStandardHelpOptions(true); assertFalse(spec.mixins().isEmpty()); assertFalse(spec.optionsMap().isEmpty()); assertFalse(spec.posixOptionsMap().isEmpty()); assertFalse(spec.options().isEmpty()); assertTrue(spec.mixinStandardHelpOptions()); OptionSpec usageHelp = spec.posixOptionsMap().get('h'); assertSame(usageHelp, spec.optionsMap().get("--help")); assertTrue(usageHelp.usageHelp()); OptionSpec versionHelp = spec.posixOptionsMap().get('V'); assertSame(versionHelp, spec.optionsMap().get("--version")); assertTrue(versionHelp.versionHelp()); }
@Test public void testMixinStandardHelpOptions_SettingToFalseRemovesHelpOptions() { CommandSpec spec = CommandSpec.create(); spec.mixinStandardHelpOptions(true); assertFalse(spec.mixins().isEmpty()); assertFalse(spec.optionsMap().isEmpty()); assertFalse(spec.posixOptionsMap().isEmpty()); assertFalse(spec.options().isEmpty()); assertTrue(spec.mixinStandardHelpOptions()); assertNotNull(spec.posixOptionsMap().get('h')); assertNotNull(spec.optionsMap().get("--help")); assertNotNull(spec.posixOptionsMap().get('V')); assertNotNull(spec.optionsMap().get("--version")); spec.mixinStandardHelpOptions(false); assertTrue(spec.mixins().isEmpty()); assertTrue(spec.optionsMap().isEmpty()); assertTrue(spec.posixOptionsMap().isEmpty()); assertTrue(spec.options().isEmpty()); assertFalse(spec.mixinStandardHelpOptions()); }
@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 testMixinAnnotationCanBeRetrievedByAnnotationName() { @Command class MixMeIn {} @Command class Receiver { @Mixin(name = "aMixin") MixMeIn mixMeIn; } CommandLine commandLine = new CommandLine(new Receiver(), new InnerClassFactory(this)); assertFalse("mixin was registered", commandLine.getMixins().isEmpty()); assertTrue(commandLine.getMixins().get("aMixin") instanceof MixMeIn); Receiver receiver = commandLine.getCommand(); assertNotNull(receiver.mixMeIn); assertSame(receiver.mixMeIn, commandLine.getMixins().get("aMixin")); assertSame(receiver.mixMeIn, commandLine.getCommandSpec().mixins().get("aMixin").userObject()); }
@Test public void testMixinAnnotationCanBeRetrievedByFieldName() { @Command class MixMeIn {} @Command class Receiver { @Mixin MixMeIn mixMeIn; } CommandLine commandLine = new CommandLine(new Receiver(), new InnerClassFactory(this)); assertFalse("mixin was registered", commandLine.getMixins().isEmpty()); assertTrue(commandLine.getMixins().get("mixMeIn") instanceof MixMeIn); Receiver receiver = commandLine.getCommand(); assertNotNull(receiver.mixMeIn); assertSame(receiver.mixMeIn, commandLine.getMixins().get("mixMeIn")); assertSame(receiver.mixMeIn, commandLine.getCommandSpec().mixins().get("mixMeIn").userObject()); }
@Test public void testAddMixinMustBeValidCommand_SubCommandMethod() { @Command class ValidMixin { // valid command because it has @Command annotation } @Command class Receiver { @Command void sub(@Mixin ValidMixin mixin) { } } CommandLine commandLine = new CommandLine(new Receiver(), new InnerClassFactory(this)); CommandSpec commandSpec = commandLine.getCommandSpec().subcommands().get("sub").getCommandSpec().mixins().get("arg0"); assertEquals(ValidMixin.class, commandSpec.userObject().getClass()); commandLine.addMixin("valid", new ValidMixin()); // no exception }
private static boolean isMixedIn(OptionSpec option, CommandSpec spec) { for (CommandSpec mixin : spec.mixins().values()) { if (mixin.findOption(option.longestName()) != null) { return true; } } return false; }
@Test public void testAnnotateMethod_mixinParameterFirst() { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinParameters.class, "mixinFirst").get(0); CommandLine cmd = new CommandLine(m); CommandSpec spec = cmd.getCommandSpec(); assertEquals(1, spec.mixins().size()); assertEquals(1, spec.positionalParameters().size()); assertEquals(3, spec.options().size()); spec = spec.mixins().get("arg0"); assertEquals(SomeMixin.class, spec.userObject().getClass()); }
@Test public void testAnnotateMethod_positionalAndMixinParameter() { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinParameters.class, "posAndMixin").get(0); CommandLine cmd = new CommandLine(m); CommandSpec spec = cmd.getCommandSpec(); assertEquals(1, spec.mixins().size()); assertEquals(1, spec.positionalParameters().size()); spec = spec.mixins().get("arg1"); assertEquals(SomeMixin.class, spec.userObject().getClass()); }
@Test public void testAnnotateMethod_positionalAndOptionsAndMixinParameter() { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinParameters.class, "posAndOptAndMixin").get(0); CommandLine cmd = new CommandLine(m); CommandSpec spec = cmd.getCommandSpec(); assertEquals(1, spec.mixins().size()); assertEquals(1, spec.positionalParameters().size()); assertEquals(3, spec.options().size()); spec = spec.mixins().get("arg2"); assertEquals(SomeMixin.class, spec.userObject().getClass()); }
@Test public void testAnnotateMethod_mixinParameter() { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinParameters.class, "withMixin").get(0); CommandLine cmd = new CommandLine(m); CommandSpec spec = cmd.getCommandSpec(); assertEquals(1, spec.mixins().size()); spec = spec.mixins().get("arg0"); assertEquals(SomeMixin.class, spec.userObject().getClass()); }