Search in sources :

Example 31 with Options

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

the class MiniDFSClusterManager method parseArguments.

/**
   * Parses arguments and fills out the member variables.
   * @param args Command-line arguments.
   * @return true on successful parse; false to indicate that the
   * program should exit.
   */
private boolean parseArguments(String[] args) {
    Options options = makeOptions();
    CommandLine cli;
    try {
        CommandLineParser parser = new GnuParser();
        cli = parser.parse(options, args);
    } catch (ParseException e) {
        LOG.warn("options parsing failed:  " + e.getMessage());
        new HelpFormatter().printHelp("...", options);
        return false;
    }
    if (cli.hasOption("help")) {
        new HelpFormatter().printHelp("...", options);
        return false;
    }
    if (cli.getArgs().length > 0) {
        for (String arg : cli.getArgs()) {
            LOG.error("Unrecognized option: " + arg);
            new HelpFormatter().printHelp("...", options);
            return false;
        }
    }
    // HDFS
    numDataNodes = intArgument(cli, "datanodes", 1);
    nameNodePort = intArgument(cli, "nnport", 0);
    nameNodeHttpPort = intArgument(cli, "httpport", 0);
    if (cli.hasOption("format")) {
        dfsOpts = StartupOption.FORMAT;
        format = true;
    } else {
        dfsOpts = StartupOption.REGULAR;
        format = false;
    }
    // Runner
    writeDetails = cli.getOptionValue("writeDetails");
    writeConfig = cli.getOptionValue("writeConfig");
    // General
    conf = new HdfsConfiguration();
    updateConfiguration(conf, cli.getOptionValues("D"));
    return true;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration)

Example 32 with Options

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

the class OfflineImageViewerPB method run.

public static int run(String[] args) throws Exception {
    Options options = buildOptions();
    if (args.length == 0) {
        printUsage();
        return 0;
    }
    // print help and exit with zero exit code
    if (args.length == 1 && isHelpOption(args[0])) {
        printUsage();
        return 0;
    }
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Error parsing command-line options: ");
        printUsage();
        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.
        printUsage();
        return -1;
    }
    String inputFile = cmd.getOptionValue("i");
    String processor = cmd.getOptionValue("p", "Web");
    String outputFile = cmd.getOptionValue("o", "-");
    String delimiter = cmd.getOptionValue("delimiter", PBImageDelimitedTextWriter.DEFAULT_DELIMITER);
    String tempPath = cmd.getOptionValue("t", "");
    Configuration conf = new Configuration();
    try (PrintStream out = outputFile.equals("-") ? System.out : new PrintStream(outputFile, "UTF-8")) {
        switch(processor) {
            case "FileDistribution":
                long maxSize = Long.parseLong(cmd.getOptionValue("maxSize", "0"));
                int step = Integer.parseInt(cmd.getOptionValue("step", "0"));
                boolean formatOutput = cmd.hasOption("format");
                new FileDistributionCalculator(conf, maxSize, step, formatOutput, out).visit(new RandomAccessFile(inputFile, "r"));
                break;
            case "XML":
                new PBImageXmlWriter(conf, out).visit(new RandomAccessFile(inputFile, "r"));
                break;
            case "ReverseXML":
                try {
                    OfflineImageReconstructor.run(inputFile, outputFile);
                } catch (Exception e) {
                    System.err.println("OfflineImageReconstructor failed: " + e.getMessage());
                    e.printStackTrace(System.err);
                    System.exit(1);
                }
                break;
            case "Web":
                String addr = cmd.getOptionValue("addr", "localhost:5978");
                try (WebImageViewer viewer = new WebImageViewer(NetUtils.createSocketAddr(addr))) {
                    viewer.start(inputFile);
                }
                break;
            case "Delimited":
                try (PBImageDelimitedTextWriter writer = new PBImageDelimitedTextWriter(out, delimiter, tempPath)) {
                    writer.visit(new RandomAccessFile(inputFile, "r"));
                }
                break;
            default:
                System.err.println("Invalid processor specified : " + processor);
                printUsage();
                return -1;
        }
        return 0;
    } catch (EOFException e) {
        System.err.println("Input file ended unexpectedly. Exiting");
    } catch (IOException e) {
        System.err.println("Encountered exception.  Exiting: " + e.getMessage());
        e.printStackTrace(System.err);
    }
    return -1;
}
Also used : Options(org.apache.commons.cli.Options) PrintStream(java.io.PrintStream) Configuration(org.apache.hadoop.conf.Configuration) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 33 with Options

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

the class OfflineImageViewer method main.

/**
   * Entry point to command-line-driven operation.  User may specify
   * options and start fsimage viewer from the command line.  Program
   * will process image file and exit cleanly or, if an error is
   * encountered, inform user and exit.
   *
   * @param args Command line options
   * @throws IOException 
   */
public static void main(String[] args) throws IOException {
    Options options = buildOptions();
    if (args.length == 0) {
        printUsage();
        return;
    }
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Error parsing command-line options: ");
        printUsage();
        return;
    }
    if (cmd.hasOption("h")) {
        // print help and exit
        printUsage();
        return;
    }
    boolean skipBlocks = cmd.hasOption("skipBlocks");
    boolean printToScreen = cmd.hasOption("printToScreen");
    String inputFile = cmd.getOptionValue("i");
    String processor = cmd.getOptionValue("p", "Ls");
    String outputFile = cmd.getOptionValue("o");
    String delimiter = cmd.getOptionValue("delimiter");
    if (!(delimiter == null || processor.equals("Delimited"))) {
        System.out.println("Can only specify -delimiter with Delimited processor");
        printUsage();
        return;
    }
    ImageVisitor v;
    if (processor.equals("Indented")) {
        v = new IndentedImageVisitor(outputFile, printToScreen);
    } else if (processor.equals("XML")) {
        v = new XmlImageVisitor(outputFile, printToScreen);
    } else if (processor.equals("Delimited")) {
        v = delimiter == null ? new DelimitedImageVisitor(outputFile, printToScreen) : new DelimitedImageVisitor(outputFile, printToScreen, delimiter);
        skipBlocks = false;
    } else if (processor.equals("FileDistribution")) {
        long maxSize = Long.parseLong(cmd.getOptionValue("maxSize", "0"));
        int step = Integer.parseInt(cmd.getOptionValue("step", "0"));
        boolean formatOutput = cmd.hasOption("format");
        v = new FileDistributionVisitor(outputFile, maxSize, step, formatOutput);
    } else if (processor.equals("NameDistribution")) {
        v = new NameDistributionVisitor(outputFile, printToScreen);
    } else {
        v = new LsImageVisitor(outputFile, printToScreen);
        skipBlocks = false;
    }
    try {
        OfflineImageViewer d = new OfflineImageViewer(inputFile, v, skipBlocks);
        d.go();
    } catch (EOFException e) {
        System.err.println("Input file ended unexpectedly.  Exiting");
    } catch (IOException e) {
        System.err.println("Encountered exception.  Exiting: " + e.getMessage());
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) CommandLine(org.apache.commons.cli.CommandLine) EOFException(java.io.EOFException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 34 with Options

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

the class OfflineImageViewer method buildOptions.

/**
   * Build command-line options and descriptions
   */
public static Options buildOptions() {
    Options options = new Options();
    // Build in/output file arguments, which are required, but there is no 
    // addOption method that can specify this
    OptionBuilder.isRequired();
    OptionBuilder.hasArgs();
    OptionBuilder.withLongOpt("outputFile");
    options.addOption(OptionBuilder.create("o"));
    OptionBuilder.isRequired();
    OptionBuilder.hasArgs();
    OptionBuilder.withLongOpt("inputFile");
    options.addOption(OptionBuilder.create("i"));
    options.addOption("p", "processor", true, "");
    options.addOption("h", "help", false, "");
    options.addOption("maxSize", true, "");
    options.addOption("step", true, "");
    options.addOption("format", false, "");
    options.addOption("skipBlocks", false, "");
    options.addOption("printToScreen", false, "");
    options.addOption("delimiter", true, "");
    return options;
}
Also used : Options(org.apache.commons.cli.Options)

Example 35 with Options

use of org.apache.commons.cli.Options 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)

Aggregations

Options (org.apache.commons.cli.Options)1086 CommandLine (org.apache.commons.cli.CommandLine)557 CommandLineParser (org.apache.commons.cli.CommandLineParser)382 ParseException (org.apache.commons.cli.ParseException)341 Option (org.apache.commons.cli.Option)325 HelpFormatter (org.apache.commons.cli.HelpFormatter)275 GnuParser (org.apache.commons.cli.GnuParser)207 DefaultParser (org.apache.commons.cli.DefaultParser)166 Test (org.junit.Test)148 PosixParser (org.apache.commons.cli.PosixParser)135 IOException (java.io.IOException)118 File (java.io.File)97 OptionGroup (org.apache.commons.cli.OptionGroup)56 DMLScript (org.apache.sysml.api.DMLScript)56 Path (org.apache.hadoop.fs.Path)54 ArrayList (java.util.ArrayList)38 BasicParser (org.apache.commons.cli.BasicParser)36 Properties (java.util.Properties)33 Configuration (org.apache.hadoop.conf.Configuration)31 FileInputStream (java.io.FileInputStream)29