當前位置: 首頁>>代碼示例>>Java>>正文


Java JCommander.Builder方法代碼示例

本文整理匯總了Java中com.beust.jcommander.JCommander.Builder方法的典型用法代碼示例。如果您正苦於以下問題:Java JCommander.Builder方法的具體用法?Java JCommander.Builder怎麽用?Java JCommander.Builder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.beust.jcommander.JCommander的用法示例。


在下文中一共展示了JCommander.Builder方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import com.beust.jcommander.JCommander; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
  initializeCommands();
  Runner runner = new Runner();
  Server server = new Server();

  JCommander.Builder builder = JCommander.newBuilder().addObject(runner);
  commands.forEach(command -> builder
      .addCommand(command.getClass().getSimpleName().toLowerCase(), command));
  JCommander jc = builder.build();
  try {
    jc.parse(args);
    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();
  }

}
 
開發者ID:apache,項目名稱:incubator-ratis,代碼行數:26,代碼來源:Runner.java

示例2: createCommander

import com.beust.jcommander.JCommander; //導入方法依賴的package包/類
private static JCommander createCommander(CommandContainer commandContainer, String[] args) {
    JCommander.Builder builder = JCommander.newBuilder();
    for (String operation : commandContainer.getAllCommands()) {
        CommandContainer.Command command = commandContainer.getCommand(operation);
        builder.addCommand(operation, command);
    }
    JCommander commander = builder.build();
    commander.setProgramName("stf");
    commander.setCaseSensitiveOptions(false);
    commander.parseWithoutValidation(args);
    return commander;
}
 
開發者ID:e13mort,項目名稱:stf-console-client,代碼行數:13,代碼來源:StfCommander.java

示例3: main

import com.beust.jcommander.JCommander; //導入方法依賴的package包/類
public static void main(String... argv) throws Exception {

        //Attempt to determine verbose level first
        final Now now = new Now();
        JCommander.newBuilder().addObject(now)
                .acceptUnknownOptions(true)
                .build().parse(argv);

        final CommandMain commandMain = new CommandMain();

        final JCommander.Builder commanderBuilder = JCommander.newBuilder()
                .programName("java -jar now4j-cli-<version>.jar")
                .verbose(now.verboseLevel)
                .addObject(commandMain);
        for (final Map.Entry<String, AbstractCommand> commandEntry : commandMap.entrySet()) {
            commanderBuilder.addCommand(commandEntry.getKey(), commandEntry.getValue());
        }
        final JCommander commander = commanderBuilder.build();
        commander.parse(argv);

        if (commandMain.help) {
            commander.usage();
            return;
        }

        final NowClient nowClient;
        final String token = commandMain.token;
        if (token == null || token.isEmpty()) {
            //Default Now Client, with no option => read from  /.now.json file
            nowClient = NowClient.create();
        } else {
            //There is a token option
            final String team = commandMain.team;
            if (team == null || team.isEmpty()) {
                //Read team from  /.now.json, if any
                nowClient = NowClient.create(token);
            } else {
                nowClient = NowClient.create(token, team);
            }
        }

        final String parsedCommand = commander.getParsedCommand();
        if (!commandMap.containsKey(parsedCommand)) {
            throw new IllegalArgumentException("Unsupported command: " +
                    parsedCommand + ". Possible commands: " + commandMap.keySet());
        }
        final AbstractCommand abstractCommand = commandMap.get(parsedCommand);
        abstractCommand.setNowClient(nowClient);
        abstractCommand.work();
    }
 
開發者ID:rm3l,項目名稱:now4j,代碼行數:51,代碼來源:Now.java

示例4: main

import com.beust.jcommander.JCommander; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException, InterruptedException {
    SenseHatTester senseHatTester = new SenseHatTester();

    Map<String, Command> commands = new HashMap<>();

    commands.put("fill", new FillCommand());
    commands.put("set-pixel", new SetPixelCommand());
    commands.put("save-image", new SaveImageCommand());
    commands.put("load-image", new LoadImageCommand());
    commands.put("env", new EnvCommand());
    commands.put("pulse", new PulseCommand());
    commands.put("text", new TextCommand());
    commands.put("clock", new BinaryClockCommand());

    JCommander.Builder builder = JCommander.newBuilder()
            .addObject(senseHatTester);

    commands.forEach((name, cmd) -> builder.addCommand(name, cmd));

    JCommander jCommander = builder.build();
    jCommander.parse(args);

    if(senseHatTester.isPrintUsage()) {
        jCommander.usage();
        return;
    }

    String parsedCommand = jCommander.getParsedCommand();

    if (parsedCommand != null && commands.containsKey(parsedCommand)) {
        SenseHat senseHat;
        try {
            if(senseHatTester.getFrameBuffer() != null) {
                senseHat = new SenseHat(senseHatTester.getFrameBuffer());
            } else {
                senseHat = new SenseHat();
            }
        } catch (IllegalStateException e) {
            log.error(e.getLocalizedMessage(), e);
            return;
        }
        commands.get(parsedCommand).run(senseHat);
    } else {
        jCommander.usage();
    }
}
 
開發者ID:larsgrefer,項目名稱:java-sense-hat,代碼行數:47,代碼來源:SenseHatTester.java


注:本文中的com.beust.jcommander.JCommander.Builder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。