Search in sources :

Example 86 with ParseException

use of org.apache.commons.cli.ParseException in project cdap by caskdata.

the class FlowQueuePendingCorrector method parseArgs.

private static CommandLine parseArgs(String[] args) {
    Options options = new Options();
    options.addOption(createOption("n", "namespace", true, "namespace (optional, leave empty to correct all flowlets)", false));
    options.addOption(createOption("a", "app", true, "app (optional, leave empty to correct all apps)", false));
    options.addOption(createOption("f", "flow", true, "flow (optional, leave empty to correct all flows)", false));
    options.addOption(createOption("p", "producer-flowlet", true, "producer flowlet (optional, leave empty to correct entire flow)", false));
    options.addOption(createOption("c", "consumer-flowlet", true, "consumer flowlet (optional, leave empty to correct entire flow)", false));
    options.addOption(createOption("q", "queue", true, "flowlet queue (optional, defaults to \"queue\")", false));
    CommandLineParser parser = new BasicParser();
    try {
        return parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        String argsFormat = "[--namespace <namespace> " + "[--app <app> " + "[--flow <flow> " + "[[--producer-flowlet <flowlet> " + "--consumer-flowlet <flowlet> " + "[--queue <queue>]]]]]]";
        formatter.printHelp(argsFormat, options);
        System.exit(0);
        return null;
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) BasicParser(org.apache.commons.cli.BasicParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 87 with ParseException

use of org.apache.commons.cli.ParseException in project com.revolsys.open by revolsys.

the class ProcessorPipelineTool method processArguments.

@SuppressWarnings("unchecked")
public boolean processArguments(final String[] args) {
    try {
        final CommandLineParser parser = new PosixParser();
        this.commandLine = parser.parse(this.options, args);
        final List<String> arguments = this.commandLine.getArgList();
        final Option[] options = this.commandLine.getOptions();
        for (final Option option : options) {
            final String shortOpt = option.getOpt();
            if (shortOpt != null && shortOpt.equals("D")) {
                final String argument = arguments.remove(0);
                final String[] values = argument.split("=");
                System.setProperty(values[0], values[1]);
            }
        }
        if (this.commandLine.hasOption(SOURCE_DIRECTORY_OPTION)) {
            this.sourceDirectory = new File(this.commandLine.getOptionValue(SOURCE_DIRECTORY_OPTION));
            if (!this.sourceDirectory.isDirectory()) {
                System.err.println("Source directory '" + this.sourceDirectory.getAbsolutePath() + "' does not exist or is not a directory");
                return false;
            }
        }
        if (this.commandLine.hasOption(SOURCE_FILE_EXTENSION_OPTION)) {
            this.sourceFileExtension = this.commandLine.getOptionValue(SOURCE_FILE_EXTENSION_OPTION);
        }
        if (this.commandLine.hasOption(OUTPUT_DIRECTORY_OPTION)) {
            this.targetDirectory = new File(this.commandLine.getOptionValue(OUTPUT_DIRECTORY_OPTION));
            if (!this.targetDirectory.isDirectory()) {
                System.err.println("Target directory '" + this.targetDirectory.getAbsolutePath() + "' does not exist or is not a directory");
                return false;
            }
        }
        if (this.commandLine.hasOption(LOG_DIRECTORY_OPTION)) {
            this.logDirectory = new File(this.commandLine.getOptionValue(LOG_DIRECTORY_OPTION));
            if (!this.logDirectory.isDirectory()) {
                System.err.println("Log directory '" + this.logDirectory.getAbsolutePath() + "' does not exist or is not a directory");
                return false;
            }
        }
        this.scriptFile = new File(this.commandLine.getOptionValue(SCRIPT_OPTION));
        if (!this.scriptFile.exists()) {
            System.err.println("The script '" + this.scriptFile + "' does not exist");
            return false;
        }
        this.excludePattern = this.commandLine.getOptionValue(EXCLUDE_PATTERN_OPTION);
        if (this.sourceDirectory != null) {
            if (this.targetDirectory == null) {
                System.err.println("A " + OUTPUT_DIRECTORY + " must be specified if " + SOURCE_DIRECTORY + " is specified");
                return false;
            }
            if (this.sourceFileExtension == null) {
                System.err.println("A " + SOURCE_FLE_EXTENSION + " must be specified if " + SOURCE_DIRECTORY + " is specified");
                return false;
            }
        } else {
            this.sourceFile = new File(arguments.get(0));
            if (!this.sourceFile.exists()) {
                System.err.println("The file '" + this.sourceFile + "' does not exist");
                return false;
            }
            this.targetFile = new File(arguments.get(1));
        // if (targetFile.isDirectory()) {
        // targetFile = new File(targetFile, sourceFile.getName());
        // }
        }
        return true;
    } catch (final MissingOptionException e) {
        System.err.println("Missing " + e.getMessage() + " argument");
        return false;
    } catch (final ParseException e) {
        System.err.println("Unable to process command line arguments: " + e.getMessage());
        return false;
    }
}
Also used : PosixParser(org.apache.commons.cli.PosixParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) MissingOptionException(org.apache.commons.cli.MissingOptionException)

Example 88 with ParseException

use of org.apache.commons.cli.ParseException in project ANNIS by korpling.

the class AnnisAdminRunner method doExport.

private void doExport(List<String> commandArgs) {
    Options options = new OptionBuilder().createOptions();
    try {
        CommandLineParser parser = new PosixParser();
        CommandLine cmdLine = parser.parse(options, commandArgs.toArray(new String[commandArgs.size()]));
        if (cmdLine.getArgs().length < 2) {
            throw new ParseException("Needs two arguments: corpus name and output folder");
        }
        queryDao.exportCorpus(cmdLine.getArgs()[0], new File(cmdLine.getArgs()[1]));
    } catch (ParseException ex) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("annis-admin.sh export CORPUS DIR ...", options);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 89 with ParseException

use of org.apache.commons.cli.ParseException in project ANNIS by korpling.

the class AnnisAdminRunner method doInit.

private void doInit(List<String> commandArgs) {
    Options options = new OptionBuilder().addParameter("h", "host", "database server host (defaults to localhost)").addLongParameter("port", "database server port").addRequiredParameter("d", "database", "name of the ANNIS database (REQUIRED)").addRequiredParameter("u", "user", "name of the ANNIS user (REQUIRED)").addRequiredParameter("p", "password", "password of the ANNIS suer (REQUIRED)").addParameter("D", "defaultdb", "name of the PostgreSQL default database (defaults to \"postgres\")").addParameter("U", "superuser", "name of a PostgreSQL super user (defaults to \"postgres\")").addParameter("P", "superpassword", "password of a PostgreSQL super user").addParameter("m", "migratecorpora", "Try to import the already existing corpora into the database. " + "You can set the root directory for corpus sources as an argument.").addToggle("s", "ssl", false, "if given use SSL for connecting to the database").addLongParameter("schema", "The PostgreSQL schema to use (defaults to \"public\"). " + "Only lowercase characters and digits are allowed in the schema name.").createOptions();
    CommandLineParser parser = new PosixParser();
    CommandLine cmdLine = null;
    try {
        cmdLine = parser.parse(options, commandArgs.toArray(new String[commandArgs.size()]));
        // check for required flags
        if (!cmdLine.hasOption("user") || !cmdLine.hasOption("database") || !cmdLine.hasOption("password")) {
            throw new ParseException("required option is missing");
        }
        String host = cmdLine.getOptionValue("host", "localhost");
        String port = cmdLine.getOptionValue("port", "5432");
        String database = cmdLine.getOptionValue("database");
        String user = cmdLine.getOptionValue("user");
        String password = cmdLine.getOptionValue("password");
        String defaultDatabase = cmdLine.getOptionValue("defaultdb", "postgres");
        String superUser = cmdLine.getOptionValue("superuser", "postgres");
        String superPassword = cmdLine.getOptionValue("superpassword");
        boolean useSSL = cmdLine.hasOption("ssl");
        String pgSchema = cmdLine.getOptionValue("schema", "public").toLowerCase().replaceAll("[^a-z0-9]", "_");
        ;
        boolean migrateCorpora = cmdLine.hasOption("migratecorpora");
        List<Map<String, Object>> existingCorpora = new LinkedList<>();
        if (migrateCorpora) {
            // get corpus list
            try {
                existingCorpora = corpusAdministration.listCorpusStats();
            } catch (Exception ex) {
                log.warn("Could not get existing corpus list for migration, migrating " + "the corpora will be disabled.", ex);
                migrateCorpora = false;
            }
        }
        corpusAdministration.initializeDatabase(host, port, database, user, password, defaultDatabase, superUser, superPassword, useSSL, pgSchema);
        if (migrateCorpora && existingCorpora.size() > 0) {
            doMigration(cmdLine.getOptionValue("migratecorpora"), existingCorpora);
        }
    } catch (ParseException e) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("annis-admin.sh init", options);
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) LinkedList(java.util.LinkedList) AnnisRunnerException(annis.AnnisRunnerException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) UsageException(annis.UsageException) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 90 with ParseException

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

the class ArgParser method parseCommandLineArguments.

boolean parseCommandLineArguments(String[] args) {
    final CommandLineParser parser = new GnuParser();
    final Options options = setupOptions();
    try {
        commandLine = parser.parse(options, args);
    } catch (final ParseException pe) {
        System.err.print("Error: " + pe.getMessage());
        System.err.println();
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("DiagBundleCollectorDriver", "A tool that collects a diagnostic bundle of information from Oozie", options, "", true);
        return false;
    }
    return true;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

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