/** * Create and returns a new CommandLine instance. * This method sets the command name in the usage help message to the script's class simple name (unless * annotated with some other command name with the {@code @Command(name = "...")} annotation). * <p> * Subclasses may override to register custom type converters or programmatically add subcommands. * </p> * * @return A CommandLine instance. */ public CommandLine createCommandLine() { CommandLine commandLine = new CommandLine(this); if (commandLine.getCommandName().equals("<main class>")) { // only if user did not specify @Command(name) attribute commandLine.setCommandName(this.getClass().getSimpleName()); } return commandLine; }
commandName = commandLine.getCommandName(); //new CommandLine.Help(commandLine.commandDescriptor).commandName; if (CommandLine.Help.DEFAULT_COMMAND_NAME.equals(commandName)) { commandName = cls.getSimpleName().toLowerCase();
/** Reflects on the class of the {@linkplain #userObject() user object} and registers any command methods * (class methods annotated with {@code @Command}) as subcommands. * @param factory the factory used to create instances of subcommands, converters, etc., that are registered declaratively with annotation attributes * @return this {@link CommandSpec} object for method chaining * @see #addSubcommand(String, CommandLine) * @since 3.7.0 */ public CommandSpec addMethodSubcommands(IFactory factory) { if (userObject() instanceof Method) { throw new InitializationException("Cannot discover subcommand methods of this Command Method: " + userObject()); } for (Method method : getCommandMethods(userObject().getClass(), null)) { CommandLine cmd = new CommandLine(method, factory); addSubcommand(cmd.getCommandName(), cmd); } isAddMethodSubcommands = true; return this; }
@Test public void test187SetCommandNameOverwritesCommandAnnotationNameAttribute() { @Command(name = "someCommand") class Args { @Parameters String[] args; } assertEquals("someCommand", new CommandLine(new Args()).getCommandName()); String OTHER = "a different name"; assertEquals(OTHER, new CommandLine(new Args()).setCommandName(OTHER).getCommandName()); }
@Test public void test187GetCommandReturnsSubclassName() { @Command(name = "parent") class Parent { } @Command(name = "child") class Child extends Parent { } assertEquals("child", new CommandLine(new Child()).getCommandName()); }
@Test public void test187GetCommandNameReturnsCommandAnnotationNameAttribute() { @Command(name = "someCommand") class Args { @Parameters String[] args; } assertEquals("someCommand", new CommandLine(new Args()).getCommandName()); }
@Test public void test187GetCommandNameReturnsMainClassByDefault() { class Args { @Parameters String[] args; } assertEquals("<main class>", new CommandLine(new Args()).getCommandName()); assertEquals("<main class>", Help.DEFAULT_COMMAND_NAME); }
@SuppressWarnings("deprecation") @Test public void testAnnotateMethod_noArg() throws Exception { setTraceLevel("OFF"); Method m = CommandLine.getCommandMethods(MethodApp.class, "run0").get(0); CommandLine cmd1 = new CommandLine(m); assertEquals("run-0", cmd1.getCommandName()); assertEquals(Arrays.asList(), cmd1.getCommandSpec().args()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); cmd1.parseWithHandler(((IParseResultHandler) null), new PrintStream(baos), new String[]{"--y"}); assertEquals(Arrays.asList("--y"), cmd1.getUnmatchedArguments()); // test execute Object ret = CommandLine.invoke(m.getName(), MethodApp.class, new PrintStream(new ByteArrayOutputStream())); assertNull("return value", ret); setTraceLevel("WARN"); } @Test
@Test public void testCommandMethodsFromSuperclassAddedToSubcommands() throws Exception { CommandLine cmd = new CommandLine(MethodApp.class); assertEquals("method", cmd.getCommandName()); assertEquals(3, cmd.getSubcommands().size()); assertEquals(0, cmd.getSubcommands().get("run-0").getCommandSpec().args().size()); assertEquals(1, cmd.getSubcommands().get("run-1").getCommandSpec().args().size()); assertEquals(2, cmd.getSubcommands().get("run-2").getCommandSpec().args().size()); //CommandLine.usage(cmd.getSubcommands().get("run-2"), System.out); }
tracer.warn("Both '%s' and '%s' are valid option names in %s. Using '%s'...%n", arg, key, getCommandName(), arg); } else if (commandSpec.optionsMap().containsKey(key)) { paramAttachedToOption = true;
@Test public void testAnnotateMethod_matchesAnnotatedClass() throws Exception { setTraceLevel("OFF"); CommandLine classCmd = new CommandLine(new CompactFields()); Method m = CompactFieldsMethod.class.getDeclaredMethod("run", new Class<?>[] {boolean.class, boolean.class, File.class, File[].class}); CommandLine methodCmd = new CommandLine(m); assertEquals("run", methodCmd.getCommandName()); assertEquals("argument count", classCmd.getCommandSpec().args().size(), methodCmd.getCommandSpec().args().size()); for (int i = 0; i < classCmd.getCommandSpec().args().size(); i++) { Model.ArgSpec classArg = classCmd.getCommandSpec().args().get(i); Model.ArgSpec methodArg = methodCmd.getCommandSpec().args().get(i); assertEquals("arg #" + i, classArg, methodArg); } setTraceLevel("WARN"); } /** replicate {@link CommandLineTest#testCompactFieldsAnyOrder()} but using
@Test public void testSubcommandWithoutAnnotationName() { CommandLine top = new CommandLine(new Top563()); top.addSubcommand("subname", new Sub563()); CommandLine sub = top.getSubcommands().get("subname"); assertEquals("subname", sub.getCommandName()); assertEquals("subname", sub.getCommandSpec().name()); assertEquals("<main class> subname", sub.getCommandSpec().qualifiedName()); String expected = String.format("" + "Usage: <main class> subname [-hV]%n" + " -h, --help Show this help message and exit.%n" + " -V, --version Print version information and exit.%n"); sub.usage(System.out); assertEquals(expected, systemOutRule.getLog()); }
@Test public void testSubcommandNameIsInitializedWhenAddedToParent() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create(); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); CommandLine subCommandLine = new CommandLine(sub); assertEquals("<main class>", sub.name()); assertEquals("<main class>", subCommandLine.getCommandName()); commandLine.addSubcommand("sub", subCommandLine); assertEquals("sub", sub.name()); assertEquals("sub", subCommandLine.getCommandName()); subCommandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> sub [-x]%n" + " -x x option%n"); assertEquals(expected, systemOutRule.getLog()); }
@Test public void testSubcommandNameNotOverwrittenWhenAddedToParent() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create().name("SOMECOMMAND"); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); CommandLine subCommandLine = new CommandLine(sub); assertEquals("SOMECOMMAND", sub.name()); assertEquals("SOMECOMMAND", subCommandLine.getCommandName()); commandLine.addSubcommand("sub", subCommandLine); assertEquals("SOMECOMMAND", sub.name()); assertEquals("SOMECOMMAND", subCommandLine.getCommandName()); subCommandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> SOMECOMMAND [-x]%n" + " -x x option%n"); assertEquals(expected, systemOutRule.getLog()); }
/** Reflects on the class of the {@linkplain #userObject() user object} and registers any command methods * (class methods annotated with {@code @Command}) as subcommands. * @param factory the factory used to create instances of subcommands, converters, etc., that are registered declaratively with annotation attributes * @return this {@link CommandSpec} object for method chaining * @see #addSubcommand(String, CommandLine) * @since 3.7.0 */ public CommandSpec addMethodSubcommands(IFactory factory) { if (userObject() instanceof Method) { throw new InitializationException("Cannot discover subcommand methods of this Command Method: " + userObject()); } for (Method method : getCommandMethods(userObject().getClass(), null)) { CommandLine cmd = new CommandLine(method, factory); addSubcommand(cmd.getCommandName(), cmd); } return this; }
@Test public void test187GetCommandReturnsSubclassName() { @Command(name = "parent") class Parent { } @Command(name = "child") class Child extends Parent { } assertEquals("child", new CommandLine(new Child()).getCommandName()); }
@Test public void test187GetCommandNameReturnsCommandAnnotationNameAttribute() { @Command(name = "someCommand") class Args { @Parameters String[] args; } assertEquals("someCommand", new CommandLine(new Args()).getCommandName()); }
@Test public void test187SetCommandNameOverwritesCommandAnnotationNameAttribute() { @Command(name = "someCommand") class Args { @Parameters String[] args; } assertEquals("someCommand", new CommandLine(new Args()).getCommandName()); String OTHER = "a different name"; assertEquals(OTHER, new CommandLine(new Args()).setCommandName(OTHER).getCommandName()); }
@Test public void test187GetCommandNameReturnsMainClassByDefault() { class Args { @Parameters String[] args; } assertEquals("<main class>", new CommandLine(new Args()).getCommandName()); assertEquals("<main class>", Help.DEFAULT_COMMAND_NAME); }
@Test public void testCommandMethodsFromSuperclassAddedToSubcommands() throws Exception { CommandLine cmd = new CommandLine(MethodApp.class); assertEquals("method", cmd.getCommandName()); assertEquals(3, cmd.getSubcommands().size()); assertEquals(0, cmd.getSubcommands().get("run-0").getCommandSpec().args().size()); assertEquals(1, cmd.getSubcommands().get("run-1").getCommandSpec().args().size()); assertEquals(2, cmd.getSubcommands().get("run-2").getCommandSpec().args().size()); //CommandLine.usage(cmd.getSubcommands().get("run-2"), System.out); }