public void run() { for (Class<?> cls : classes) { StringWriter sw = new StringWriter(); new CommandSpecYamlPrinter().print(CommandSpec.forAnnotatedObject(cls), new PrintWriter(sw)); System.out.println(sw); } } }
/** Sets whether the standard help options should be mixed in with this command. * @return this CommandSpec for method chaining * @see Command#mixinStandardHelpOptions() */ public CommandSpec mixinStandardHelpOptions(boolean newValue) { if (newValue) { CommandSpec mixin = CommandSpec.forAnnotatedObject(new AutoHelpMixin(), new DefaultFactory()); addMixin(AutoHelpMixin.KEY, mixin); } else { CommandSpec helpMixin = mixins.remove(AutoHelpMixin.KEY); if (helpMixin != null) { options.removeAll(helpMixin.options); for (OptionSpec option : helpMixin.options()) { for (String name : option.names) { optionsByNameMap.remove(name); if (name.length() == 2 && name.startsWith("-")) { posixOptionsByKeyMap.remove(name.charAt(1)); } } } } } return this; }
/** Constructs a new {@code Help} instance with the specified color scheme, initialized from annotatations * on the specified class and superclasses. * @param command the annotated object to create usage help for * @param colorScheme the color scheme to use * @deprecated use {@link picocli.CommandLine.Help#Help(picocli.CommandLine.Model.CommandSpec, picocli.CommandLine.Help.ColorScheme)} */ @Deprecated public Help(Object command, ColorScheme colorScheme) { this(CommandSpec.forAnnotatedObject(command, new DefaultFactory()), colorScheme); } /** Constructs a new {@code Help} instance with the specified color scheme, initialized from annotatations
private static CommandSpec buildMixinForField(IAnnotatedElement member, IFactory factory) { try { Object userObject = member.getter().get(); if (userObject == null) { userObject = factory.create(member.getTypeInfo().getType()); member.setter().set(userObject); } CommandSpec result = CommandSpec.forAnnotatedObject(userObject, factory); return result.withToString(member.getToString()); } catch (InitializationException ex) { throw ex; } catch (Exception ex) { throw new InitializationException("Could not access or modify mixin member " + member + ": " + ex, ex); } } private static UnmatchedArgsBinding buildUnmatchedForField(final IAnnotatedElement member) {
static void print(Object userObject) { print(CommandSpec.forAnnotatedObject(userObject)); }
/** * Adds the options and positional parameters in the specified mixin to this command. * <p>The specified object may be a {@link CommandSpec CommandSpec} object, or it may be a user object with * {@code @Option} and {@code @Parameters}-annotated fields, in which case picocli automatically * constructs a {@code CommandSpec} from this user object. * </p> * @param name the name by which the mixin object may later be retrieved * @param mixin an annotated user object or a {@link CommandSpec CommandSpec} object whose options and positional parameters to add to this command * @return this CommandLine object, to allow method chaining * @since 3.0 */ public CommandLine addMixin(String name, Object mixin) { getCommandSpec().addMixin(name, CommandSpec.forAnnotatedObject(mixin, factory)); return this; }
@Test(expected = InitializationException.class) public void testCommandReflection_buildUnmatchedForField_raw() { CommandSpec.forAnnotatedObject(new MyUnmatched()); }
@Test public void testBuildMixinForField_invalid() { CommandLine.IFactory myFactory = new CommandLine.IFactory() { public <K> K create(Class<K> cls) { throw new IllegalStateException("boom"); } }; try { CommandSpec.forAnnotatedObject(new MixeeUninstantiated(), myFactory); } catch (InitializationException ex) { assertEquals("Could not access or modify mixin member picocli.ModelCommandReflectionTest$ValidMixin picocli.ModelCommandReflectionTest$MixeeUninstantiated.mixin: java.lang.IllegalStateException: boom", ex.getMessage()); } }
@Test public void testCompleteFindPositionalForTopLevelCommand() { class App { @Parameters() List<Possibilities> poss; } CommandSpec spec = CommandSpec.forAnnotatedObject(new App()); int cur = 500; test(spec, a(), 0, 0, cur, l("Aaa", "Bbb", "Ccc")); test(spec, a("A"), 0, 0, cur, l("Aaa", "Bbb", "Ccc")); test(spec, a("A"), 0, 1, cur, l("aa")); test(spec, a("Aaa"), 1, 0, cur, l("Aaa", "Bbb", "Ccc")); test(spec, a("Aaa", "Bbb"), 2, 0, cur, l("Aaa", "Bbb", "Ccc")); }
@Test public void testCompleteFindCompletionStartPoint() { class App { @Option(names = "-x", arity = "2") List<Possibilities> poss; } CommandSpec spec = CommandSpec.forAnnotatedObject(new App()); int cur = 500; test(spec, a("-x"), 1, 0, cur, l("Aaa", "Bbb", "Ccc")); test(spec, a("-x", "A"), 1, 0, cur, l("Aaa", "Bbb", "Ccc")); // suggest 1st arg of same type test(spec, a("-x", "A"), 1, 1, cur, l("aa")); test(spec, a("-x", "Aaa"), 2, 0, cur, l("Aaa", "Bbb", "Ccc")); // suggest 2nd arg of same type test(spec, a("-x", "Aaa", "Bbb"), 3, 0, cur, l("-x")); // we have 2 args for first -x. Suggest -x again. }
@Test public void testCommandSpec_forAnnotatedObject_requiresPicocliAnnotation() { try { CommandSpec.forAnnotatedObject(new Object()); fail("Expected error"); } catch (InitializationException ok) { assertEquals("java.lang.Object is not a command: it has no @Command, @Option, @Parameters or @Unmatched annotations", ok.getMessage()); } }
/** Registers the specified subcommand with this Help. * @param commandName the name of the subcommand to display in the usage message * @param command the {@code CommandSpec} or {@code @Command} annotated object to get more information from * @return this Help instance (for method chaining) * @deprecated */ @Deprecated public Help addSubcommand(String commandName, Object command) { commands.put(commandName, getHelpFactory().create(CommandSpec.forAnnotatedObject(command, commandSpec.commandLine().factory), defaultColorScheme(Ansi.AUTO))); return this; }
/** Creates and returns a new {@code CommandSpec} initialized from the specified associated user object. The specified * user object must have at least one {@link Command}, {@link Option} or {@link Parameters} annotation. * @param userObject the user object annotated with {@link Command}, {@link Option} and/or {@link Parameters} annotations. * @throws InitializationException if the specified object has no picocli annotations or has invalid annotations */ public static CommandSpec forAnnotatedObject(Object userObject) { return forAnnotatedObject(userObject, new DefaultFactory()); }
@Test public void testBuildUnmatchedForField_valid() { CommandSpec.forAnnotatedObject(new MyUnmatched2()); } }
@Test public void testInitSubcommands() { try { CommandSpec.forAnnotatedObject(InvalidTop.class); } catch (InitializationException ex) { assertEquals("Cannot instantiate subcommand picocli.ModelCommandReflectionTest$InvalidSub: the class has no constructor", ex.getMessage()); } }
@Ignore @Test public void generate() { CommandSpec spec = CommandSpec.forAnnotatedObject(Example.class); String generated = new AnnotatedCommandSourceGenerator(spec).generate(); //System.out.println(generated); String expected = Resources.slurp("/picocli/codegen/aot/graalvm/Example.txt"); assertEquals(expected, generated); } }
@Test public void testUsageHelp_emptyWithAutoHelpMixin() { CommandSpec spec = CommandSpec.create().addMixin("auto", CommandSpec.forAnnotatedObject(new AutoHelpMixin())); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-hV]%n" + " -h, --help Show this help message and exit.%n" + " -V, --version Print version information and exit.%n"); assertEquals(expected, actual); }
@Test public void testBuildMixinForField_valid() { CommandSpec commandSpec = CommandSpec.forAnnotatedObject(new MixeeInstantiated()); assertNotNull(commandSpec.findOption("h")); }
@Test public void testSubcommandName() { try { CommandSpec.forAnnotatedObject(InvalidTop2.class); } catch (InitializationException ex) { assertEquals("Subcommand picocli.ModelCommandReflectionTest$InvalidSub2 is missing the mandatory @Command annotation with a 'name' attribute", ex.getMessage()); } }
@Test public void testInferTypes() { class App { @CommandLine.Parameters List<Class<? extends Class<? extends String>[]>> list; } assertEquals("<list>", CommandLine.Model.CommandSpec.forAnnotatedObject(new App()).positionalParameters().get(0).paramLabel()); } }