@Command int multiply(@Option(names = "--count") int count, @Parameters int multiplier) { System.out.println("Result is " + count * multiplier); return count * multiplier; }
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) {
mixinStandardHelpOptions |= cmd.mixinStandardHelpOptions(); if (cls.getAnnotation(Command.class) != null && cls.getAnnotation(Command.class).addMethodSubcommands()) { CommandSpec commandSpec = buildCommand(cls, factory, commands, commandTypes, options, parameters); result.mixinStandardHelpOptions(method.getAnnotation(Command.class).mixinStandardHelpOptions()); buildOptionsAndPositionalsFromMethodParameters(method, result, factory, options, parameters);
private static void initSubcommands(Command cmd, CommandSpec parent, IFactory factory) { for (Class<?> sub : cmd.subcommands()) { try { if (Help.class == sub) { throw new InitializationException(Help.class.getName() + " is not a valid subcommand. Did you mean " + HelpCommand.class.getName() + "?"); } CommandLine subcommandLine = toCommandLine(factory.create(sub), factory); parent.addSubcommand(subcommandName(sub), subcommandLine); initParentCommand(subcommandLine.getCommandSpec().userObject(), parent.userObject()); } catch (InitializationException ex) { throw ex; } catch (NoSuchMethodException ex) { throw new InitializationException("Cannot instantiate subcommand " + sub.getName() + ": the class has no constructor", ex); } catch (Exception ex) { throw new InitializationException("Could not instantiate and add subcommand " + sub.getName() + ": " + ex, ex); } } if (cmd.addMethodSubcommands() && !(parent.userObject() instanceof Method)) { parent.addMethodSubcommands(factory); } } static void initParentCommand(Object subcommand, Object parent) {
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) {
private static void initSubcommands(Command cmd, CommandSpec parent, IFactory factory) { for (Class<?> sub : cmd.subcommands()) { try { if (Help.class == sub) { throw new InitializationException(Help.class.getName() + " is not a valid subcommand. Did you mean " + HelpCommand.class.getName() + "?"); } CommandLine subcommandLine = toCommandLine(factory.create(sub), factory); parent.addSubcommand(subcommandName(sub), subcommandLine); initParentCommand(subcommandLine.getCommandSpec().userObject(), parent.userObject()); } catch (InitializationException ex) { throw ex; } catch (NoSuchMethodException ex) { throw new InitializationException("Cannot instantiate subcommand " + sub.getName() + ": the class has no constructor", ex); } catch (Exception ex) { throw new InitializationException("Could not instantiate and add subcommand " + sub.getName() + ": " + ex, ex); } } if (cmd.addMethodSubcommands() && !(parent.userObject() instanceof Method)) { parent.addMethodSubcommands(factory); } } static void initParentCommand(Object subcommand, Object parent) {
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); } } }
/** * 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); } }
/** * Top-level command that just prints help. */ @Command(name = "", description = "Example interactive shell with completion", footer = {"", "Press Ctl-D to exit."}, subcommands = {MyCommand.class, ClearScreen.class}) static class CliCommands implements Runnable { LineReaderImpl reader; PrintWriter out; CliCommands() {} public void setReader(LineReader reader){ this.reader = (LineReaderImpl)reader; out = reader.getTerminal().writer(); } public void run() { out.println(new CommandLine(this).getUsageMessage()); } }
/** * Command "stop" that is used to stop TensorFlow cluster. */ @CommandLine.Command( name = "stop", description = "Stops a running TensorFlow cluster.", mixinStandardHelpOptions = true ) public class StopCommand extends AbstractCommand { /** Cluster identifier. */ @CommandLine.Parameters(paramLabel = "CLUSTER_ID", description = "Cluster identifier.") private UUID clusterId; /** {@inheritDoc} */ @Override public void run() { try (Ignite ignite = getIgnite()) { TensorFlowClusterGatewayManager mgr = new TensorFlowClusterGatewayManager(ignite); mgr.stopClusterIfExists(clusterId); } } /** */ public void setClusterId(UUID clusterId) { this.clusterId = clusterId; } }
@Command(name = "mixmein", version = "Mixin 1.0", separator = ":", description = "description from mixin", descriptionHeading = "Mixin Description Heading%n", header = "Mixin Header", headerHeading = "Mixin Header Heading%n", footer = "Mixin Footer", footerHeading = "Mixin Footer Heading%n", optionListHeading = "Mixin Option List Heading%n", parameterListHeading = "Mixin Parameter List Heading%n", commandListHeading = "Mixin Command List Heading%n", requiredOptionMarker = '%', synopsisHeading = "Mixin Synopsis Heading%n", abbreviateSynopsis = true, customSynopsis = "Mixin custom synopsis", showDefaultValues = true, sortOptions = false) static class MixMeIn {} }
/** * @author keli.wang * @since 2018-12-05 */ @Command(name = "ListSubjectRoutes", mixinStandardHelpOptions = true, sortOptions = false) public class ListSubjectRoutesCommand implements Runnable { private final MetaManagementService service; @Option(names = {"--metaserver"}, required = true, description = {"meta server address, format: <host> or <host>:<port>"}) private String metaserver; public ListSubjectRoutesCommand(final MetaManagementService service) { this.service = service; } @Override public void run() { final HashMap<String, String> params = new HashMap<>(); params.put("action", "ListSubjectRoutes"); System.out.println(service.post(metaserver, "", params)); } }
/** * @author keli.wang * @since 2018-12-05 */ @Command(name = "ListBrokerGroups", mixinStandardHelpOptions = true, sortOptions = false) public class ListBrokerGroupsCommand implements Runnable { private final MetaManagementService service; @Option(names = {"--metaserver"}, required = true, description = {"meta server address, format: <host> or <host>:<port>"}) private String metaserver; public ListBrokerGroupsCommand(final MetaManagementService service) { this.service = service; } @Override public void run() { final HashMap<String, String> params = new HashMap<>(); params.put("action", "ListBrokerGroups"); System.out.println(service.post(metaserver, "", params)); } }
@Command(name = "mixmein", version = "Mixin 1.0", separator = ":", description = "description from mixin", descriptionHeading = "Mixin Description Heading%n", header = "Mixin Header", headerHeading = "Mixin Header Heading%n", footer = "Mixin Footer", footerHeading = "Mixin Footer Heading%n", optionListHeading = "Mixin Option List Heading%n", parameterListHeading = "Mixin Parameter List Heading%n", commandListHeading = "Mixin Command List Heading%n", requiredOptionMarker = '%', synopsisHeading = "Mixin Synopsis Heading%n", abbreviateSynopsis = true, customSynopsis = "Mixin custom synopsis", showDefaultValues = true, sortOptions = false) static class MixMeInSuper {}