Example usage for org.apache.commons.cli BasicParser parse

List of usage examples for org.apache.commons.cli BasicParser parse

Introduction

In this page you can find the example usage for org.apache.commons.cli BasicParser parse.

Prototype

public CommandLine parse(Options options, String[] arguments) throws ParseException 

Source Link

Document

Parses the specified arguments based on the specifed Options .

Usage

From source file:org.xwoot.xwootserverpeer.ConcertoSuperPeer.java

/**
 * Parse the command line arguments.//from  w  w  w . j a v  a 2 s .  c om
 * 
 * @param args the arguments to parse.
 * @throws ParseException if problems occur while parsing.
 */
public void parseArgs(String[] args) throws ParseException {

    BasicParser parser = new BasicParser();
    CommandLine cl = parser.parse(this.options, args);

    if (cl.hasOption(HELP_PARAMETER)) {
        showUsage();
        System.exit(0);
    }

    String rdvSeedingUriString = cl.getOptionValue(RDV_SEEDING_URI_PARAMETER);
    String relaySeedingUriString = cl.getOptionValue(RELAY_SEEDING_URI_PARAMETER);
    this.modeRendezvous = (cl.hasOption(MODE_RENDEZVOUS_PARAMETER) != this.modeRendezvous
            ? cl.hasOption(MODE_RENDEZVOUS_PARAMETER)
            : this.modeRendezvous);
    this.modeRelay = cl.hasOption(MODE_RELAY_PARAMETER);
    this.peerName = cl.getOptionValue(PEER_NAME_PARAMETER);
    this.homePath = cl.getOptionValue(HOME_PARAMETER);
    String rdvSeedsString = cl.getOptionValue(RDV_SEEDS_PARAMETER);
    String relaySeedsString = cl.getOptionValue(RELAY_SEEDS_PARAMETER);

    this.useTcp = cl.hasOption(USE_TCP_PARAMETER);
    String tcpPortString = cl.getOptionValue(TCP_PORT_PARAMETER);

    this.useHttp = cl.hasOption(USE_HTTP_PARAMETER);
    String httpPortString = cl.getOptionValue(HTTP_PORT_PARAMETER);

    this.externalIp = cl.getOptionValue(EXTERNAL_IP_PARAMETER);
    this.useOnlyExternalIp = cl.hasOption(ONLY_EXTERNAL_IP_PARAMETER);

    this.clean = cl.hasOption(CLEAN_EXISTING_CONFIG_PARAMETER);
    this.useMulticast = cl.hasOption(USE_MULTICAST_PARAMETER);

    String infrastructureIDString = cl.getOptionValue(INFRASTRUCTURE_ID);
    this.infrastructureName = cl.getOptionValue(INFRASTRUCTURE_NAME);

    if (!useTcp && !useHttp) {
        throw new ParseException("At least one communication method (TCP and/or HTTP) must be chosen.");
    }

    if (useTcp) {
        if (tcpPortString != null && tcpPortString.trim().length() != 0) {
            try {
                this.tcpPort = Integer.parseInt(tcpPortString);
                if (this.tcpPort <= 0) {
                    throw new ParseException("TCP port number must be greater than 0.");
                }
            } catch (NumberFormatException e) {
                throw new ParseException("Invalid TCP port: " + tcpPortString);
            }
        } else {
            throw new ParseException("No TCP port provided.");
        }
    }

    if (useHttp) {
        if (httpPortString != null && httpPortString.trim().length() != 0) {
            try {
                this.httpPort = Integer.parseInt(httpPortString);
                if (this.httpPort <= 0) {
                    throw new ParseException("HTTP port number must be greater than 0.");
                }
            } catch (NumberFormatException e) {
                throw new ParseException("Invalid HTTP port.");
            }
        } else {
            throw new ParseException("No HTTP port provided.");
        }
    }

    if (!modeRendezvous && !modeRelay) {
        throw new ParseException("This peer must run in relay, rendezvous or both modes.");
    }

    String delimiter = ",";
    if (rdvSeedsString != null) {
        this.rdvSeeds = rdvSeedsString.split(delimiter);
    }

    if (relaySeedsString != null) {
        this.relaySeeds = relaySeedsString.split(delimiter);
    }

    if (rdvSeedingUriString != null) {
        this.rdvSeedingUris = rdvSeedingUriString.split(delimiter);
    }

    if (relaySeedsString != null) {
        this.relaySeedingUris = relaySeedingUriString.split(delimiter);
    }

    // Check if at least one rdv seed/seedingUri exists if this peer is a relay.
    if (!modeRendezvous
            && (rdvSeedingUriString == null || rdvSeedingUris.length == 0
                    || (rdvSeedingUris.length == 1 && rdvSeedingUris[0].trim().length() == 0))
            && (rdvSeedsString == null || rdvSeeds.length == 0
                    || (rdvSeeds.length == 1 && rdvSeeds[0].length() == 0))) {
        throw new ParseException("Must specify at least one RendezVous seed or RendezVous seeding URI.");
    }

    // Rdv Seeding Uris
    if (this.rdvSeedingUris != null) {
        for (String rdvSeedingUri : rdvSeedingUris) {
            if (rdvSeedingUri != null && rdvSeedingUri.trim().length() != 0) {
                try {
                    URI seedingUri = new URI(rdvSeedingUri);
                    String scheme = seedingUri.getScheme();
                    String host = seedingUri.getHost();
                    if (host == null || scheme == null) {
                        throw new Exception();
                    }
                } catch (Exception e) {
                    throw new ParseException(
                            rdvSeedingUri + " is not a valid location for retrieving RendezVous seeds.");
                }
            }
        }
    }

    // Relay Seeding Uris
    if (relaySeedingUris != null) {
        for (String relaySeedingUri : relaySeedingUris) {
            if (relaySeedingUri != null && relaySeedingUri.trim().length() != 0) {
                try {
                    URI seedUri = new URI(relaySeedingUri);
                    String scheme = seedUri.getScheme();
                    String host = seedUri.getHost();
                    if (host == null || scheme == null) {
                        throw new Exception();
                    }
                } catch (Exception e) {
                    throw new ParseException(
                            relaySeedingUri + " is not a valid location for retrieving relay seeds.");
                }
            }
        }
    }

    // Rdv Seeds
    if (rdvSeeds != null) {
        for (String rdvSeed : rdvSeeds) {
            if (rdvSeed != null && rdvSeed.trim().length() != 0) {
                try {
                    URI seedUri = new URI(rdvSeed);
                    String scheme = seedUri.getScheme();
                    String host = seedUri.getHost();
                    if (host == null || scheme == null) {
                        throw new Exception();
                    } else if (seedUri.getPort() < 1) {
                        throw new ParseException(rdvSeed + " has no port specified.");
                    }
                } catch (Exception e) {
                    throw new ParseException(rdvSeed + " is not a valid RendezVous seed.");
                }
            }
        }
    }

    // Relay Seeds
    if (relaySeeds != null) {
        for (String relaySeed : relaySeeds) {
            if (relaySeed != null && relaySeed.trim().length() != 0) {
                try {
                    URI seedUri = new URI(relaySeed);
                    String scheme = seedUri.getScheme();
                    String host = seedUri.getHost();
                    if (host == null || scheme == null) {
                        throw new Exception();
                    } else if (seedUri.getPort() < 1) {
                        throw new ParseException(relaySeed + " has no port specified.");
                    }
                } catch (Exception e) {
                    throw new ParseException(relaySeed + " is not a valid Relay seed.");
                }
            }
        }
    }

    if (infrastructureIDString != null && infrastructureIDString.length() != 0) {
        try {
            this.infrastructureID = PeerGroupID.create(URI.create(infrastructureIDString));
        } catch (Exception e) {
            throw new ParseException(infrastructureIDString + " is not a valid infrastructure ID.");
        }
    }
}

From source file:salomon.engine.Starter.java

private void parseOptions(String[] args) throws ParseException {
    BasicParser parser = new BasicParser();
    CommandLine cmdLine = parser.parse(_options, args);

    // if any options provided process them
    // otherwise run application in standard way
    if (cmdLine.getArgs().length > 0 || cmdLine.getOptions().length > 0) {
        if (cmdLine.hasOption("h")) {
            usage();/*from ww w.j  a  va  2 s. c o m*/
        } else if (cmdLine.hasOption("f")) {
            try {
                executeBatch(cmdLine.getOptionValue("f"));
            } catch (PlatformException e) {
                LOGGER.fatal(e.getLocalizedMessage());
            }
        } else {
            usage();
        }
    } else {
        LOGGER.info("### Application started ###");
        startLocal();
    }
}

From source file:stereopic.Main.java

public static void main(String args[]) throws IOException {
    Options opt = new Options();
    opt.addOption("h", false, bundle.getString("h"));
    opt.addOption("gif", false, bundle.getString("gif"));
    opt.addOption("si", false, bundle.getString("si"));
    opt.addOption("width", true, bundle.getString("width"));
    opt.addOption("delay", true, bundle.getString("delay"));
    BasicParser parser = new BasicParser();
    CommandLine cl = null;// w  ww.  ja  v  a 2 s.c  om
    try {
        cl = parser.parse(opt, args);
        if (cl.hasOption('h')) {
            printHelpAndExit(opt);
        }
    } catch (ParseException e) {
        printHelpAndExit(opt);
    }
    if (args.length < 1) {
        printHelpAndExit(opt);
    }
    File file = new File(args[0]);
    if (file.isDirectory()) {
        FileFilter ff = new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.isFile() && file.getName().toUpperCase().endsWith(".MPO");
            }
        };
        File[] files = file.listFiles(ff);
        if (files.length == 0) {
            LOG.warn("No .MPO file found.");
            System.exit(-1);
        } else {
            for (File mpo : files) {
                process(mpo.getAbsolutePath(), cl);
            }
        }
    } else if (!file.exists()) {
        LOG.warn(file.getAbsolutePath() + " not found.");
        System.exit(-1);
    } else {
        process(args[0], cl);
    }
}

From source file:ubc.pavlab.aspiredb.cli.AbstractCLI.java

/**
 * This must be called in your main method. It triggers parsing of the command line and processing of the options.
 * Check the error code to decide whether execution of your program should proceed.
 * /*from w  w  w.  j ava2s  .c o m*/
 * @param args
 * @return Exception; null if nothing went wrong.
 * @throws ParseException
 */
protected final Exception processCommandLine(String commandName, String[] args) {
    /* COMMAND LINE PARSER STAGE */
    BasicParser parser = new BasicParser();
    System.err.println("ASPIREdb version " + ConfigUtils.getAppVersion());

    if (args == null) {
        printHelp(commandName);
        return new Exception("No arguments");
    }

    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException e) {
        if (e instanceof MissingOptionException) {
            System.err.println("Required option(s) were not supplied: " + e.getMessage());
        } else if (e instanceof AlreadySelectedException) {
            System.err.println("The option(s) " + e.getMessage() + " were already selected");
        } else if (e instanceof MissingArgumentException) {
            System.err.println("Missing argument: " + e.getMessage());
        } else if (e instanceof UnrecognizedOptionException) {
            System.err.println("Unrecognized option: " + e.getMessage());
        } else {
            e.printStackTrace();
        }

        printHelp(commandName);

        if (log.isDebugEnabled()) {
            log.debug(e);
        }

        return e;
    }

    /* INTERROGATION STAGE */
    if (commandLine.hasOption('h')) {
        printHelp(commandName);
        return new Exception("Help selected");
    }

    processStandardOptions();
    processOptions();

    return null;

}

From source file:ubic.gemma.util.AbstractCLI.java

/**
 * This must be called in your main method. It triggers parsing of the command line and processing of the options.
 * Check the error code to decide whether execution of your program should proceed.
 * /* w ww  .j  a  v  a  2s  .  com*/
 * @param args
 * @return Exception; null if nothing went wrong.
 * @throws ParseException
 */
protected final Exception processCommandLine(String commandName, String[] args) {
    /* COMMAND LINE PARSER STAGE */
    BasicParser parser = new BasicParser();
    System.err.println("Gemma version " + ConfigUtils.getAppVersion());

    if (args == null) {
        printHelp(commandName);
        return new Exception("No arguments");
    }

    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException e) {
        if (e instanceof MissingOptionException) {
            System.err.println("Required option(s) were not supplied: " + e.getMessage());
        } else if (e instanceof AlreadySelectedException) {
            System.err.println("The option(s) " + e.getMessage() + " were already selected");
        } else if (e instanceof MissingArgumentException) {
            System.err.println("Missing argument: " + e.getMessage());
        } else if (e instanceof UnrecognizedOptionException) {
            System.err.println("Unrecognized option: " + e.getMessage());
        } else {
            e.printStackTrace();
        }

        printHelp(commandName);

        if (log.isDebugEnabled()) {
            log.debug(e);
        }

        return e;
    }

    /* INTERROGATION STAGE */
    if (commandLine.hasOption('h')) {
        printHelp(commandName);
        return new Exception("Help selected");
    }

    processStandardOptions();
    processOptions();

    return null;

}

From source file:xmipp.viewer.particlepicker.ParticlePickerParams.java

public void processArgs(String args[]) throws ParseException {

    String[] cmdargs;//from   ww w  .jav a  2  s.  com
    BasicParser parser = new BasicParser();
    cmdLine = parser.parse(options, args);
    inputfile = cmdLine.getOptionValue(INPUTOPT);
    outputdir = cmdLine.getOptionValue(OUTPUTOPT);
    mode = Mode.Manual;
    if (cmdLine.hasOption(MODEOPT)) {
        String str = cmdLine.getOptionValue(MODEOPT);
        mode = Mode.getMode(str);
        if (!(mode == Mode.Review || mode == Mode.ReadOnly))
            throw new IllegalArgumentException(
                    "Only Review or ReadOnly modes can be specified from the command line");
    }
    if (cmdLine.hasOption(THREADSOPT))
        threads = Integer.parseInt(cmdLine.getOptionValue(THREADSOPT));
    if (cmdLine.hasOption(FASTOPT))
        fast = Boolean.parseBoolean(cmdLine.getOptionValue(FASTOPT));
    if (cmdLine.hasOption(INCOREOPT))
        incore = Boolean.parseBoolean(cmdLine.getOptionValue(INCOREOPT));

    if (cmdLine.hasOption(SCIPIONOPT)) {
        cmdargs = cmdLine.getOptionValues(SCIPIONOPT);
        if (cmdargs != null)
            port = Integer.parseInt(cmdargs[0]);
    }
    if (cmdLine.hasOption(CLASSIFIER)) {
        classifierProperties = cmdLine.getOptionValue(CLASSIFIER);

    }
    if (cmdLine.hasOption(TMP)) {
        tmp = Boolean.parseBoolean(cmdLine.getOptionValue(TMP));

    }

}