Search in sources :

Example 91 with ParseException

use of org.apache.commons.cli.ParseException in project Krill by KorAP.

the class Indexer method main.

/**
 * Main method.
 *
 * @param argv
 *            Argument list,
 *            expecting the properties file
 *            and a list of directories
 * @throws IOException
 */
public static void main(String[] argv) throws IOException {
    Options options = new Options();
    options.addOption(Option.builder("c").longOpt("config").desc("configuration file (defaults to " + KrillProperties.defaultPropertiesLocation + ").").hasArg().argName("properties file").required().build());
    options.addOption(Option.builder("i").longOpt("inputDir").desc("input directories separated by semicolons. The input files " + "have to be in <filename>.json.gz format. ").hasArgs().argName("input directories").required().valueSeparator(new Character(';')).build());
    options.addOption(Option.builder("o").longOpt("outputDir").desc("index output directory (defaults to " + "krill.indexDir in the configuration.").hasArg().argName("output directory").build());
    CommandLineParser parser = new DefaultParser();
    String propFile = null;
    String[] inputDirectories = null;
    try {
        CommandLine cmd = parser.parse(options, argv);
        log.info("Configuration file: " + cmd.getOptionValue("c"));
        propFile = cmd.getOptionValue("c");
        log.info("Input directories: " + StringUtils.join(cmd.getOptionValues("i"), ";"));
        inputDirectories = cmd.getOptionValues("i");
        if (cmd.hasOption("o")) {
            log.info("Output directory: " + cmd.getOptionValue("o"));
            path = cmd.getOptionValue("o");
        }
    } catch (MissingOptionException e) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Krill indexer\n java -jar -c <properties file> -i <input directories> " + "[-o <output directory>]", options);
        return;
    } catch (ParseException e) {
        log.error("Unexpected error: " + e);
        e.printStackTrace();
    }
    // Load properties
    Properties prop = KrillProperties.loadProperties(propFile);
    // Get indexer object
    Indexer indexer = new Indexer(prop);
    // Iterate over list of directories
    for (String arg : inputDirectories) {
        log.info("Indexing files in " + arg);
        File f = new File(arg);
        if (f.isDirectory())
            indexer.parse(f);
    }
    indexer.closeIndex();
    // Final commit
    log.info("Finished indexing.");
    // Finish indexing
    String message = "Indexed " + indexer.count + " file";
    if (indexer.count > 1) {
        message += "s";
    }
    System.out.print(message + ".");
}
Also used : Options(org.apache.commons.cli.Options) KrillProperties(de.ids_mannheim.korap.util.KrillProperties) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 92 with ParseException

use of org.apache.commons.cli.ParseException in project openmeetings by apache.

the class Admin method process.

// package private wrapper for testing
void process(String... args) throws Exception {
    CommandLineParser parser = new DefaultParser();
    try {
        cmdl = parser.parse(opts, args);
    } catch (ParseException e) {
        log(e.getMessage());
        usage();
        throw new ExitException();
    }
    verbose = cmdl.hasOption('v');
    Command cmd = Command.usage;
    if (cmdl.hasOption('i')) {
        cmd = Command.install;
    } else if (cmdl.hasOption('b')) {
        cmd = Command.backup;
    } else if (cmdl.hasOption('r')) {
        cmd = Command.restore;
    } else if (cmdl.hasOption('f')) {
        cmd = Command.files;
    } else if (cmdl.hasOption('l')) {
        cmd = Command.ldap;
    }
    String file = cmdl.getOptionValue("file", "");
    switch(cmd) {
        case install:
            step = "Install";
            processInstall(file);
            break;
        case backup:
            step = "Backup";
            processBackup(file);
            break;
        case restore:
            step = "Restore";
            processRestore(checkRestoreFile(file));
            break;
        case files:
            step = "Files";
            processFiles();
            break;
        case ldap:
            step = "LDAP import";
            processLdap();
            break;
        case usage:
        default:
            usage();
            break;
    }
}
Also used : CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 93 with ParseException

use of org.apache.commons.cli.ParseException in project ff4j by ff4j.

the class FF4jCliProcessor method processCommandUpdateProperty.

private void processCommandUpdateProperty(String commandLine) {
    try {
        CommandLine cmd = CMD_PARSER.parse(propertyOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 1 || !cmd.hasOption("p") || !cmd.hasOption("v")) {
            logError("Invalid command, expecting update -p <property> -v <value>");
        } else {
            String property = cmd.getOptionValue('p');
            String value = cmd.getOptionValue('v');
            if (!currentFF4J.getPropertiesStore().existProperty(property)) {
                logWarn("Property " + property + " does not exist, nothing to update");
            } else {
                currentFF4J.getPropertiesStore().updateProperty(property, value);
                logInfo("Property " + property + " has been updated with " + value);
            }
        }
    } catch (ParseException e) {
        error(e, "parsing error during update property command");
    } catch (Exception e) {
        error(e, "Cannot update property");
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) ParseException(org.apache.commons.cli.ParseException)

Example 94 with ParseException

use of org.apache.commons.cli.ParseException in project ff4j by ff4j.

the class FF4jCliProcessor method processCommandAddGroup.

private void processCommandAddGroup(String commandLine) {
    try {
        CommandLine cmd = CMD_PARSER.parse(addGroupOptions(), commandLine.split(" "));
        if (cmd.getArgList().size() != 1 || !cmd.hasOption("f") || !cmd.hasOption("g")) {
            logError("Invalid command, expecting addToGroup[removeFromGroup] -f <featureName> -g <grouName>");
        } else {
            String feature = cmd.getOptionValue('f');
            String group = cmd.getOptionValue('g');
            if (!currentFF4J.getFeatureStore().exist(feature)) {
                logWarn("Feature does not exist, nothing updated");
            } else {
                if (cmd.getArgList().get(0).equals("addToGroup")) {
                    currentFF4J.getFeatureStore().addToGroup(feature, group);
                    logInfo(FEATURE + feature + " has been added to group " + group);
                } else if (cmd.getArgList().get(0).equals("removeFromGroup")) {
                    String currentGroup = currentFF4J.getFeatureStore().read(feature).getGroup();
                    if (group.equals(currentGroup)) {
                        currentFF4J.getFeatureStore().removeFromGroup(feature, group);
                        logInfo(FEATURE + feature + " has been removed from group: " + group);
                    } else if (currentGroup == null || currentGroup.isEmpty()) {
                        logWarn("The groupName is invalid expected:" + currentGroup + " but was [" + group + "]");
                    } else {
                        logWarn("Cannot remove group: there are no group on this feature");
                    }
                }
            }
        }
    } catch (ParseException e) {
        error(e, "Error during addToGroup/removeFromGroup command");
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException)

Example 95 with ParseException

use of org.apache.commons.cli.ParseException in project java-jotasync by trixon.

the class Main method main.

/**
 * @param args the command line arguments
 * @throws java.rmi.RemoteException
 */
public static void main(String[] args) throws RemoteException {
    SystemHelper.enableRmiServer();
    Options options = initOptions();
    CommandLineParser parser = new DefaultParser();
    try {
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption(Main.OPT_HELP)) {
            displayHelp(options);
        } else if (cmd.hasOption(OPT_VERSION)) {
            displayVersion();
        } else {
            Client client = new Client(cmd);
        }
    } catch (ParseException ex) {
        Xlog.timedErr(ex.getMessage());
        System.out.println(sBundle.getString("parse_help_client"));
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

ParseException (org.apache.commons.cli.ParseException)586 CommandLine (org.apache.commons.cli.CommandLine)488 CommandLineParser (org.apache.commons.cli.CommandLineParser)380 Options (org.apache.commons.cli.Options)370 DefaultParser (org.apache.commons.cli.DefaultParser)220 HelpFormatter (org.apache.commons.cli.HelpFormatter)204 GnuParser (org.apache.commons.cli.GnuParser)173 IOException (java.io.IOException)124 Option (org.apache.commons.cli.Option)109 File (java.io.File)90 PosixParser (org.apache.commons.cli.PosixParser)65 Path (org.apache.hadoop.fs.Path)50 ArrayList (java.util.ArrayList)42 Properties (java.util.Properties)35 BasicParser (org.apache.commons.cli.BasicParser)31 FileInputStream (java.io.FileInputStream)29 Job (org.apache.hadoop.mapreduce.Job)27 Configuration (org.apache.hadoop.conf.Configuration)26 List (java.util.List)25 URI (java.net.URI)21