Search in sources :

Example 6 with PosixParser

use of org.apache.commons.cli.PosixParser in project hadoop by apache.

the class OfflineEditsViewer method run.

/**
   * Main entry point for ToolRunner (see ToolRunner docs)
   *
   * @param argv The parameters passed to this program.
   * @return 0 on success, non zero on error.
   */
@Override
public int run(String[] argv) throws Exception {
    Options options = buildOptions();
    if (argv.length == 0) {
        printHelp();
        return 0;
    }
    // print help and exit with zero exit code
    if (argv.length == 1 && isHelpOption(argv[0])) {
        printHelp();
        return 0;
    }
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, argv);
    } catch (ParseException e) {
        System.out.println("Error parsing command-line options: " + e.getMessage());
        printHelp();
        return -1;
    }
    if (cmd.hasOption("h")) {
        // print help and exit with non zero exit code since
        // it is not expected to give help and other options together.
        printHelp();
        return -1;
    }
    String inputFileName = cmd.getOptionValue("i");
    String outputFileName = cmd.getOptionValue("o");
    String processor = cmd.getOptionValue("p");
    if (processor == null) {
        processor = defaultProcessor;
    }
    Flags flags = new Flags();
    if (cmd.hasOption("r")) {
        flags.setRecoveryMode();
    }
    if (cmd.hasOption("f")) {
        flags.setFixTxIds();
    }
    if (cmd.hasOption("v")) {
        flags.setPrintToScreen();
    }
    return go(inputFileName, outputFileName, processor, flags, null);
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 7 with PosixParser

use of org.apache.commons.cli.PosixParser in project hadoop by apache.

the class ArgumentParser method parse.

/**
   * Parses the command line options
   * 
   * @return false if need to print help output
   * 
   * @throws Exception
   *           when parsing fails
   */
ParsedOutput parse() throws Exception {
    if (parsed == null) {
        PosixParser parser = new PosixParser();
        CommandLine popts = parser.parse(getOptionList(), argumentList, true);
        if (popts.hasOption(ConfigOption.HELP.getOpt())) {
            parsed = new ParsedOutput(null, this, true);
        } else {
            parsed = new ParsedOutput(popts, this, false);
        }
    }
    return parsed;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser)

Example 8 with PosixParser

use of org.apache.commons.cli.PosixParser in project hbase by apache.

the class WALPrettyPrinter method run.

/**
   * Pass one or more log file names and formatting options and it will dump out
   * a text version of the contents on <code>stdout</code>.
   *
   * @param args
   *          Command line arguments
   * @throws IOException
   *           Thrown upon file system errors etc.
   */
public static void run(String[] args) throws IOException {
    // create options
    Options options = new Options();
    options.addOption("h", "help", false, "Output help message");
    options.addOption("j", "json", false, "Output JSON");
    options.addOption("p", "printvals", false, "Print values");
    options.addOption("r", "region", true, "Region to filter by. Pass encoded region name; e.g. '9192caead6a5a20acb4454ffbc79fa14'");
    options.addOption("s", "sequence", true, "Sequence to filter by. Pass sequence number.");
    options.addOption("w", "row", true, "Row to filter by. Pass row name.");
    WALPrettyPrinter printer = new WALPrettyPrinter();
    CommandLineParser parser = new PosixParser();
    List<?> files = null;
    try {
        CommandLine cmd = parser.parse(options, args);
        files = cmd.getArgList();
        if (files.isEmpty() || cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("WAL <filename...>", options, true);
            System.exit(-1);
        }
        // configure the pretty printer using command line options
        if (cmd.hasOption("p"))
            printer.enableValues();
        if (cmd.hasOption("j"))
            printer.enableJSON();
        if (cmd.hasOption("r"))
            printer.setRegionFilter(cmd.getOptionValue("r"));
        if (cmd.hasOption("s"))
            printer.setSequenceFilter(Long.parseLong(cmd.getOptionValue("s")));
        if (cmd.hasOption("w"))
            printer.setRowFilter(cmd.getOptionValue("w"));
    } catch (ParseException e) {
        e.printStackTrace();
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("HFile filename(s) ", options, true);
        System.exit(-1);
    }
    // get configuration, file system, and process the given files
    Configuration conf = HBaseConfiguration.create();
    FSUtils.setFsDefault(conf, FSUtils.getRootDir(conf));
    // begin output
    printer.beginPersistentOutput();
    for (Object f : files) {
        Path file = new Path((String) f);
        FileSystem fs = file.getFileSystem(conf);
        if (!fs.exists(file)) {
            System.err.println("ERROR, file doesnt exist: " + file);
            return;
        }
        printer.processFile(conf, file);
    }
    printer.endPersistentOutput();
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Path(org.apache.hadoop.fs.Path) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Configuration(org.apache.hadoop.conf.Configuration) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) PosixParser(org.apache.commons.cli.PosixParser) FileSystem(org.apache.hadoop.fs.FileSystem) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 9 with PosixParser

use of org.apache.commons.cli.PosixParser in project hadoop by apache.

the class TimelineSchemaCreator method parseArgs.

/**
   * Parse command-line arguments.
   *
   * @param args
   *          command line arguments passed to program.
   * @return parsed command line.
   * @throws ParseException
   */
private static CommandLine parseArgs(String[] args) throws ParseException {
    Options options = new Options();
    // Input
    Option o = new Option(ENTITY_TABLE_NAME_SHORT, "entityTableName", true, "entity table name");
    o.setArgName("entityTableName");
    o.setRequired(false);
    options.addOption(o);
    o = new Option(TTL_OPTION_SHORT, "metricsTTL", true, "TTL for metrics column family");
    o.setArgName("metricsTTL");
    o.setRequired(false);
    options.addOption(o);
    o = new Option(APP_TO_FLOW_TABLE_NAME_SHORT, "appToflowTableName", true, "app to flow table name");
    o.setArgName("appToflowTableName");
    o.setRequired(false);
    options.addOption(o);
    o = new Option(APP_TABLE_NAME_SHORT, "applicationTableName", true, "application table name");
    o.setArgName("applicationTableName");
    o.setRequired(false);
    options.addOption(o);
    // Options without an argument
    // No need to set arg name since we do not need an argument here
    o = new Option(SKIP_EXISTING_TABLE_OPTION_SHORT, "skipExistingTable", false, "skip existing Hbase tables and continue to create new tables");
    o.setRequired(false);
    options.addOption(o);
    CommandLineParser parser = new PosixParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (Exception e) {
        LOG.error("ERROR: " + e.getMessage() + "\n");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(NAME + " ", options, true);
        System.exit(-1);
    }
    return commandLine;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException)

Example 10 with PosixParser

use of org.apache.commons.cli.PosixParser in project hbase by apache.

the class ProcedureWALPrettyPrinter method run.

/**
   * Pass one or more log file names and formatting options and it will dump out
   * a text version of the contents on <code>stdout</code>.
   *
   * @param args
   *          Command line arguments
   * @throws IOException
   *           Thrown upon file system errors etc.
   */
public int run(final String[] args) throws IOException {
    // create options
    Options options = new Options();
    options.addOption("h", "help", false, "Output help message");
    options.addOption("f", "file", true, "File to print");
    final List<Path> files = new ArrayList<>();
    try {
        CommandLine cmd = new PosixParser().parse(options, args);
        if (cmd.hasOption("f")) {
            files.add(new Path(cmd.getOptionValue("f")));
        }
        if (files.isEmpty() || cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("ProcedureWALPrettyPrinter ", options, true);
            return (-1);
        }
    } catch (ParseException e) {
        e.printStackTrace();
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("ProcedureWALPrettyPrinter ", options, true);
        return (-1);
    }
    // get configuration, file system, and process the given files
    for (Path file : files) {
        processFile(getConf(), file);
    }
    return (0);
}
Also used : Path(org.apache.hadoop.fs.Path) HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) ArrayList(java.util.ArrayList) ParseException(org.apache.commons.cli.ParseException)

Aggregations

PosixParser (org.apache.commons.cli.PosixParser)79 CommandLine (org.apache.commons.cli.CommandLine)74 CommandLineParser (org.apache.commons.cli.CommandLineParser)61 Options (org.apache.commons.cli.Options)52 ParseException (org.apache.commons.cli.ParseException)50 IOException (java.io.IOException)19 File (java.io.File)17 HelpFormatter (org.apache.commons.cli.HelpFormatter)13 SystemExitException (org.apache.openejb.cli.SystemExitException)7 Properties (java.util.Properties)6 Parser (org.apache.commons.cli.Parser)6 List (java.util.List)5 Option (org.apache.commons.cli.Option)5 Path (org.apache.hadoop.fs.Path)4 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 InitialContext (javax.naming.InitialContext)3 NamingException (javax.naming.NamingException)3 ServiceUnavailableException (javax.naming.ServiceUnavailableException)3 Configuration (org.apache.hadoop.conf.Configuration)3