Java Code Examples for com.beust.jcommander.JCommander#getParsedCommand()

The following examples show how to use com.beust.jcommander.JCommander#getParsedCommand() . 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: HugeGraphCommand.java    From hugegraph-tools with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    HugeGraphCommand cmd = new HugeGraphCommand();
    JCommander jCommander = cmd.jCommander();

    if (args.length == 0) {
        jCommander.usage();
        System.exit(-1);
    }
    try {
        jCommander.parse(args);
    } catch (ParameterException e) {
        Printer.print(e.getMessage());
        System.exit(-1);
    }

    String subCommand = jCommander.getParsedCommand();
    if (subCommand == null) {
        Printer.print("Must provide one sub-command");
        jCommander.usage();
        System.exit(-1);
    }

    cmd.execute(subCommand, jCommander);
    System.exit(0);
}
 
Example 2
Source File: Tool.java    From openjavacard-tools with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void runMainCommand(JCommander jc) {
    // get defined commands
    Map<String, JCommander> commands = jc.getCommands();
    // get the command
    String commandName = jc.getParsedCommand();
    // help if there was no command
    if (commandName == null) {
        help = true;
    }
    // run help if appropriate
    JCommander helpCommand = commands.get("help");
    if (help || commandName == "help") {
        runCommand("help", helpCommand);
        return;
    }
    // run the command
    JCommander command = commands.get(commandName);
    runCommand(commandName, command);
}
 
Example 3
Source File: CramTools.java    From cramtools with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		Params params = new Params();
		JCommander jc = new JCommander(params);
		jc.setProgramName("cramtools");

		addProgram(jc, Cram2Bam.class);
		addProgram(jc, Bam2Cram.class);
		addProgram(jc, CramIndexer.class);
		addProgram(jc, Merge.class);
		addProgram(jc, Cram2Fastq.class);
		addProgram(jc, CramFixHeader.class);
		addProgram(jc, DownloadReferences.class);
		addProgram(jc, QualityScoreStats.class);

		jc.parse(args);

		String command = jc.getParsedCommand();

		if (command == null || params.help) {
			Utils.printUsage(jc);
			return;
		}

		String[] commandArgs = new String[args.length - 1];
		System.arraycopy(args, 1, commandArgs, 0, commandArgs.length);

		invoke(command, commandArgs);
	}
 
Example 4
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 5
Source File: ExplainCommand.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final OperationParams inputParams) {

  final CommandLineOperationParams params = (CommandLineOperationParams) inputParams;

  final StringBuilder builder = new StringBuilder();

  // Sort first
  String nextCommand = "geowave";
  JCommander commander = params.getCommander();
  while (commander != null) {
    if ((commander.getParameters() != null) && (commander.getParameters().size() > 0)) {
      builder.append("Command: ");
      builder.append(nextCommand);
      builder.append(" [options]");
      if (commander.getParsedCommand() != null) {
        builder.append(" <subcommand> ...");
      }
      builder.append("\n\n");
      builder.append(explainCommander(commander));
      builder.append("\n");
    } else if (commander.getMainParameter() != null) {
      builder.append("Command: ");
      builder.append(nextCommand);
      if (commander.getParsedCommand() != null) {
        builder.append(" <subcommand> ...");
      }
      builder.append("\n\n");
      builder.append(explainMainParameter(commander));
      builder.append("\n");
    }
    nextCommand = commander.getParsedCommand();
    commander = commander.getCommands().get(nextCommand);
  }

  params.getConsole().println(builder.toString().trim());
}
 
Example 6
Source File: Main.java    From digdag with Apache License 2.0 5 votes vote down vote up
private static Command getParsedCommand(JCommander jc)
{
    String commandName = jc.getParsedCommand();
    if (commandName == null) {
        return null;
    }

    return (Command) jc.getCommands().get(commandName).getObjects().get(0);
}
 
Example 7
Source File: CliMain.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  CliConfiguration config = new CliConfiguration();

  JCommander commander = new JCommander();
  Map<String, Object> commands = ImmutableMap.<String, Object>builder()
      .put("login", new LoginActionConfig())
      .put("list", new ListActionConfig())
      .put("describe", new DescribeActionConfig())
      .put("add", new AddActionConfig())
      .put("update", new UpdateActionConfig())
      .put("delete", new DeleteActionConfig())
      .put("assign", new AssignActionConfig())
      .put("unassign", new UnassignActionConfig())
      .put("versions", new ListVersionsActionConfig())
      .put("rollback", new RollbackActionConfig())
      .build();
  commander.setProgramName("KeyWhiz Configuration Utility");
  commander.addObject(config);

  for (Map.Entry<String, Object> entry : commands.entrySet()) {
    commander.addCommand(entry.getKey(), entry.getValue());
  }

  try {
    commander.parse(args);
  } catch (ParameterException e) {
    System.err.println("Invalid command: " + e.getMessage());
    commander.usage();
    System.exit(1);
  }

  String command = commander.getParsedCommand();
  JCommander specificCommander = commander.getCommands().get(command);
  Injector injector = Guice.createInjector(new CliModule(config, commander, specificCommander,
      command, commands));
  CommandExecutor executor = injector.getInstance(CommandExecutor.class);
  executor.executeCommand();
}
 
Example 8
Source File: Main.java    From jetcd with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Args global = new Args();
    CommandGet getCmd = new CommandGet();
    CommandPut putCmd = new CommandPut();
    CommandWatch watchCmd = new CommandWatch();

    JCommander jc = JCommander.newBuilder().addObject(global).addCommand("get", getCmd).addCommand("put", putCmd)
        .addCommand("watch", watchCmd).build();

    jc.parse(args);

    String cmd = jc.getParsedCommand();
    if (cmd == null || global.help) {
        jc.usage();
        return;
    }

    try (Client client = Client.builder().endpoints(global.endpoints.split(",")).build()) {
        switch (cmd) {
            case "get":
                getCmd.accept(client);
                break;
            case "put":
                putCmd.accept(client);
                break;
            case "watch":
                watchCmd.accept(client);
                break;
        }
    } catch (Exception e) {
        LOGGER.error(cmd + " Error {}", e);
        System.exit(1);
    }
}
 
Example 9
Source File: AbstractCommand.java    From apiman-cli with Apache License 2.0 5 votes vote down vote up
private String getCommandChain(JCommander in) {
    String chain = in.getProgramName() + " ";
    JCommander jc = in;
    while (jc.getParsedCommand() != null) {
        chain += jc.getParsedCommand() + " ";
        jc = jc.getCommands().get(jc.getParsedCommand());
    }
    return chain;
}
 
Example 10
Source File: AbstractCommand.java    From apiman-cli with Apache License 2.0 5 votes vote down vote up
private JCommander getCommand(JCommander in) {
    JCommander jc = in;
    while (jc.getParsedCommand() != null) {
        jc = jc.getCommands().get(jc.getParsedCommand());
    }
    return jc;
}
 
Example 11
Source File: Main.java    From akka-tutorial with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	
	CommandMaster commandMaster = new CommandMaster();
       CommandSlave commandSlave = new CommandSlave();
       JCommander jCommander = JCommander.newBuilder()
       	.addCommand(MasterSystem.MASTER_ROLE, commandMaster)
           .addCommand(SlaveSystem.SLAVE_ROLE, commandSlave)
           .build();
       
       try {
       	jCommander.parse(args);

           if (jCommander.getParsedCommand() == null)
               throw new ParameterException("No command given.");

           switch (jCommander.getParsedCommand()) {
               case MasterSystem.MASTER_ROLE:
               	ConfigurationSingleton.get().update(commandMaster);
               	
               	MasterSystem.start();
                   break;
               case SlaveSystem.SLAVE_ROLE:
               	ConfigurationSingleton.get().update(commandSlave);
               	
               	SlaveSystem.start();
                   break;
               default:
                   throw new AssertionError();
           }
       } catch (ParameterException e) {
           System.out.printf("Could not parse args: %s\n", e.getMessage());
           if (jCommander.getParsedCommand() == null) {
               jCommander.usage();
           } else {
               jCommander.usage(jCommander.getParsedCommand());
           }
           System.exit(1);
       }
}
 
Example 12
Source File: OctopusApp.java    From akka-tutorial with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

    	MasterCommand masterCommand = new MasterCommand();
        SlaveCommand slaveCommand = new SlaveCommand();
        JCommander jCommander = JCommander.newBuilder()
        	.addCommand(OctopusMaster.MASTER_ROLE, masterCommand)
            .addCommand(OctopusSlave.SLAVE_ROLE, slaveCommand)
            .build();

        try {
            jCommander.parse(args);

            if (jCommander.getParsedCommand() == null) {
                throw new ParameterException("No command given.");
            }

            switch (jCommander.getParsedCommand()) {
                case OctopusMaster.MASTER_ROLE:
                    OctopusMaster.start(ACTOR_SYSTEM_NAME, masterCommand.workers, masterCommand.host, masterCommand.port);
                    break;
                case OctopusSlave.SLAVE_ROLE:
                    OctopusSlave.start(ACTOR_SYSTEM_NAME, slaveCommand.workers, slaveCommand.host, slaveCommand.port, slaveCommand.masterhost, slaveCommand.masterport);
                    break;
                default:
                    throw new AssertionError();
            }

        } catch (ParameterException e) {
            System.out.printf("Could not parse args: %s\n", e.getMessage());
            if (jCommander.getParsedCommand() == null) {
                jCommander.usage();
            } else {
                jCommander.usage(jCommander.getParsedCommand());
            }
            System.exit(1);
        }
	}
 
Example 13
Source File: Main.java    From walle with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
    final Map<String, IWalleCommand> subCommandList = new HashMap<String, IWalleCommand>();
    subCommandList.put("show", new ShowCommand());
    subCommandList.put("rm", new RemoveCommand());
    subCommandList.put("put", new PutCommand());
    subCommandList.put("batch", new BatchCommand());
    subCommandList.put("batch2", new Batch2Command());

    final WalleCommandLine walleCommandLine = new WalleCommandLine();
    final JCommander commander = new JCommander(walleCommandLine);

    for (Map.Entry<String, IWalleCommand> commandEntry : subCommandList.entrySet()) {
        commander.addCommand(commandEntry.getKey(), commandEntry.getValue());
    }
    try {
        commander.parse(args);
    } catch (ParameterException e) {
        System.out.println(e.getMessage());
        commander.usage();
        System.exit(1);
        return;
    }

    walleCommandLine.parse(commander);

    final String parseCommand = commander.getParsedCommand();
    if (parseCommand != null) {
        subCommandList.get(parseCommand).parse();
    }
}
 
Example 14
Source File: UpdateBot.java    From updatebot with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the command from the given command line arguments or returns null if there is no command found
 */
public static CommandSupport parseCommand(String[] args, Configuration config, boolean defaultToHelp) {
    PushSourceChanges pushSourceChanges = new PushSourceChanges();
    PushVersionChanges pushVersionChanges = new PushVersionChanges();
    PullVersionChanges pullVersionChanges = new PullVersionChanges();
    UpdatePullRequests updatePullRequests = new UpdatePullRequests();
    Help help = new Help();

    JCommander commander = JCommander.newBuilder()
            .addObject(config)
            .addCommand(HELP, help)
            .addCommand(PULL, pullVersionChanges)
            .addCommand(PUSH_SOURCE, pushSourceChanges)
            .addCommand(PUSH_VERSION, pushVersionChanges)
            .addCommand(UPDATE, updatePullRequests)
            .build();
    commander.setExpandAtSign(false);
    commander.setProgramName("updatebot");
    commander.parse(args);

    help.setCommander(commander);

    String parsedCommand = commander.getParsedCommand();
    if (parsedCommand != null) {
        switch (parsedCommand) {
            case HELP:
                return help;

            case PULL:
                return pullVersionChanges;

            case PUSH_SOURCE:
                return pushSourceChanges;

            case PUSH_VERSION:
                return pushVersionChanges;

            case UPDATE:
                return updatePullRequests;
        }
    }
    if (defaultToHelp) {
        return help;
    }
    return null;
}
 
Example 15
Source File: Main.java    From akka-tutorial with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	
	CommandMaster commandMaster = new CommandMaster();
       CommandSlave commandSlave = new CommandSlave();
       JCommander jCommander = JCommander.newBuilder()
       	.addCommand(MasterSystem.MASTER_ROLE, commandMaster)
           .addCommand(SlaveSystem.SLAVE_ROLE, commandSlave)
           .build();
       
       try {
       	jCommander.parse(args);

           if (jCommander.getParsedCommand() == null)
               throw new ParameterException("No command given.");

           switch (jCommander.getParsedCommand()) {
               case MasterSystem.MASTER_ROLE:
               	ConfigurationSingleton.get().update(commandMaster);
               	DatasetDescriptorSingleton.get().update(commandMaster);
               	
               	MasterSystem.start();
                   break;
               case SlaveSystem.SLAVE_ROLE:
               	ConfigurationSingleton.get().update(commandSlave);
               	DatasetDescriptorSingleton.set(null);
               	
               	SlaveSystem.start();
                   break;
               default:
                   throw new AssertionError();
           }
       } catch (ParameterException e) {
           System.out.printf("Could not parse args: %s\n", e.getMessage());
           if (jCommander.getParsedCommand() == null) {
               jCommander.usage();
           } else {
               jCommander.usage(jCommander.getParsedCommand());
           }
           System.exit(1);
       }
}
 
Example 16
Source File: TokensCliUtils.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander(arguments);

    CommandCreateSecretKey commandCreateSecretKey = new CommandCreateSecretKey();
    jcommander.addCommand("create-secret-key", commandCreateSecretKey);

    CommandCreateKeyPair commandCreateKeyPair = new CommandCreateKeyPair();
    jcommander.addCommand("create-key-pair", commandCreateKeyPair);

    CommandCreateToken commandCreateToken = new CommandCreateToken();
    jcommander.addCommand("create", commandCreateToken);

    CommandShowToken commandShowToken = new CommandShowToken();
    jcommander.addCommand("show", commandShowToken);

    CommandValidateToken commandValidateToken = new CommandValidateToken();
    jcommander.addCommand("validate", commandValidateToken);

    try {
        jcommander.parse(args);

        if (arguments.help || jcommander.getParsedCommand() == null) {
            jcommander.usage();
            System.exit(1);
        }
    } catch (Exception e) {
        System.err.println(e);
        String chosenCommand = jcommander.getParsedCommand();
        jcommander.usage(chosenCommand);
        System.exit(1);
    }

    String cmd = jcommander.getParsedCommand();

    if (cmd.equals("create-secret-key")) {
        commandCreateSecretKey.run();
    } else if (cmd.equals("create-key-pair")) {
        commandCreateKeyPair.run();
    } else if (cmd.equals("create")) {
        commandCreateToken.run();
    } else if (cmd.equals("show")) {
        commandShowToken.run();
    } else if (cmd.equals("validate")) {
        commandValidateToken.run();
    } else {
        System.err.println("Invalid command: " + cmd);
        System.exit(1);
    }
}
 
Example 17
Source File: Main.java    From akka-tutorial with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    
	// Parse the command-line args.
    MasterCommand masterCommand = new MasterCommand();
    SlaveCommand slaveCommand = new SlaveCommand();
    JCommander jCommander = JCommander.newBuilder()
            .addCommand("master", masterCommand)
            .addCommand("slave", slaveCommand)
            .build();

    try {
        jCommander.parse(args);

        if (jCommander.getParsedCommand() == null) {
            throw new ParameterException("No command given.");
        }

        // Start a master or slave.
        switch (jCommander.getParsedCommand()) {
            case "master":
                startMaster(masterCommand);
                break;
            case "slave":
                startSlave(slaveCommand);
                break;
            default:
                throw new AssertionError();

        }

    } catch (ParameterException e) {
        System.out.printf("Could not parse args: %s\n", e.getMessage());
        if (jCommander.getParsedCommand() == null) {
            jCommander.usage();
        } else {
            jCommander.usage(jCommander.getParsedCommand());
        }
        System.exit(1);
    }

}
 
Example 18
Source File: CompletionCommand.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Override
public Executable configure(PrintStream out, PrintStream err, String... args) {
  HelpFlags help = new HelpFlags();

  Zsh zsh = new Zsh();

  JCommander commander = JCommander.newBuilder()
    .programName("selenium")
    .addObject(help)
    .addCommand(zsh)
    .build();
  commander.setConsole(new DefaultConsole(out));

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

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

    if (args.length == 0) {
      commander.parse();
    }

    switch (commander.getParsedCommand()) {
      case "zsh":
        outputZshCompletions(out);
        break;

      default:
        err.println("Unrecognised shell: " + commander.getParsedCommand());
        System.exit(1);
        break;
    }
  };
}
 
Example 19
Source File: Script.java    From openjavacard-tools with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
    try {
        for(File script: scripts) {
            FileReader fr = new FileReader(script);
            BufferedReader br = new BufferedReader(fr);
            for (String line; (line = br.readLine()) != null; ) {
                // skip empty lines
                if(line.isEmpty() || line.matches("^\\s+$")) {
                    continue;
                }
                // ignore comments
                if(line.startsWith("#")) {
                    continue;
                }
                // eat multilines
                if(line.endsWith("\\")) {
                    while(line.endsWith("\\")) {
                        line = line.substring(0, line.length() - 1);
                        String next = br.readLine();
                        if(next == null) {
                            break;
                        } else {
                            line += next;
                        }
                    }
                }
                // tokenize the line
                String[] tokens = line.split("\\s+");
                // process command if there is one
                if (tokens.length > 0) {
                    // build a fresh commander
                    JCommander jc = mTool.makeCommander();
                    jc.parse(tokens);
                    String command = jc.getParsedCommand();
                    if (command != null) {
                        JCommander commandJc = jc.getCommands().get(command);
                        mTool.runCommand(command, commandJc);
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new Error(e);
    }
}
 
Example 20
Source File: Main.java    From ballerina-message-broker with Apache License 2.0 3 votes vote down vote up
/**
 * When it needs to find matching command, traversing is done through a recursive algorithm.
 *
 * This can throw runtime exceptions through {@link BrokerClientException} when an error occurs while executing
 * the command.
 *
 * @param jCommander commander in current level.
 * @return matched command instance.
 */
private static MBClientCmd findLeafCommand(JCommander jCommander) {
    String commandText = jCommander.getParsedCommand();
    if (Objects.isNull(commandText)) {
        return (MBClientCmd) jCommander.getObjects().get(0);
    }
    return findLeafCommand(jCommander.getCommands().get(commandText));
}