Java Code Examples for com.beust.jcommander.JCommander#Builder

The following examples show how to use com.beust.jcommander.JCommander#Builder . 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: CommandLine.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
private static JCommander prepareParser(CliOptions options) {
  JCommander.Builder builder = JCommander.newBuilder()
      .programName(SchedulerMain.class.getName());

  builder.addConverterFactory(new IStringConverterFactory() {
    private Map<Class<?>, Class<? extends IStringConverter<?>>> classConverters =
        ImmutableMap.<Class<?>, Class<? extends IStringConverter<?>>>builder()
            .put(Class.class, ClassConverter.class)
            .put(DataAmount.class, DataAmountConverter.class)
            .put(DockerParameter.class, DockerParameterConverter.class)
            .put(InetSocketAddress.class, InetSocketAddressConverter.class)
            .put(KerberosPrincipal.class, KerberosPrincipalConverter.class)
            .put(TimeAmount.class, TimeAmountConverter.class)
            .put(Volume.class, VolumeConverter.class)
            .build();

    @SuppressWarnings("unchecked")
    @Override
    public <T> Class<? extends IStringConverter<T>> getConverter(Class<T> forType) {
      return (Class<IStringConverter<T>>) classConverters.get(forType);
    }
  });

  builder.addObject(getOptionsObjects(options));
  return builder.build();
}
 
Example 2
Source File: RecoveryTool.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
private static JCommander configure(Options options, String... args) {
  JCommander.Builder builder = JCommander.newBuilder().programName(RecoveryTool.class.getName());
  builder.addConverterFactory(new IStringConverterFactory() {
    private Map<Class<?>, Class<? extends IStringConverter<?>>> classConverters =
        ImmutableMap.<Class<?>, Class<? extends IStringConverter<?>>>builder()
            .put(DataAmount.class, DataAmountConverter.class)
            .put(InetSocketAddress.class, InetSocketAddressConverter.class)
            .put(TimeAmount.class, TimeAmountConverter.class)
            .build();

    @SuppressWarnings("unchecked")
    @Override
    public <T> Class<? extends IStringConverter<T>> getConverter(Class<T> forType) {
      return (Class<IStringConverter<T>>) classConverters.get(forType);
    }
  });

  builder.addObject(options);
  for (Endpoint endpoint : Endpoint.values()) {
    endpoint.impl.getOptions().forEach(builder::addObject);
  }

  JCommander parser = builder.build();
  parser.parse(args);
  return parser;
}
 
Example 3
Source File: BossCommand.java    From openmessaging-storage-dledger with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    Map<String, BaseCommand> commands = new HashMap<>();
    commands.put("append", new AppendCommand());
    commands.put("get", new GetCommand());
    commands.put("readFile", new ReadFileCommand());
    commands.put("leadershipTransfer", new LeadershipTransferCommand());

    JCommander.Builder builder = JCommander.newBuilder();
    builder.addCommand("server", new DLedgerConfig());
    for (String cmd : commands.keySet()) {
        builder.addCommand(cmd, commands.get(cmd));
    }
    JCommander jc = builder.build();
    jc.parse(args);

    if (jc.getParsedCommand() == null) {
        jc.usage();
    } else if (jc.getParsedCommand().equals("server")) {
        String[] subArgs = new String[args.length - 1];
        System.arraycopy(args, 1, subArgs, 0, subArgs.length);
        DLedger.main(subArgs);
    } else {
        BaseCommand command = commands.get(jc.getParsedCommand());
        if (command != null) {
            command.doCommand();
        } else {
            jc.usage();
        }
    }
}
 
Example 4
Source File: DLedgerConfigTest.java    From openmessaging-storage-dledger with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseArguments() {
    DLedgerConfig dLedgerConfig = new DLedgerConfig();
    JCommander.Builder builder = JCommander.newBuilder().addObject(dLedgerConfig);
    JCommander jc = builder.build();
    jc.parse("-i", "n1", "-g", "test", "-p", "n1-localhost:21911", "-s", "/tmp");
    Assert.assertEquals("n1", dLedgerConfig.getSelfId());
    Assert.assertEquals("test", dLedgerConfig.getGroup());
    Assert.assertEquals("n1-localhost:21911", dLedgerConfig.getPeers());
    Assert.assertEquals("/tmp", dLedgerConfig.getStoreBaseDir());
}
 
Example 5
Source File: Runner.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length == 0) {
    System.err.println("No command type specified: ");
    return;
  }
  List<SubCommandBase> commands = initializeCommands(args[0]);
  Runner runner = new Runner();

  if (commands == null) {
    System.err.println("Wrong command type: " + args[0]);
    return;
  }
  String[] newArgs = new String[args.length - 1];
  System.arraycopy(args, 1, newArgs, 0, args.length - 1);

  JCommander.Builder builder = JCommander.newBuilder().addObject(runner);
  commands.forEach(command -> builder
      .addCommand(command.getClass().getSimpleName().toLowerCase(), command));
  JCommander jc = builder.build();
  try {
    jc.parse(newArgs);
    Optional<SubCommandBase> selectedCommand = commands.stream().filter(
        command -> command.getClass().getSimpleName()
            .equalsIgnoreCase(jc.getParsedCommand())).findFirst();
    if (selectedCommand.isPresent()) {
      selectedCommand.get().run();
    } else {
      jc.usage();
    }
  } catch (ParameterException exception) {
    System.err.println("Wrong parameters: " + exception.getMessage());
    jc.usage();
  }

}
 
Example 6
Source File: Runner.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length == 0) {
    System.err.println("No command type specified: ");
    return;
  }
  List<SubCommandBase> commands = initializeCommands(args[0]);
  Runner runner = new Runner();

  if (commands == null) {
    System.err.println("Wrong command type: " + args[0]);
    return;
  }
  String[] newArgs = new String[args.length - 1];
  System.arraycopy(args, 1, newArgs, 0, args.length - 1);

  JCommander.Builder builder = JCommander.newBuilder().addObject(runner);
  commands.forEach(command -> builder
      .addCommand(command.getClass().getSimpleName().toLowerCase(), command));
  JCommander jc = builder.build();
  try {
    jc.parse(newArgs);
    Optional<SubCommandBase> selectedCommand = commands.stream().filter(
        command -> command.getClass().getSimpleName().toLowerCase()
            .equals(jc.getParsedCommand())).findFirst();
    if (selectedCommand.isPresent()) {
      selectedCommand.get().run();
    } else {
      jc.usage();
    }
  } catch (ParameterException exception) {
    System.err.println("Wrong parameters: " + exception.getMessage());
    jc.usage();
  }

}
 
Example 7
Source File: JCommanderAutoConfiguration.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Provide a {@link JCommander} bean.
 *
 * @param globalAgentArguments  The global command arguments to use
 * @param agentCommandArguments An command argument beans in the environment that should also be used in addition
 *                              to the global command arguments
 * @return A {@link JCommander} instance
 */
@Bean
public JCommander jCommander(
    final GlobalAgentArguments globalAgentArguments,
    final List<AgentCommandArguments> agentCommandArguments
) {
    final JCommander.Builder jCommanderBuilder = JCommander.newBuilder()
        .addObject(globalAgentArguments)
        .acceptUnknownOptions(false);

    agentCommandArguments.forEach(jCommanderBuilder::addCommand);

    return jCommanderBuilder.build();
}
 
Example 8
Source File: TemplateGridCommand.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Override
public final Executable configure(PrintStream out, PrintStream err, String... args) {
  HelpFlags helpFlags = new HelpFlags();
  ConfigFlags configFlags = new ConfigFlags();

  Set<Object> allFlags = new LinkedHashSet<>();

  allFlags.add(helpFlags);
  allFlags.add(configFlags);

  StreamSupport.stream(ServiceLoader.load(HasRoles.class).spliterator(), true)
    .filter(flags -> !Sets.intersection(getConfigurableRoles(), flags.getRoles()).isEmpty())
    .forEach(allFlags::add);

  allFlags.addAll(getFlagObjects());

  JCommander.Builder builder = JCommander.newBuilder().programName(getName());
  allFlags.forEach(builder::addObject);
  JCommander commander = builder.build();
  commander.setConsole(new DefaultConsole(out));

  return () -> {
    try {
      commander.parse(args);
    } catch (ParameterException e) {
      err.println(e.getMessage());
      commander.usage();
      return;
    }

    if (helpFlags.displayHelp(commander, out)) {
      return;
    }

    Set<Config> allConfigs = new LinkedHashSet<>();
    allConfigs.add(new EnvConfig());
    allConfigs.add(new ConcatenatingConfig(getSystemPropertiesConfigPrefix(), '.', System.getProperties()));
    allFlags.forEach(flags -> allConfigs.add(new AnnotatedConfig(flags)));
    allConfigs.add(configFlags.readConfigFiles());
    allConfigs.add(getDefaultConfig());

    Config config = new MemoizedConfig(new CompoundConfig(allConfigs.toArray(new Config[0])));

    if (configFlags.dumpConfig(config, out)) {
      return;
    }

    if (configFlags.dumpConfigHelp(config, getConfigurableRoles(), out)) {
      return;
    }

    LoggingOptions loggingOptions = new LoggingOptions(config);
    loggingOptions.configureLogging();

    execute(config);
  };
}