Example usage for org.apache.commons.cli HelpFormatter printOptions

List of usage examples for org.apache.commons.cli HelpFormatter printOptions

Introduction

In this page you can find the example usage for org.apache.commons.cli HelpFormatter printOptions.

Prototype

public void printOptions(PrintWriter pw, int width, Options options, int leftPad, int descPad) 

Source Link

Document

Print the help for the specified Options to the specified writer, using the specified width, left padding and description padding.

Usage

From source file:org.ldmud.jldmud.CommandLineArguments.java

/**
 * Parse the commandline and set the associated globals.
 *
 * @return {@code true} if the main program should exit; in that case {@link @getExitCode()} provides the suggest exit code.
 *//*w w w.  j  a va2 s  . c o  m*/
@SuppressWarnings("static-access")
public boolean parseCommandline(String[] args) {

    try {
        Option configSetting = OptionBuilder.withLongOpt("config").withArgName("setting=value").hasArgs(2)
                .withValueSeparator()
                .withDescription(
                        "Set the configuration setting to the given value (overrides any setting in the <config settings> file). Unsupported settings are ignored.")
                .create("C");
        Option help = OptionBuilder.withLongOpt("help").withDescription("Print the help text and exits.")
                .create("h");
        Option helpConfig = OptionBuilder.withLongOpt("help-config")
                .withDescription("Print the <config settings> help text and exits.").create();
        Option version = OptionBuilder.withLongOpt("version")
                .withDescription("Print the driver version and exits").create("V");
        Option printConfig = OptionBuilder.withLongOpt("print-config")
                .withDescription("Print the effective configuration settings to stdout and exit.").create();
        Option printLicense = OptionBuilder.withLongOpt("license")
                .withDescription("Print the software license and exit.").create();

        Options options = new Options();
        options.addOption(help);
        options.addOption(helpConfig);
        options.addOption(version);
        options.addOption(configSetting);
        options.addOption(printConfig);
        options.addOption(printLicense);

        CommandLineParser parser = new PosixParser();
        CommandLine line = parser.parse(options, args);

        /* Handle the print-help-and-exit options first, to allow them to be chained in a nice way. */

        boolean helpOptionsGiven = false;

        if (line.hasOption(version.getOpt())) {
            System.out.println(
                    Version.DRIVER_NAME + " " + Version.getVersionString() + " - a LPMud Game Driver.");
            System.out.println(Version.Copyright);
            System.out.print(Version.DRIVER_NAME + " is licensed under the " + Version.License + ".");
            if (!line.hasOption(printLicense.getLongOpt())) {
                System.out.print(" Use option --license for details.");
            }
            System.out.println();
            helpOptionsGiven = true;
        }

        if (line.hasOption(printLicense.getLongOpt())) {
            if (helpOptionsGiven) {
                System.out.println();
            }
            printLicense();
            helpOptionsGiven = true;
        }

        if (line.hasOption(help.getOpt())) {
            final PrintWriter systemOut = new PrintWriter(System.out, true);

            HelpFormatter formatter = new HelpFormatter();

            if (helpOptionsGiven) {
                System.out.println();
            }
            System.out.println("Usage: " + Version.DRIVER_NAME + " [options] [<config settings>]");
            System.out.println();
            formatter.printWrapped(systemOut, formatter.getWidth(),
                    "The <config settings> is a file containing the game settings; if not specified, it defaults to '"
                            + GameConfiguration.DEFAULT_SETTINGS_FILE + "'. "
                            + "The settings file must exist if no configuration setting is specified via commandline argument.");
            System.out.println();
            formatter.printOptions(systemOut, formatter.getWidth(), options, formatter.getLeftPadding(),
                    formatter.getDescPadding());
            helpOptionsGiven = true;
        }

        if (line.hasOption(helpConfig.getLongOpt())) {
            if (helpOptionsGiven) {
                System.out.println();
            }
            GameConfiguration.printTemplate();
            helpOptionsGiven = true;
        }

        if (helpOptionsGiven) {
            exitCode = 0;
            return true;
        }

        /* Parse the real options */

        /* TODO: If we get many real options, it would be useful to implement a more general system like {@link GameConfiguration#SettingBase} */

        if (line.hasOption(configSetting.getLongOpt())) {
            configSettings = line.getOptionProperties(configSetting.getLongOpt());
        }

        if (line.hasOption(printConfig.getLongOpt())) {
            printConfiguration = true;
        }

        if (line.getArgs().length > 1) {
            throw new ParseException("Too many arguments");
        }

        if (line.getArgs().length == 1) {
            settingsFilename = line.getArgs()[0];
        }

        return false;
    } catch (ParseException e) {
        System.err.println("Error: " + e.getMessage());
        exitCode = 1;
        return true;
    }
}

From source file:org.marketcetera.orderloader.OrderLoaderMain.java

/**
 * Prints the usage to the output./*  w  ww .  ja v  a2s .  c o m*/
 */
private void usage() {
    HelpFormatter formatter = new HelpFormatter();
    PrintWriter pw = new PrintWriter(mMsgStream);
    pw.append(ERROR_USAGE.getText());
    pw.println();
    formatter.printOptions(pw, HelpFormatter.DEFAULT_WIDTH, options, HelpFormatter.DEFAULT_LEFT_PAD,
            HelpFormatter.DEFAULT_DESC_PAD);
    pw.println();
    pw.flush();
    exit();
}

From source file:org.ow2.mind.yatl.YATLC.java

protected static void printHelp(PrintStream ps, Options options) {
    PrintWriter pw = new PrintWriter(ps);
    HelpFormatter help = new HelpFormatter();
    pw.println("Usage : yatlc [options] input-file [output-file]");
    pw.println("compile YATL template input-file to java source code");
    pw.println("");
    pw.println("Options:");
    help.printOptions(pw, 80, options, 2, 1);
    pw.flush();//from ww w . j a v  a  2s.  c o m
}

From source file:org.rapidcontext.app.Main.java

/**
 * Exits the application with optional help and/or error messages.
 * This method WILL NOT RETURN.//  w w w  . j a v  a 2 s .  c o  m
 *
 * @param options        the command-line options, or null
 * @param error          the error message, or null
 */
private static void exit(Options options, String error) {
    PrintWriter out = new PrintWriter(System.err);
    HelpFormatter fmt = new HelpFormatter();

    if (options != null) {
        out.println(USAGE);
        out.println();
        out.println("Options:");
        fmt.setOptionComparator(null);
        fmt.printOptions(out, 74, options, 2, 3);
        out.println();
    }
    if (error != null && error.length() > 0) {
        out.println("ERROR:");
        out.print("    ");
        out.println(error);
        out.println();
    }
    out.flush();
    System.exit(error == null ? 0 : 1);
}

From source file:org.springframework.migrationanalyzer.commandline.AbstractMigrationAnalysis.java

private void displayUsage() {
    PrintWriter writer = new PrintWriter(System.out);

    HelpFormatter helpFormatter = new HelpFormatter();

    writer.println(String.format("Usage: migration-analysis.%s <inputPath> [OPTION]...", getScriptSuffix()));
    printHeader("Description:", writer);
    helpFormatter.printWrapped(writer, OPTIONS_WIDTH, OPTIONS_INDENT, DESCRIPTION);
    printHeader("Options:", writer);
    helpFormatter.printOptions(writer, OPTIONS_WIDTH, OPTIONS, OPTIONS_INDENT, OPTIONS_INDENT);

    writer.flush();//w  ww  . j  av  a2s. c  om
}

From source file:org.sybila.parasim.application.ParasimOptions.java

public static void printHelp(PrintStream out) {
    PrintWriter output = new PrintWriter(out);
    output.println();/*from   w ww .j  a v  a 2s .c  o m*/
    HelpFormatter helpFormatter = new HelpFormatter();
    helpFormatter.printUsage(output, HelpFormatter.DEFAULT_WIDTH, "parasim", getOptions());
    output.println();
    helpFormatter.printOptions(output, HelpFormatter.DEFAULT_WIDTH, getOptions(),
            HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD);
    output.println();
    output.flush();
}

From source file:org.tupelo_schneck.electric.Options.java

private void showUsage() {
    StringBuilder header = new StringBuilder();
    header.append("\n");
    header.append("The \"it's electric\" Java program is designed to perform two simultaneous\n");
    header.append("activities:\n");
    header.append("(1) it records data from TED into a permanent database; and\n");
    header.append("(2) it serves data from the database in Google Visualization API format.\n");
    header.append("Additionally, the program can\n");
    header.append("(3) export data from the database in CSV format.\n");
    header.append("\n");
    header.append("To export data, use: java -jar its-electric-*.jar -d <database-directory>\n");
    header.append("                                      --export <start> <end> <resolution>\n");
    header.append("\n");
    header.append("You can specify to only record data using option --no-serve (e.g. for an\n");
    header.append("unattended setup) and to only serve data using option --no-record (e.g. with a\n");
    header.append("static copy of an its-electric database).\n");
    header.append("\n");
    header.append("Options -d (specifying the database directory) and -m (specifying the number of\n");
    header.append("MTUs) are important for both recording and serving.  Option -g (the TED Gateway\n");
    header.append("URL) and options -v and -k (which determine whether to record voltage and\n");
    header.append("volt-amperes) are important for recording.  Option -p (the port on which to\n");
    header.append("serve) is important for serving.  Other options are generally minor.\n");
    header.append("\n");
    header.append("Options (-d is REQUIRED):");

    HelpFormatter help = new HelpFormatter();
    PrintWriter writer = new PrintWriter(System.out);
    writer.println("usage: java -jar its-electric-*.jar [options]");
    writer.println(header.toString());//from   ww  w  .  ja v  a 2 s  . c  o  m
    help.printOptions(writer, 80, this, 0, 0);
    writer.flush();
    writer.close();
}

From source file:parquet.tools.Main.java

public static void showUsage(HelpFormatter format, PrintWriter err, String name, Command command) {
    Options options = mergeOptions(OPTIONS, command.getOptions());
    String[] usage = command.getUsageDescription();

    String ustr = name + " [option...]";
    if (usage != null && usage.length >= 1) {
        ustr = ustr + " " + usage[0];
    }//from   www  .  ja v a2 s  . c  o  m

    format.printUsage(err, WIDTH, ustr);
    format.printWrapped(err, WIDTH, LEFT_PAD, "where option is one of:");
    format.printOptions(err, WIDTH, options, LEFT_PAD, DESC_PAD);

    if (usage != null && usage.length >= 2) {
        for (int i = 1; i < usage.length; ++i) {
            format.printWrapped(err, WIDTH, LEFT_PAD, usage[i]);
        }
    }
}

From source file:pku.sei.checkedcoverage.traceResult.TraceResult.java

private static void printHelp(Options options, PrintStream out) {
    out.println("Usage: " + TraceResult.class.getSimpleName() + " [<options>] <file>");
    out.println("where <file> is the input trace file, and <options> may be one or more of");
    HelpFormatter formatter = new HelpFormatter();
    PrintWriter pw = new PrintWriter(out, true);
    formatter.printOptions(pw, 120, options, 5, 3);
}

From source file:uni.bielefeld.cmg.sparkhit.hadoop.decodec.util.HelpParam.java

/**
 * print out help info with parameters/*from w  ww. j a va  2 s.com*/
 */
public void printHelp() {
    HelpFormatter formatter = new HelpFormatter();
    formatter.setOptionComparator(new Comparator<Option>() {
        public int compare(Option o1, Option o2) {
            return Integer.compare(parameterMap.get(o1.getOpt()), parameterMap.get(o2.getOpt()));
        }
    });

    final String executable = System.getProperty("executable", "Hadoop jar sparkhit-hadoopDecompressor.jar");
    err.println("Name:");
    err.println("\tSparkHit hadoop decompressor");
    err.println();
    err.println("Options:");
    formatter.printOptions(new PrintWriter(err, true), 85, parameter, 2, 3); /* print formatted parameters */
    err.println();
    err.println("Usage:");
    err.println("\tDeompressing BZip2 .bz2 file in parallel using Hadoop Mapper");
    err.println(executable + " [parameters] -bz2 hdfs://master/*/fastq.bz2 -outfile hdfs://master/out_dir");
    err.println(executable + " [parameters] -gz hdfs://master/*/fastq.gz -outfile hdfs://master/out_dir");
    err.println();
}