Example usage for org.apache.commons.cli Option getValue

List of usage examples for org.apache.commons.cli Option getValue

Introduction

In this page you can find the example usage for org.apache.commons.cli Option getValue.

Prototype

public String getValue() 

Source Link

Document

Returns the specified value of this Option or null if there is no value.

Usage

From source file:gobblin.runtime.cli.PublicMethodsGobblinCliFactory.java

/**
 * For each method for which the helper created an {@link Option} and for which the input {@link CommandLine} contains
 * that option, this method will automatically call the method on the input {@link EmbeddedGobblin} with the correct
 * arguments.//from  w ww .  j ava  2 s  . c o  m
 */
public void applyCommandLineOptions(CommandLine cli, EmbeddedGobblin embeddedGobblin) {
    try {
        for (Option option : cli.getOptions()) {
            if (!this.methodsMap.containsKey(option.getOpt())) {
                // Option added by cli driver itself.
                continue;
            }
            if (option.hasArg()) {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin, option.getValue());
            } else {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin);
            }
        }
    } catch (IllegalAccessException | InvocationTargetException exc) {
        throw new RuntimeException("Could not apply options to " + embeddedGobblin.getClass().getName(), exc);
    }
}

From source file:edu.vt.middleware.ldap.AbstractCli.java

/**
 * Initialize the supplied config with command line options.
 *
 * @param  config  property config to configure
 * @param  line  Parsed command line arguments container.
 *
 * @throws  Exception  On errors thrown by handler.
 *///from w  w w .j  a v a 2 s.  c  o  m
protected void initLdapProperties(final PropertyConfig config, final CommandLine line) throws Exception {
    final LdapProperties ldapProperties = new LdapProperties(config);
    for (Option o : line.getOptions()) {
        if (o.getOpt().equals(OPT_USE_PROPERTIES)) {
            ldapProperties.useDefaultPropertiesFile();
        } else if (!this.opts.contains(o.getOpt())) {
            ldapProperties.setProperty(o.getOpt(), o.getValue());
        }
    }
    ldapProperties.configure();
}

From source file:gobblin.runtime.cli.PublicMethodsCliObjectFactory.java

/**
 * For each method for which the helper created an {@link Option} and for which the input {@link CommandLine} contains
 * that option, this method will automatically call the method on the input object with the correct
 * arguments.//from w ww .  j a v a 2s. c  om
 */
public void applyCommandLineOptions(CommandLine cli, T embeddedGobblin) {
    try {
        for (Option option : cli.getOptions()) {
            if (!this.methodsMap.containsKey(option.getOpt())) {
                // Option added by cli driver itself.
                continue;
            }
            if (option.hasArg()) {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin, option.getValue());
            } else {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin);
            }
        }
    } catch (IllegalAccessException | InvocationTargetException exc) {
        throw new RuntimeException("Could not apply options to " + embeddedGobblin.getClass().getName(), exc);
    }
}

From source file:com.github.lindenb.jvarkit.util.command.CommandFactory.java

public int instanceMain(final String args[]) {
    /* create a new  empty Command  */
    Command cmd = null;//w  w  w. j  av a 2  s.  c o  m

    try {
        /* cmd line */
        final StringBuilder sb = new StringBuilder();
        for (String s : args) {
            if (sb.length() != 0)
                sb.append(" ");
            sb.append(s);
        }
        /* create new options  */
        final Options options = new Options();
        /* get all options and add it to options  */
        fillOptions(options);
        /* create a new CLI parser */
        final CommandLineParser parser = new DefaultParser();
        final CommandLine cli = parser.parse(options, args);

        /* loop over the processed options */
        for (final Option opt : cli.getOptions()) {
            if (opt.hasArg() && !opt.hasOptionalArg() && opt.getValue() == null) {
                LOG.warn("OPTION ####" + opt);
            }
            final Status status = visit(opt);
            switch (status) {
            case EXIT_FAILURE:
                return -1;
            case EXIT_SUCCESS:
                return 0;
            default:
                break;
            }
        }
        cmd = createCommand();

        final Date startDate = new Date();
        LOG.info("Starting JOB at " + startDate + " " + getClass().getName() + " version=" + getVersion() + " "
                + " built=" + getCompileDate());
        LOG.info("Command Line args : "
                + (cmd.getProgramCommandLine().isEmpty() ? "(EMPTY/NO-ARGS)" : cmd.getProgramCommandLine()));
        String hostname = "";
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (Exception err) {
            hostname = "host";
        }
        LOG.info("Executing as " + System.getProperty("user.name") + "@" + hostname + " on "
                + System.getProperty("os.name") + " " + System.getProperty("os.version") + " "
                + System.getProperty("os.arch") + "; " + System.getProperty("java.vm.name") + " "
                + System.getProperty("java.runtime.version"));

        LOG.debug("initialize " + cmd);
        final Collection<Throwable> initErrors = cmd.initializeKnime();
        if (!(initErrors == null || initErrors.isEmpty())) {
            for (Throwable err : initErrors) {
                LOG.error(err);
            }
            LOG.fatal("Command initialization failed ");
            return -1;
        }

        LOG.debug("calling " + cmd);
        final Collection<Throwable> errors = cmd.call();
        if (!(errors == null || errors.isEmpty())) {
            for (Throwable err : errors) {
                LOG.error(err);
            }
            LOG.fatal("Command failed ");
            return -1;
        }
        LOG.debug("success " + cmd);
        cmd.disposeKnime();
        final Date endDate = new Date();
        final double elapsedMinutes = (endDate.getTime() - startDate.getTime()) / (1000d * 60d);
        final String elapsedString = new DecimalFormat("#,##0.00").format(elapsedMinutes);
        LOG.info("End JOB  [" + endDate + "] " + getName() + " done. Elapsed time: " + elapsedString
                + " minutes.");
        return 0;
    } catch (org.apache.commons.cli.UnrecognizedOptionException err) {
        LOG.fatal("Unknown command (" + err.getOption() + ") , please check your input.", err);
        return -1;
    } catch (Throwable err) {
        LOG.fatal("cannot build command", err);
        return -1;
    } finally {
        if (cmd != null)
            cmd.cleanup();
    }
}

From source file:cz.pecina.retro.ondra.CommandLineProcessor.java

/**
 * Creates an instance of the command line procssor.
 *
 * @param hardware the hardware object to operate on
 *//*from  w  w w.  j  a va2  s  .c o  m*/
public CommandLineProcessor(final Hardware hardware) {
    log.fine("New CommandLineProcessor creation started");
    this.hardware = hardware;

    // build options
    log.fine("Building options");
    options.addOption(
            Option.builder("?").longOpt("help").desc(Application.getString(this, "option.help")).build());
    options.addOption(
            Option.builder("V").longOpt("version").desc(Application.getString(this, "option.version")).build());
    options.addOption(Option.builder("l").longOpt("language").hasArg().argName("CODE")
            .desc(Application.getString(this, "option.language")).build());
    options.addOption(Option.builder("p").longOpt("pixel-size").hasArg().argName("SIZE")
            .desc(Application.getString(this, "option.pixelSize")).build());
    options.addOption(Option.builder("a").longOpt("address").hasArg().argName("ADDR")
            .desc(Application.getString(this, "option.address")).build());
    options.addOption(Option.builder("o").longOpt("ROM-file").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.ROM")).build());
    options.addOption(Option.builder("b").longOpt("binary").hasArgs().numberOfArgs(2).argName("FILE>,<ADDR")
            .valueSeparator(',').desc(Application.getString(this, "option.binary")).build());
    options.addOption(Option.builder("h").longOpt("intel-hex").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.intelHex")).build());
    options.addOption(Option.builder("x").longOpt("xml").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.xml")).build());
    options.addOption(Option.builder("s").longOpt("snapshot").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.snapshot")).build());
    options.addOption(Option.builder("w").longOpt("write-snapshot").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.writeSnapshot")).build());
    options.addOption(Option.builder("S").longOpt("speed-up").hasArg().argName("FACTOR")
            .desc(Application.getString(this, "option.speedUp")).build());
    options.addOption(
            Option.builder("g").longOpt("opengl").desc(Application.getString(this, "option.openGL")).build());
    options.addOption(Option.builder("G").longOpt("no-opengl")
            .desc(Application.getString(this, "option.noOpenGL")).build());
    log.finer("Options set up");

    // parse the command line
    final CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    try {
        line = parser.parse(options, Parameters.arguments);
    } catch (final Exception exception) {
        usage();
        error();
    }
    log.finer("Command line parsed");

    // load default snapshot if exists
    boolean success = false;
    for (String name : DEFAULT_SNAPSHOT_NAMES) {
        final File defaultSnapshot = new File(name);
        if (defaultSnapshot.canRead()) {
            try {
                new Snapshot(hardware).read(defaultSnapshot);
            } catch (final RuntimeException exception) {
                log.fine("Error reading default snapshot");
                System.out.println(Application.getString(this, "error.errorDefaultShapshot"));
                error();
            }
            log.fine("Default snapshot read from the current directory");
            success = true;
            break;
        }
    }
    if (!success) {
        for (String name : DEFAULT_SNAPSHOT_NAMES) {
            final File defaultSnapshot = new File(new File(System.getProperty("user.home")), name);
            if (defaultSnapshot.canRead()) {
                try {
                    new Snapshot(hardware).read(defaultSnapshot);
                } catch (final Exception exception) {
                    log.fine("Error reading default snapshot");
                    System.out.println(Application.getString(this, "error.errorDefaultShapshot"));
                    error();
                }
                log.fine("Default snapshot read from the user's home directory");
                break;
            }
        }
    }
    log.finer("Default files processed");

    // process the options
    if (!line.getArgList().isEmpty()) {
        usage();
        error();
    }
    if (line.hasOption("?")) {
        log.finer("Processing -?");
        usage();
        System.exit(0);
    }
    if (line.hasOption("V")) {
        log.finer("Processing -V");
        System.out.println(Application.getString(this, "longAppName") + " "
                + Application.getString(this, "version") + " @VERSION@");
        System.exit(0);
    }
    try {
        for (Option option : line.getOptions()) {
            switch (option.getOpt()) {
            case "l":
                log.finer("Processing -l");
                final String language = option.getValue();
                if (!Arrays.asList(GeneralConstants.SUPPORTED_LOCALES).contains(language)) {
                    System.out.println(Application.getString(this, "error.unsupportedLanguage"));
                    error();
                }
                UserPreferences.setLocale(language);
                break;
            case "p":
                log.finer("Processing -p");
                final int pixelSize = Integer.parseInt(option.getValue());
                if (!Arrays.asList(GeneralConstants.PIXEL_SIZES).contains(pixelSize)) {
                    System.out.println(Application.getString(this, "error.unsupportedPixelSize"));
                    error();
                }
                UserPreferences.setPixelSize(pixelSize);
                break;
            case "o":
                log.finer("Processing -o");
                fileNameROM = option.getValue();
                break;
            case "a":
                log.finer("Processing -a");
                int address = Integer.parseInt(option.getValue(), 16);
                if ((address < 0) || (address >= 0x10000)) {
                    System.out.println(Application.getString(this, "error.invalidAddress"));
                    error();
                }
                Parameters.cpu.setPC(address);
                break;
            case "b":
                log.finer("Processing -b");
                File file = new File(option.getValue(0));
                address = Integer.parseInt(option.getValue(1), 16);
                if ((address < 0) || (address >= 0x10000)) {
                    System.out.println(Application.getString(this, "error.invalidAddress"));
                    error();
                }
                new Raw(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file, address);
                break;
            case "h":
                log.finer("Processing -h");
                file = new File(option.getValue());
                new IntelHEX(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file);
                break;
            case "x":
                log.finer("Processing -x");
                file = new File(option.getValue());
                new XML(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file);
                break;
            case "s":
                log.finer("Processing -s");
                file = new File(option.getValue());
                new Snapshot(hardware).read(file);
                break;
            case "w":
                log.finer("Processing -w");
                file = new File(option.getValue());
                new Snapshot(hardware).write(file);
                System.exit(0);
                break;
            case "S":
                log.finer("Processing -S");
                Parameters.speedUp = Integer.parseInt(option.getValue());
                if (Parameters.speedUp < 1) {
                    System.out.println(Application.getString(this, "error.nonPositiveSpeedUp"));
                    error();
                }
                break;
            case "g":
                log.finer("Processing -g");
                Parameters.openGL = true;
                break;
            case "G":
                log.finer("Processing -G");
                Parameters.openGL = false;
                break;
            }
        }
    } catch (final Exception exception) {
        usage();
        error();
    }

    log.fine("New CommandLineProcessor creation completed");
}

From source file:cz.pecina.retro.pmd85.CommandLineProcessor.java

/**
 * Creates an instance of the command line procssor.
 *
 * @param hardware the hardware object to operate on
 *///  w w w  .  ja v a  2 s . com
public CommandLineProcessor(final Hardware hardware) {
    log.fine("New CommandLineProcessor creation started");
    this.hardware = hardware;

    // build options
    log.fine("Building options");
    options.addOption(
            Option.builder("?").longOpt("help").desc(Application.getString(this, "option.help")).build());
    options.addOption(
            Option.builder("V").longOpt("version").desc(Application.getString(this, "option.version")).build());
    options.addOption(Option.builder("l").longOpt("language").hasArg().argName("CODE")
            .desc(Application.getString(this, "option.language")).build());
    options.addOption(Option.builder("p").longOpt("pixel-size").hasArg().argName("SIZE")
            .desc(Application.getString(this, "option.pixelSize")).build());
    options.addOption(Option.builder("a").longOpt("address").hasArg().argName("ADDR")
            .desc(Application.getString(this, "option.address")).build());
    options.addOption(Option.builder("o").longOpt("ROM-file").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.ROM")).build());
    options.addOption(Option.builder("m").longOpt("RMM-file").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.RMM")).build());
    options.addOption(Option.builder("b").longOpt("binary").hasArgs().numberOfArgs(2).argName("FILE>,<ADDR")
            .valueSeparator(',').desc(Application.getString(this, "option.binary")).build());
    options.addOption(Option.builder("h").longOpt("intel-hex").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.intelHex")).build());
    options.addOption(Option.builder("x").longOpt("xml").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.xml")).build());
    options.addOption(Option.builder("s").longOpt("snapshot").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.snapshot")).build());
    options.addOption(Option.builder("w").longOpt("write-snapshot").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.writeSnapshot")).build());
    options.addOption(Option.builder("S").longOpt("speed-up").hasArg().argName("FACTOR")
            .desc(Application.getString(this, "option.speedUp")).build());
    options.addOption(
            Option.builder("g").longOpt("opengl").desc(Application.getString(this, "option.openGL")).build());
    options.addOption(Option.builder("G").longOpt("no-opengl")
            .desc(Application.getString(this, "option.noOpenGL")).build());
    log.finer("Options set up");

    // parse the command line
    final CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    try {
        line = parser.parse(options, Parameters.arguments);
    } catch (final Exception exception) {
        usage();
        error();
    }
    log.finer("Command line parsed");

    // load default snapshot if exists
    boolean success = false;
    for (String name : DEFAULT_SNAPSHOT_NAMES) {
        final File defaultSnapshot = new File(name);
        if (defaultSnapshot.canRead()) {
            try {
                new Snapshot(hardware).read(defaultSnapshot);
            } catch (final RuntimeException exception) {
                log.fine("Error reading default snapshot");
                System.out.println(Application.getString(this, "error.errorDefaultShapshot"));
                error();
            }
            log.fine("Default snapshot read from the current directory");
            success = true;
            break;
        }
    }
    if (!success) {
        for (String name : DEFAULT_SNAPSHOT_NAMES) {
            final File defaultSnapshot = new File(new File(System.getProperty("user.home")), name);
            if (defaultSnapshot.canRead()) {
                try {
                    new Snapshot(hardware).read(defaultSnapshot);
                } catch (final Exception exception) {
                    log.fine("Error reading default snapshot");
                    System.out.println(Application.getString(this, "error.errorDefaultShapshot"));
                    error();
                }
                log.fine("Default snapshot read from the user's home directory");
                break;
            }
        }
    }
    log.finer("Default files processed");

    // process the options
    if (!line.getArgList().isEmpty()) {
        usage();
        error();
    }
    if (line.hasOption("?")) {
        log.finer("Processing -?");
        usage();
        System.exit(0);
    }
    if (line.hasOption("V")) {
        log.finer("Processing -V");
        System.out.println(Application.getString(this, "longAppName") + " "
                + Application.getString(this, "version") + " @VERSION@");
        System.exit(0);
    }
    try {
        for (Option option : line.getOptions()) {
            switch (option.getOpt()) {
            case "l":
                log.finer("Processing -l");
                final String language = option.getValue();
                if (!Arrays.asList(GeneralConstants.SUPPORTED_LOCALES).contains(language)) {
                    System.out.println(Application.getString(this, "error.unsupportedLanguage"));
                    error();
                }
                UserPreferences.setLocale(language);
                break;
            case "p":
                log.finer("Processing -p");
                final int pixelSize = Integer.parseInt(option.getValue());
                if (!Arrays.asList(GeneralConstants.PIXEL_SIZES).contains(pixelSize)) {
                    System.out.println(Application.getString(this, "error.unsupportedPixelSize"));
                    error();
                }
                UserPreferences.setPixelSize(pixelSize);
                break;
            case "o":
                log.finer("Processing -o");
                fileNameROM = option.getValue();
                break;
            case "m":
                log.finer("Processing -m");
                fileNameRMM = option.getValue();
                break;
            case "a":
                log.finer("Processing -a");
                int address = Integer.parseInt(option.getValue(), 16);
                if ((address < 0) || (address >= 0x10000)) {
                    System.out.println(Application.getString(this, "error.invalidAddress"));
                    error();
                }
                Parameters.cpu.setPC(address);
                break;
            case "b":
                log.finer("Processing -b");
                File file = new File(option.getValue(0));
                address = Integer.parseInt(option.getValue(1), 16);
                if ((address < 0) || (address >= 0x10000)) {
                    System.out.println(Application.getString(this, "error.invalidAddress"));
                    error();
                }
                new Raw(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file, address);
                break;
            case "h":
                log.finer("Processing -h");
                file = new File(option.getValue());
                new IntelHEX(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file);
                break;
            case "x":
                log.finer("Processing -x");
                file = new File(option.getValue());
                new XML(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file);
                break;
            case "s":
                log.finer("Processing -s");
                file = new File(option.getValue());
                new Snapshot(hardware).read(file);
                break;
            case "w":
                log.finer("Processing -w");
                file = new File(option.getValue());
                new Snapshot(hardware).write(file);
                System.exit(0);
                break;
            case "S":
                log.finer("Processing -S");
                Parameters.speedUp = Integer.parseInt(option.getValue());
                if (Parameters.speedUp < 1) {
                    System.out.println(Application.getString(this, "error.nonPositiveSpeedUp"));
                    error();
                }
                break;
            case "g":
                log.finer("Processing -g");
                Parameters.openGL = true;
                break;
            case "G":
                log.finer("Processing -G");
                Parameters.openGL = false;
                break;
            }
        }
    } catch (final Exception exception) {
        usage();
        error();
    }

    log.fine("New CommandLineProcessor creation completed");
}

From source file:cz.pecina.retro.pmi80.CommandLineProcessor.java

/**
 * Creates an instance of the command line procssor.
 *
 * @param hardware the hardware object to operate on
 *//*from w  w w .ja v  a  2  s . c  om*/
public CommandLineProcessor(final Hardware hardware) {
    log.fine("New CommandLineProcessor creation started");
    this.hardware = hardware;

    // build options
    log.fine("Building options");
    options.addOption(
            Option.builder("?").longOpt("help").desc(Application.getString(this, "option.help")).build());
    options.addOption(
            Option.builder("V").longOpt("version").desc(Application.getString(this, "option.version")).build());
    options.addOption(Option.builder("l").longOpt("language").hasArg().argName("CODE")
            .desc(Application.getString(this, "option.language")).build());
    options.addOption(Option.builder("p").longOpt("pixel-size").hasArg().argName("SIZE")
            .desc(Application.getString(this, "option.pixelSize")).build());
    options.addOption(Option.builder("a").longOpt("address").hasArg().argName("ADDR")
            .desc(Application.getString(this, "option.address")).build());
    options.addOption(Option.builder("O").longOpt("start-rom").hasArg().argName("ADDR")
            .desc(Application.getString(this, "option.startRom")).build());
    options.addOption(Option.builder("A").longOpt("start-ram").hasArg().argName("ADDR")
            .desc(Application.getString(this, "option.startRam")).build());
    options.addOption(Option.builder("b").longOpt("binary").hasArgs().numberOfArgs(2).argName("FILE>,<ADDR")
            .valueSeparator(',').desc(Application.getString(this, "option.binary")).build());
    options.addOption(Option.builder("h").longOpt("intel-hex").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.intelHex")).build());
    options.addOption(Option.builder("x").longOpt("xml").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.xml")).build());
    options.addOption(Option.builder("s").longOpt("snapshot").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.snapshot")).build());
    options.addOption(Option.builder("w").longOpt("write-snapshot").hasArg().argName("FILE")
            .desc(Application.getString(this, "option.writeSnapshot")).build());
    options.addOption(Option.builder("S").longOpt("speed-up").hasArg().argName("FACTOR")
            .desc(Application.getString(this, "option.speedUp")).build());
    options.addOption(
            Option.builder("g").longOpt("opengl").desc(Application.getString(this, "option.openGL")).build());
    options.addOption(Option.builder("G").longOpt("no-opengl")
            .desc(Application.getString(this, "option.noOpenGL")).build());
    log.finer("Options set up");

    // parse the command line
    final CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    try {
        line = parser.parse(options, Parameters.arguments);
    } catch (final Exception exception) {
        usage();
        error();
    }
    log.finer("Command line parsed");

    // load default snapshot if exists
    boolean success = false;
    for (String name : DEFAULT_SNAPSHOT_NAMES) {
        final File defaultSnapshot = new File(name);
        if (defaultSnapshot.canRead()) {
            try {
                new Snapshot(hardware).read(defaultSnapshot);
            } catch (final Exception exception) {
                log.fine("Error reading default snapshot");
                System.out.println(Application.getString(this, "error.errorDefaultShapshot"));
                error();
            }
            log.fine("Default snapshot read from the current directory");
            success = true;
            break;
        }
    }
    if (!success) {
        for (String name : DEFAULT_SNAPSHOT_NAMES) {
            final File defaultSnapshot = new File(new File(System.getProperty("user.home")), name);
            if (defaultSnapshot.canRead()) {
                try {
                    new Snapshot(hardware).read(defaultSnapshot);
                } catch (final RuntimeException exception) {
                    log.fine("Error reading default snapshot");
                    System.out.println(Application.getString(this, "error.errorDefaultShapshot"));
                    error();
                }
                log.fine("Default snapshot read from the user's home directory");
                break;
            }
        }
    }
    log.finer("Default files processed");

    // process the options
    if (!line.getArgList().isEmpty()) {
        usage();
        error();
    }
    if (line.hasOption("?")) {
        log.finer("Processing -?");
        usage();
        System.exit(0);
    }
    if (line.hasOption("V")) {
        log.finer("Processing -V");
        System.out.println(Application.getString(this, "longAppName") + " "
                + Application.getString(this, "version") + " @VERSION@");
        System.exit(0);
    }
    try {
        for (Option option : line.getOptions()) {
            switch (option.getOpt()) {
            case "l":
                log.finer("Processing -l");
                final String language = option.getValue();
                if (!Arrays.asList(GeneralConstants.SUPPORTED_LOCALES).contains(language)) {
                    System.out.println(Application.getString(this, "error.unsupportedLanguage"));
                    error();
                }
                UserPreferences.setLocale(language);
                break;
            case "p":
                log.finer("Processing -p");
                final int pixelSize = Integer.parseInt(option.getValue());
                if (!Arrays.asList(GeneralConstants.PIXEL_SIZES).contains(pixelSize)) {
                    System.out.println(Application.getString(this, "error.unsupportedPixelSize"));
                    error();
                }
                UserPreferences.setPixelSize(pixelSize);
                break;
            case "a":
                log.finer("Processing -a");
                int address = Integer.parseInt(option.getValue(), 16);
                if ((address < 0) || (address >= 0x10000)) {
                    System.out.println(Application.getString(this, "error.invalidAddress"));
                    error();
                }
                Parameters.cpu.setPC(address);
                break;
            case "O":
                log.finer("Processing -O");
                final int startROM = Integer.parseInt(option.getValue());
                if ((startROM < 0) || (startROM > 64)) {
                    System.out.println(Application.getString(this, "error.unsupportedMemoryStart"));
                    error();
                }
                UserPreferences.setStartROM(startROM);
                break;
            case "A":
                log.finer("Processing -A");
                final int startRAM = Integer.parseInt(option.getValue());
                if ((startRAM < 0) || (startRAM > 64)) {
                    System.out.println(Application.getString(this, "error.unsupportedMemoryStart"));
                    error();
                }
                UserPreferences.setStartRAM(startRAM);
                break;
            case "b":
                log.finer("Processing -b");
                File file = new File(option.getValue(0));
                address = Integer.parseInt(option.getValue(1), 16);
                if ((address < 0) || (address >= 0x10000)) {
                    System.out.println(Application.getString(this, "error.invalidAddress"));
                    error();
                }
                new Raw(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file, address);
                break;
            case "h":
                log.finer("Processing -h");
                file = new File(option.getValue());
                new IntelHEX(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file);
                break;
            case "x":
                log.finer("Processing -x");
                file = new File(option.getValue());
                new XML(hardware, DEFAULT_BANK, DEFAULT_BANK).read(file);
                break;
            case "s":
                log.finer("Processing -s");
                file = new File(option.getValue());
                new Snapshot(hardware).read(file);
                break;
            case "w":
                log.finer("Processing -w");
                file = new File(option.getValue());
                new Snapshot(hardware).write(file);
                System.exit(0);
                break;
            case "S":
                log.finer("Processing -S");
                Parameters.speedUp = Integer.parseInt(option.getValue());
                if (Parameters.speedUp < 1) {
                    System.out.println(Application.getString(this, "error.nonPositiveSpeedUp"));
                    error();
                }
                break;
            case "g":
                log.finer("Processing -g");
                Parameters.openGL = true;
                break;
            case "G":
                log.finer("Processing -G");
                Parameters.openGL = false;
                break;
            }
        }
    } catch (final Exception exception) {
        usage();
        error();
    }

    log.fine("New CommandLineProcessor creation completed");
}

From source file:com.github.lindenb.jvarkit.util.command.Command.java

public int instanceMainWithExceptions(final String args[], final List<Throwable> putExceptionsHere) {
    try {/*from   w  w w.  j a  va  2  s  .  c  om*/
        /* cmd line */
        final StringBuilder sb = new StringBuilder();
        for (String s : args) {
            if (sb.length() != 0)
                sb.append(" ");
            sb.append(s);
        }
        this.programCommandLine = sb.toString().replace('\n', ' ').replace('\t', ' ').replace('\"', ' ')
                .replace('\'', ' ');

        /* create new options  */
        final Options options = new Options();
        /* get all options and add it to options  */
        LOG.debug("creating options");
        fillOptions(options);
        /* create a new CLI parser */
        LOG.debug("parsing options");
        final CommandLineParser parser = new DefaultParser();
        this._cliCommand = parser.parse(options, args);
        this.setInputFiles(this._cliCommand.getArgList());

        /* loop over the processed options */
        for (final Option opt : this._cliCommand.getOptions()) {
            if (opt.hasArg() && !opt.hasOptionalArg() && opt.getValue() == null) {
                LOG.info("WARNING OPTION ####" + opt + " with null value ??");
            }
            final Status status = visit(opt);
            switch (status) {
            case EXIT_FAILURE:
                return -1;
            case EXIT_SUCCESS:
                return 0;
            default:
                break;
            }
        }

        final Date startDate = new Date();
        LOG.info("Starting JOB at " + startDate + " " + getClass().getName() + " version=" + getVersion() + " "
                + " built=" + getCompileDate());
        LOG.info("Command Line args : "
                + (this.getProgramCommandLine().isEmpty() ? "(EMPTY/NO-ARGS)" : this.getProgramCommandLine()));
        String hostname = "";
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (Exception err) {
            hostname = "host";
        }
        LOG.info("Executing as " + System.getProperty("user.name") + "@" + hostname + " on "
                + System.getProperty("os.name") + " " + System.getProperty("os.version") + " "
                + System.getProperty("os.arch") + "; " + System.getProperty("java.vm.name") + " "
                + System.getProperty("java.runtime.version"));

        LOG.debug("initialize");
        final Collection<Throwable> initErrors = this.initializeKnime();
        if (!(initErrors == null || initErrors.isEmpty())) {
            putExceptionsHere.addAll(initErrors);
            for (Throwable err : initErrors) {
                LOG.error(err.getMessage());
            }
            LOG.error("Command initialization failed ");
            return -1;
        }
        LOG.debug("calling");
        final Collection<Throwable> errors = this.call();
        if (!(errors == null || errors.isEmpty())) {
            putExceptionsHere.addAll(initErrors);
            for (Throwable err : errors) {
                LOG.error(err.getMessage());
            }
            LOG.error("Command failed ");
            return -1;
        }
        LOG.debug("success ");
        this.disposeKnime();
        final Date endDate = new Date();
        final double elapsedMinutes = (endDate.getTime() - startDate.getTime()) / (1000d * 60d);
        final String elapsedString = new DecimalFormat("#,##0.00").format(elapsedMinutes);
        LOG.info("End JOB  [" + endDate + "] " + getName() + " done. Elapsed time: " + elapsedString
                + " minutes.");
        return 0;
    } catch (org.apache.commons.cli.UnrecognizedOptionException err) {
        LOG.error("Unknown command (" + err.getOption() + ") , please check your input.", err);
        return -1;
    } catch (Throwable err) {
        LOG.error("cannot execute command", err);
        return -1;
    } finally {
        this.disposeKnime();
    }
}

From source file:com.greenpepper.maven.runner.CommandLineRunner.java

@SuppressWarnings("unchecked")
private List<String> parseCommandLine(String[] args)
        throws ArgumentMissingException, IOException, ParseException {
    List<String> parameters = new ArrayList<String>();
    CommandLine commandLine = argumentsParser.parse(args);
    if (commandLine != null) {
        Option[] options = commandLine.getOptions();
        for (Option option : options) {
            if ("v".equals(option.getOpt())) {
                isDebug = true;/*from www  . j av  a  2  s . c o m*/
                parameters.add("--debug");
            } else if ("p".equals(option.getOpt())) {
                projectDependencyDescriptor = option.getValue();
            } else if ("m".equals(option.getOpt())) {
                usingScopes(option.getValue());
            } else if ("o".equals(option.getOpt())) {
                parameters.add("-" + option.getOpt());
                parameters.add(option.getValue());
            } else if ("r".equals(option.getOpt())) {
                parameters.add("-" + option.getOpt());
                parameters.add(option.getValue());
            } else {
                parameters.add("--" + option.getLongOpt());
                if (option.hasArg()) {
                    parameters.add(option.getValue());
                }
            }
        }
        parameters.addAll(commandLine.getArgList());
    }
    return parameters;
}

From source file:com.github.lindenb.jvarkit.util.command.CommandFactory.java

protected Status visit(final Option opt) {
    if (opt.getOpt().equals("version")) {
        stdout().println(getVersion());/*from   w ww  .j  a  va  2  s .c om*/
        return Status.EXIT_SUCCESS;
    } else if (opt.getOpt().equals("h")) {
        usage(stdout());
        return Status.EXIT_SUCCESS;
    } else if (opt.getOpt().equals("proxy")) {
        String proxy = opt.getValue();
        int colon = proxy.lastIndexOf(':');
        if (colon == -1) {
            LOG.error("bad proxy \"" + proxy + "\"");
            return Status.EXIT_FAILURE;
        }
        LOG.debug("setting proxy " + proxy);
        System.setProperty("http.proxyHost", proxy.substring(0, colon));
        System.setProperty("http.proxyPort", proxy.substring(colon + 1));
        System.setProperty("https.proxyHost", proxy.substring(0, colon));
        System.setProperty("https.proxyPort", proxy.substring(colon + 1));
        return Status.OK;
    }
    LOG.fatal("Unknown option " + opt.getOpt());
    return Status.EXIT_FAILURE;
}