private static String subcommandName(Class<?> sub) { Command subCommand = sub.getAnnotation(Command.class); if (subCommand == null || Help.DEFAULT_COMMAND_NAME.equals(subCommand.name())) { throw new InitializationException("Subcommand " + sub.getName() + " is missing the mandatory @Command annotation with a 'name' attribute"); } return subCommand.name(); } private static boolean initFromAnnotatedFields(Object scope, Class<?> cls, CommandSpec receiver, IFactory factory) {
private boolean isValidSubcommandHasNameAttribute(Element subcommandElement) { Command annotation = subcommandElement.getAnnotation(Command.class); if (annotation == null) { error(subcommandElement, "Subcommand is missing @Command annotation with a name attribute"); return false; } else if (COMMAND_DEFAULT_NAME.equals(annotation.name())) { error(subcommandElement, "Subcommand @Command annotation should have a name attribute"); return false; } return true; }
private List<Object> getMainCommands(Collection<Object> candidates) { List<Object> mainCommands = new ArrayList<>(); for (Object candidate : candidates) { Class<?> clazz = AopUtils.getTargetClass(candidate); Method method = ReflectionUtils.findMethod(Command.class, "name"); if (clazz.isAnnotationPresent(Command.class) && method != null && clazz.getAnnotation(Command.class).name().equals(method.getDefaultValue())) { mainCommands.add(candidate); } } return mainCommands; }
public static CommandLine toCommandLine(Object command) { CommandLine cmd = new CommandLine(command); if (command instanceof SubCommandContainer) { Collection<Object> subcommands = ((SubCommandContainer) command).getSubCommands(); for (Object subcommand : subcommands) { Command commandAnnotation = subcommand.getClass().getAnnotation(Command.class); if (subcommand instanceof SubCommandContainer) subcommand = toCommandLine(subcommand); cmd.addSubcommand(commandAnnotation.name(), subcommand); } } return cmd; }
private String getCommandName(Class<?> commandClass) { if (commandClass == null) { return null; } return commandClass.getAnnotation(Command.class).name(); }
protected void underConstruction() { Command command = this.getClass().getAnnotation(Command.class); System.out.println("Command " + command.name() + " is under construction"); } }
private static boolean updateCommandAttributes(Command cmd, CommandSpec commandSpec, IFactory factory) { commandSpec.aliases(cmd.aliases()); commandSpec.parser().updateSeparator(cmd.separator()); commandSpec.updateName(cmd.name()); commandSpec.updateVersion(cmd.version()); commandSpec.updateHelpCommand(cmd.helpCommand()); commandSpec.updateVersionProvider(cmd.versionProvider(), factory); commandSpec.initDefaultValueProvider(cmd.defaultValueProvider(), factory); commandSpec.usageMessage().updateFromCommand(cmd, commandSpec); initSubcommands(cmd, commandSpec, factory); return true; } private static void initSubcommands(Command cmd, CommandSpec parent, IFactory factory) {
private String getCommandName(Object command) { if (command == null) { return null; } return AopUtils.getTargetClass(command).getAnnotation(Command.class).name(); }
private static String subcommandName(Class<?> sub) { Command subCommand = sub.getAnnotation(Command.class); if (subCommand == null || Help.DEFAULT_COMMAND_NAME.equals(subCommand.name())) { throw new InitializationException("Subcommand " + sub.getName() + " is missing the mandatory @Command annotation with a 'name' attribute"); } return subCommand.name(); } private static boolean initFromAnnotatedFields(Object scope, Class<?> cls, CommandSpec receiver, IFactory factory) {
/** * Updates the following attributes from the specified {@code @Command} annotation: * aliases, {@link ParserSpec#separator() parser separator}, command name, version, help command, * version provider, default provider and {@link UsageMessageSpec usage message spec}. * @param cmd the {@code @Command} annotation to get attribute values from * @param factory factory used to instantiate classes * @since 3.7 */ public void updateCommandAttributes(Command cmd, IFactory factory) { aliases(cmd.aliases()); parser().updateSeparator(cmd.separator()); updateName(cmd.name()); updateVersion(cmd.version()); updateHelpCommand(cmd.helpCommand()); updateAddMethodSubcommands(cmd.addMethodSubcommands()); usageMessage().updateFromCommand(cmd, this); if (factory != null) { updateVersionProvider(cmd.versionProvider(), factory); initDefaultValueProvider(cmd.defaultValueProvider(), factory); } }