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

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

Introduction

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

Prototype

public int getLeftPadding() 

Source Link

Document

Returns the 'leftPadding'.

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.
 *//*from ww w  .j  ava2s .com*/
@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.ow2.proactive_grid_cloud_portal.cli.cmd.HelpCommand.java

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    HelpFormatter formatter = new HelpFormatter();
    Writer writer = currentContext.getDevice().getWriter();

    ProActiveVersionUtility.writeProActiveVersionWithBreakEndLine(currentContext, System.out);

    PrintWriter pw = new PrintWriter(writer, true);
    formatter.printHelp(pw, 110, USAGE, "",
            CommandFactory.getCommandFactory(CommandFactory.Type.ALL).supportedOptions(),
            formatter.getLeftPadding(), formatter.getDescPadding(), "", false);
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.cmd.rm.RmHelpCommand.java

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    HelpFormatter formatter = new HelpFormatter();
    Writer writer = currentContext.getDevice().getWriter();
    PrintWriter pw = new PrintWriter(writer, true);
    formatter.printHelp(pw, 110, USAGE, "",
            CommandFactory.getCommandFactory(CommandFactory.Type.RM).supportedOptions(),
            formatter.getLeftPadding(), formatter.getDescPadding(), "", false);
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.cmd.sched.SchedHelpCommand.java

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    HelpFormatter formatter = new HelpFormatter();
    Writer writer = currentContext.getDevice().getWriter();
    PrintWriter pw = new PrintWriter(writer, true);
    Options options = CommandFactory.getCommandFactory(CommandFactory.Type.SCHEDULER).supportedOptions();
    formatter.printHelp(pw, 110, USAGE, "", options, formatter.getLeftPadding(), formatter.getDescPadding(), "",
            false);/*  w  w  w.  jav a 2 s. c o m*/
}

From source file:org.trancecode.xproc.cli.CommandLineExecutor.java

private void printHelp(final PrintStream destination) {
    final HelpFormatter helpFormatter = new HelpFormatter();
    final PrintWriter printWriter = new PrintWriter(destination);
    try {/*from   w  ww.j  a  va 2  s.c  o m*/
        helpFormatter.printHelp(printWriter, helpFormatter.getWidth(), "java -jar tubular-cli.jar", null,
                options, helpFormatter.getLeftPadding(), helpFormatter.getDescPadding(), null, true);
    } finally {
        printWriter.flush();
        printWriter.close();
    }
}

From source file:org.vetmeduni.tools.AbstractTool.java

/**
 * Output to System.err the help for this tool (with the full description)
 *//*w  w w. j  a va  2s .c  o m*/
protected void help() {
    ToolNames tool = ToolNames.valueOf(this.getClass().getSimpleName());
    HelpFormatter formatter = new HelpFormatter();
    PrintWriter writer = new PrintWriter(System.err);
    Main.printProgramHeader(writer);
    writer.println();
    writer.println(String.format("%s: %s", tool, tool.shortDescription));
    writer.println("---");
    formatter.printWrapped(writer, formatter.getWidth(), tool.fullDescription);
    writer.println("---\n");
    formatter.printHelp(writer, formatter.getWidth(), usage(), "\n", programOptions(),
            formatter.getLeftPadding(), formatter.getDescPadding(), "", true);
    writer.close();
}