com.beust.jcommander.Parameters Java Examples

The following examples show how to use com.beust.jcommander.Parameters. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomJCommander.java    From terracotta-platform with Apache License 2.0 6 votes vote down vote up
private void appendCommands(StringBuilder out, String indent, Map<String, JCommander> commands, boolean hasCommands) {
  if (hasCommands) {
    out.append(lineSeparator()).append("Commands:").append(lineSeparator());
    for (Map.Entry<String, JCommander> command : commands.entrySet()) {
      Object arg = command.getValue().getObjects().get(0);
      Parameters p = arg.getClass().getAnnotation(Parameters.class);
      String name = command.getKey();
      if (p == null || !p.hidden()) {
        String description = getCommandDescription(name);
        out.append(indent).append("    ").append(name).append("      ").append(description).append(lineSeparator());
        appendUsage(commandRepository.getCommand(name), out, indent + "    ");

        // Options for this command
        JCommander jc = command.getValue();
        appendOptions(jc, out, "    ");
        out.append(lineSeparator());
      }
    }
  }
}
 
Example #2
Source File: CommandFactory.java    From genie with Apache License 2.0 6 votes vote down vote up
CommandFactory(
    final List<AgentCommandArguments> agentCommandArguments,
    final ApplicationContext applicationContext
) {
    this.agentCommandArgumentsBeans = agentCommandArguments;
    this.applicationContext = applicationContext;

    agentCommandArguments.forEach(
        commandArgs -> {
            Sets.newHashSet(commandArgs.getClass().getAnnotation(Parameters.class).commandNames()).forEach(
                commandName -> {
                    commandMap.put(commandName, commandArgs.getConsumerClass());
                }
            );
        }
    );
}
 
Example #3
Source File: AbstractCmd.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
/**
 * Extract command description from the jCommander instance and append it to the StringBuilder.
 *
 * @param jCommander respective JCommander instance.
 * @param sb         StringBuilder instance.
 */
static void appendCommandDescription(JCommander jCommander, StringBuilder sb) {
    MBClientCmd selfCommand = (MBClientCmd) jCommander.getObjects().get(0);

    // if no description is provided, return from here
    if (selfCommand.getClass().getAnnotations().length == 0) {
        return;
    }

    Parameters parameters = (Parameters) selfCommand.getClass().getAnnotations()[0];
    String commandDescription = parameters.commandDescription();
    sb.append(commandDescription);
    sb.append("\n\n");
}
 
Example #4
Source File: CommandSupport.java    From updatebot with Apache License 2.0 5 votes vote down vote up
protected void appendPullRequestComment(StringBuilder builder) {
    builder.append(COMMAND_COMMENT_INDENT);

    Parameters annotation = getClass().getAnnotation(Parameters.class);
    if (annotation != null) {
        String[] commandNames = annotation.commandNames();
        if (commandNames != null && commandNames.length > 0) {
            builder.append(commandNames[0]);
        }
    }
    appendPullRequestCommentArguments(builder);
    builder.append("\n");
}
 
Example #5
Source File: ToolsTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
@junitparams.Parameters(method = "getToolCommandMap")
@TestCaseName("{method}_{0}")
public void test_commandMap_namesAreInAlphabeticalOrder(
    String toolName, ImmutableMap<String, Class<? extends Command>> commandMap) {
  assertThat(commandMap.keySet()).isInStrictOrder();
}
 
Example #6
Source File: ToolsTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
@junitparams.Parameters(method = "getToolCommandMap")
@TestCaseName("{method}_{0}")
public void test_commandMap_namesAreDerivedFromClassNames(
    String toolName, ImmutableMap<String, Class<? extends Command>> commandMap) {
  for (Map.Entry<String, ? extends Class<? extends Command>> commandEntry :
      commandMap.entrySet()) {
    String className = commandEntry.getValue().getSimpleName();
    expect.that(commandEntry.getKey())
        // JCommander names should match the class name, up to "Command" and case formatting.
        .isEqualTo(UPPER_CAMEL.to(LOWER_UNDERSCORE, className.replaceFirst("Command$", "")));
  }
}
 
Example #7
Source File: ToolsTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
@junitparams.Parameters(method = "getToolCommandMap")
@TestCaseName("{method}_{0}")
public void test_commandMap_allCommandsHaveDescriptions(
    String toolName, ImmutableMap<String, Class<? extends Command>> commandMap) {
  for (Map.Entry<String, ? extends Class<? extends Command>> commandEntry :
      commandMap.entrySet()) {
    Parameters parameters = commandEntry.getValue().getAnnotation(Parameters.class);
    assertThat(parameters).isNotNull();
    assertThat(parameters.commandDescription()).isNotEmpty();
  }
}
 
Example #8
Source File: Metadata.java    From terracotta-platform with Apache License 2.0 5 votes vote down vote up
public static String getName(Command command) {
  Parameters annotation = command.getClass().getAnnotation(Parameters.class);
  if (annotation != null && annotation.commandNames().length > 0) {
    return annotation.commandNames()[0];
  }
  return command.getClass().getSimpleName().toLowerCase().replace("command", "");
}
 
Example #9
Source File: Command.java    From mojito with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the {@link Parameters} annotation from the command.
 *
 * @return the {@link Parameters} annotation
 */
Parameters getParameters() {
    Parameters parameters = this.getClass().getAnnotation(Parameters.class);
    Preconditions.checkNotNull(parameters, "There must be @Parameters on the Command class: " + this.getClass());
    return parameters;
}
 
Example #10
Source File: HelpCommand.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(final OperationParams inputParams) {
  final CommandLineOperationParams params = (CommandLineOperationParams) inputParams;

  final List<String> nameArray = new ArrayList<>();
  final OperationRegistry registry = OperationRegistry.getInstance();

  StringBuilder builder = new StringBuilder();

  Operation lastOperation = null;
  for (final Map.Entry<String, Operation> entry : params.getOperationMap().entrySet()) {
    if (entry.getValue() == this) {
      continue;
    }
    nameArray.add(entry.getKey());
    lastOperation = entry.getValue();
  }

  if (lastOperation == null) {
    lastOperation = registry.getOperation(GeoWaveTopLevelSection.class).createInstance();
  }
  if (lastOperation != null) {
    final String usage = lastOperation.usage();
    if (usage != null) {
      System.out.println(usage);
    } else {
      // This is done because if we don't, then JCommander will
      // consider the given parameters as the Default parameters.
      // It's also done so that we can parse prefix annotations
      // and special delegate processing.
      final JCommanderPrefixTranslator translator = new JCommanderPrefixTranslator();

      translator.addObject(lastOperation);
      final JCommanderTranslationMap map = translator.translate();
      map.createFacadeObjects();

      // Copy default parameters over for help display.
      map.transformToFacade();

      // Execute a prepare

      // Add processed objects
      final JCommander jc = new JCommander();
      for (final Object obj : map.getObjects()) {
        jc.addObject(obj);
      }

      final String programName = StringUtils.join(nameArray, " ");
      jc.setProgramName(programName);
      jc.getUsageFormatter().usage(builder);

      // Trim excess newlines.
      final String operations = builder.toString().trim();
      builder = new StringBuilder();
      builder.append(operations);
      builder.append("\n\n");

      // Add sub-commands
      final OperationEntry lastEntry = registry.getOperation(lastOperation.getClass());
      // Cast to list so we can sort it based on operation name.
      final List<OperationEntry> children = new ArrayList<>(lastEntry.getChildren());
      Collections.sort(children, getOperationComparator());
      if (children.size() > 0) {
        builder.append("  Commands:\n");
        for (final OperationEntry childEntry : children) {

          // Get description annotation
          final Parameters p = childEntry.getOperationClass().getAnnotation(Parameters.class);

          // If not hidden, then output it.
          if ((p == null) || !p.hidden()) {
            builder.append(
                String.format(
                    "    %s%n",
                    StringUtils.join(childEntry.getOperationNames(), ", ")));
            if (p != null) {
              final String description = p.commandDescription();
              builder.append(String.format("      %s%n", description));
            } else {
              builder.append("      <no description>\n");
            }
            builder.append("\n");
          }
        }
      }

      // Trim excess newlines.
      final String output = builder.toString().trim();

      System.out.println(output);
    }
  }
}
 
Example #11
Source File: CommandHandler.java    From Cardshifter with Apache License 2.0 4 votes vote down vote up
public String getDescription() {
	Parameters params = supplier.get().getClass().getAnnotation(Parameters.class);
	return params == null ? "(No description)" : params.commandDescription();
}
 
Example #12
Source File: CliTool.java    From morfologik-stemming with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CliTool() {
  if (!getClass().isAnnotationPresent(Parameters.class)) {
    throw new RuntimeException();
  }
}
 
Example #13
Source File: Command.java    From mojito with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the names of this command (should be long name first followed by
 * short name).
 *
 * @return list of command names
 */
public List<String> getNames() {
    Parameters parameters = getParameters();
    String[] commandNames = parameters.commandNames();
    return Arrays.asList(commandNames);
}