Search in sources :

Example 61 with ParseException

use of org.apache.commons.cli.ParseException in project incubator-gobblin by apache.

the class Cli method parseAndExecuteCommand.

/**
 * Parse and execute the appropriate command based on the args.
 * The general flow looks like this:
 *
 * 1. Parse a set of global options (eg host/port for the admin server)
 * 2. Parse out the command name
 * 3. Pass the global options and any left over parameters to a command handler
 */
public void parseAndExecuteCommand() {
    CommandLineParser parser = new DefaultParser();
    try {
        CommandLine parsedOpts = parser.parse(this.options, this.args, true);
        GlobalOptions globalOptions = createGlobalOptions(parsedOpts);
        // Fetch the command and fail if there is ambiguity
        String[] remainingArgs = parsedOpts.getArgs();
        if (remainingArgs.length == 0) {
            printHelpAndExit("Command not specified!");
        }
        String commandName = remainingArgs[0].toLowerCase();
        remainingArgs = remainingArgs.length > 1 ? Arrays.copyOfRange(remainingArgs, 1, remainingArgs.length) : new String[] {};
        Command command = commandList.get(commandName);
        if (command == null) {
            System.out.println("Command " + commandName + " not known.");
            printHelpAndExit();
        } else {
            command.execute(globalOptions, remainingArgs);
        }
    } catch (ParseException e) {
        printHelpAndExit("Ran into an error parsing args.");
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 62 with ParseException

use of org.apache.commons.cli.ParseException in project incubator-gobblin by apache.

the class GobblinAWSTaskRunner method main.

public static void main(String[] args) throws Exception {
    final Options options = buildOptions();
    try {
        final CommandLine cmd = new DefaultParser().parse(options, args);
        if (!cmd.hasOption(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME) || !cmd.hasOption(GobblinClusterConfigurationKeys.HELIX_INSTANCE_NAME_OPTION_NAME) || !cmd.hasOption(GobblinAWSConfigurationKeys.APP_WORK_DIR)) {
            printUsage(options);
            System.exit(1);
        }
        if (System.getProperty("log4j.configuration") == null) {
            Log4jConfigHelper.updateLog4jConfiguration(GobblinTaskRunner.class, GobblinAWSConfigurationKeys.GOBBLIN_AWS_LOG4J_CONFIGURATION_FILE);
        }
        LOGGER.info(JvmUtils.getJvmInputArguments());
        final String applicationName = cmd.getOptionValue(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME);
        final String helixInstanceName = cmd.getOptionValue(GobblinClusterConfigurationKeys.HELIX_INSTANCE_NAME_OPTION_NAME);
        final String appWorkDir = cmd.getOptionValue(GobblinAWSConfigurationKeys.APP_WORK_DIR);
        final GobblinTaskRunner gobblinTaskRunner = new GobblinAWSTaskRunner(applicationName, helixInstanceName, ConfigFactory.load(), Optional.of(new Path(appWorkDir)));
        gobblinTaskRunner.start();
    } catch (ParseException pe) {
        printUsage(options);
        System.exit(1);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GobblinTaskRunner(org.apache.gobblin.cluster.GobblinTaskRunner) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 63 with ParseException

use of org.apache.commons.cli.ParseException in project incubator-gobblin by apache.

the class StateStoreBasedWatermarkStorageCli method run.

@Override
public void run(String[] args) {
    Options options = new Options();
    options.addOption(HELP);
    options.addOption(ZK);
    options.addOption(JOB_NAME);
    options.addOption(ROOT_DIR);
    options.addOption(WATCH);
    CommandLine cli;
    try {
        CommandLineParser parser = new DefaultParser();
        cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length));
    } catch (ParseException pe) {
        System.out.println("Command line parse exception: " + pe.getMessage());
        return;
    }
    if (cli.hasOption(HELP.getOpt())) {
        printUsage(options);
        return;
    }
    TaskState taskState = new TaskState();
    String jobName;
    if (!cli.hasOption(JOB_NAME.getOpt())) {
        log.error("Need Job Name to be specified --", JOB_NAME.getLongOpt());
        throw new RuntimeException("Need Job Name to be specified");
    } else {
        jobName = cli.getOptionValue(JOB_NAME.getOpt());
        log.info("Using job name: {}", jobName);
    }
    taskState.setProp(ConfigurationKeys.JOB_NAME_KEY, jobName);
    String zkAddress = "locahost:2181";
    if (cli.hasOption(ZK.getOpt())) {
        zkAddress = cli.getOptionValue(ZK.getOpt());
    }
    log.info("Using zk address : {}", zkAddress);
    taskState.setProp(StateStoreBasedWatermarkStorage.WATERMARK_STORAGE_TYPE_KEY, "zk");
    taskState.setProp("state.store.zk.connectString", zkAddress);
    if (cli.hasOption(ROOT_DIR.getOpt())) {
        String rootDir = cli.getOptionValue(ROOT_DIR.getOpt());
        taskState.setProp(StateStoreBasedWatermarkStorage.WATERMARK_STORAGE_CONFIG_PREFIX + ConfigurationKeys.STATE_STORE_ROOT_DIR_KEY, rootDir);
        log.info("Setting root dir to {}", rootDir);
    } else {
        log.error("Need root directory specified");
        printUsage(options);
        return;
    }
    StateStoreBasedWatermarkStorage stateStoreBasedWatermarkStorage = new StateStoreBasedWatermarkStorage(taskState);
    final AtomicBoolean stop = new AtomicBoolean(true);
    if (cli.hasOption(WATCH.getOpt())) {
        stop.set(false);
    }
    try {
        if (!stop.get()) {
            Runtime.getRuntime().addShutdownHook(new Thread() {

                public void run() {
                    stop.set(true);
                }
            });
        }
        do {
            boolean foundWatermark = false;
            try {
                for (CheckpointableWatermarkState wmState : stateStoreBasedWatermarkStorage.getAllCommittedWatermarks()) {
                    foundWatermark = true;
                    System.out.println(wmState.getProperties());
                }
            } catch (IOException ie) {
                Throwables.propagate(ie);
            }
            if (!foundWatermark) {
                System.out.println("No watermarks found.");
            }
            if (!stop.get()) {
                Thread.sleep(1000);
            }
        } while (!stop.get());
    } catch (Exception e) {
        Throwables.propagate(e);
    }
}
Also used : Options(org.apache.commons.cli.Options) IOException(java.io.IOException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 64 with ParseException

use of org.apache.commons.cli.ParseException in project incubator-gobblin by apache.

the class CliOptions method parseArgs.

/**
 * Parse command line arguments and return a {@link java.util.Properties} object for the gobblin job found.
 * @param caller Class of the calling main method. Used for error logs.
 * @param args Command line arguments.
 * @return Instance of {@link Properties} for the Gobblin job to run.
 * @throws IOException
 */
public static Properties parseArgs(Class<?> caller, String[] args) throws IOException {
    try {
        // Parse command-line options
        CommandLine cmd = new DefaultParser().parse(options(), args);
        if (cmd.hasOption(HELP_OPTION.getOpt())) {
            printUsage(caller);
            System.exit(0);
        }
        if (!cmd.hasOption(SYS_CONFIG_OPTION.getLongOpt()) || !cmd.hasOption(JOB_CONFIG_OPTION.getLongOpt())) {
            printUsage(caller);
            System.exit(1);
        }
        // Load system and job configuration properties
        Properties sysConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(SYS_CONFIG_OPTION.getLongOpt()));
        Properties jobConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(JOB_CONFIG_OPTION.getLongOpt()));
        return JobConfigurationUtils.combineSysAndJobProperties(sysConfig, jobConfig);
    } catch (ParseException | ConfigurationException e) {
        throw new IOException(e);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException) Properties(java.util.Properties) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 65 with ParseException

use of org.apache.commons.cli.ParseException in project twister2 by DSC-SPIDAL.

the class JavaCheckstyle method main.

public static void main(String[] args) throws IOException {
    CommandLineParser parser = new DefaultParser();
    // create the Options
    Options options = new Options();
    options.addOption(Option.builder("f").required(true).hasArg().longOpt("extra_action_file").desc("bazel extra action protobuf file").build());
    options.addOption(Option.builder("hc").required(true).hasArg().longOpt("twister2_checkstyle_config_file").desc("checkstyle config file").build());
    options.addOption(Option.builder("ac").required(true).hasArg().longOpt("apache_checkstyle_config_file").desc("checkstyle config file for imported source files").build());
    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);
        String extraActionFile = line.getOptionValue("f");
        String configFile = line.getOptionValue("hc");
        String apacheConfigFile = line.getOptionValue("ac");
        // check twister2 source file style
        String[] twister2SourceFiles = getTwister2SourceFiles(extraActionFile);
        checkStyle(twister2SourceFiles, configFile);
        // check other apache source file style
        String[] apacheSourceFiles = getApacheSourceFiles(extraActionFile);
        checkStyle(apacheSourceFiles, apacheConfigFile);
    } catch (ParseException exp) {
        LOG.severe(String.format("Invalid input to %s: %s", CLASSNAME, exp.getMessage()));
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java " + CLASSNAME, options);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

ParseException (org.apache.commons.cli.ParseException)586 CommandLine (org.apache.commons.cli.CommandLine)488 CommandLineParser (org.apache.commons.cli.CommandLineParser)380 Options (org.apache.commons.cli.Options)370 DefaultParser (org.apache.commons.cli.DefaultParser)220 HelpFormatter (org.apache.commons.cli.HelpFormatter)204 GnuParser (org.apache.commons.cli.GnuParser)173 IOException (java.io.IOException)124 Option (org.apache.commons.cli.Option)109 File (java.io.File)90 PosixParser (org.apache.commons.cli.PosixParser)65 Path (org.apache.hadoop.fs.Path)50 ArrayList (java.util.ArrayList)42 Properties (java.util.Properties)35 BasicParser (org.apache.commons.cli.BasicParser)31 FileInputStream (java.io.FileInputStream)29 Job (org.apache.hadoop.mapreduce.Job)27 Configuration (org.apache.hadoop.conf.Configuration)26 List (java.util.List)25 URI (java.net.URI)21