Example usage for org.apache.commons.cli2 OptionException printStackTrace

List of usage examples for org.apache.commons.cli2 OptionException printStackTrace

Introduction

In this page you can find the example usage for org.apache.commons.cli2 OptionException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:egat.cli.Main.java

public static void main(String[] args) {
    final DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder();
    final GroupBuilder groupBuilder = new GroupBuilder();

    Option help = optionBuilder.withLongName("help").withShortName("h").withDescription("print this message")
            .create();//from   w w  w.  ja v  a2  s. c  o  m
    Option version = optionBuilder.withLongName("version").withShortName("v")
            .withDescription("print the version information and exit").create();

    List<CommandHandler> commandHandlers = createCommandHandlers();

    groupBuilder.withName("command");
    for (CommandHandler handler : commandHandlers) {
        groupBuilder.withOption(handler.getCommand());
    }
    Group commandGroup = groupBuilder.create();

    Group mainOptions = groupBuilder.withName("options").withOption(help).withOption(version).create();

    Group options = groupBuilder.withOption(mainOptions).withOption(commandGroup).create();

    Parser parser = new Parser();
    parser.setGroup(options);

    try {
        CommandLine cl = parser.parse(args);

        if (cl.hasOption(version)) {
            printVersion();
        } else {
            boolean commandTriggered = false;

            for (CommandHandler commandHandler : commandHandlers) {
                if (cl.hasOption(commandHandler.getCommand())) {
                    commandHandler.handleCommand(cl);
                    commandTriggered = true;
                }
            }

            if (!commandTriggered) {
                printHelp(options);
            }
        }

    } catch (OptionException e) {
        e.printStackTrace();
        printHelp(options);
    }
}