Search in sources :

Example 66 with ParseException

use of org.apache.commons.cli.ParseException in project twister2 by DSC-SPIDAL.

the class PythonCheckstyle method main.

public static void main(String[] args) throws IOException {
    CommandLineParser parser = new DefaultParser();
    // create the Options
    Options options = new Options();
    options.addOption(Option.builder("f").required(true).hasArg().longOpt("extra_action_file").desc("bazel extra action protobuf file").build());
    options.addOption(Option.builder("p").required(true).hasArg().longOpt("pylint_file").desc("Executable pylint file to invoke").build());
    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);
        String extraActionFile = line.getOptionValue("f");
        String pylintFile = line.getOptionValue("p");
        Collection<String> sourceFiles = getSourceFiles(extraActionFile);
        if (sourceFiles.size() == 0) {
            LOG.info("No python files found by checkstyle");
            return;
        }
        LOG.info(sourceFiles.size() + " python files found by checkstyle");
        // Create and run the command
        List<String> commandBuilder = new ArrayList<>();
        commandBuilder.add(pylintFile);
        commandBuilder.add("--rcfile=" + PYLINT_RCFILE);
        commandBuilder.add("--reports=n");
        commandBuilder.add("--disable=I");
        commandBuilder.addAll(sourceFiles);
        runLinter(commandBuilder);
    } catch (ParseException exp) {
        LOG.severe(String.format("Invalid input to %s: %s", CLASSNAME, exp.getMessage()));
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java " + CLASSNAME, options);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 67 with ParseException

use of org.apache.commons.cli.ParseException in project herddb by diennea.

the class HerdDBCLI method main.

public static void main(String... args) throws IOException {
    try {
        DefaultParser parser = new DefaultParser();
        Options options = new Options();
        options.addOption("x", "url", true, "JDBC URL");
        options.addOption("u", "username", true, "JDBC Username");
        options.addOption("pwd", "password", true, "JDBC Password");
        options.addOption("q", "query", true, "Execute inline query");
        options.addOption("v", "verbose", false, "Verbose output");
        options.addOption("s", "schema", true, "Default tablespace (SQL schema)");
        options.addOption("f", "file", true, "SQL Script to execute (statement separated by 'GO' lines)");
        options.addOption("at", "autotransaction", false, "Execute scripts in autocommit=false mode and commit automatically");
        options.addOption("atbs", "autotransactionbatchsize", true, "Batch size for 'autotransaction' mode");
        options.addOption("g", "script", true, "Groovy Script to execute");
        options.addOption("i", "ignoreerrors", false, "Ignore SQL Errors during file execution");
        options.addOption("sc", "sqlconsole", false, "Execute SQL console in interactive mode");
        options.addOption("fmd", "mysql", false, "Intruct the parser that the script is coming from a MySQL Dump");
        options.addOption("rwst", "rewritestatements", false, "Rewrite all statements to use JDBC parameters");
        options.addOption("b", "backup", false, "Backup one or more tablespaces (selected with --schema)");
        options.addOption("r", "restore", false, "Restore tablespace");
        options.addOption("nl", "newleader", true, "Leader for new restored tablespace");
        options.addOption("ns", "newschema", true, "Name for new restored tablespace");
        options.addOption("tsm", "tablespacemapper", true, "Path to groovy script with a custom functin to map table names to tablespaces");
        options.addOption("dfs", "dumpfetchsize", true, "Fetch size for dump operations. Defaults to chunks of 100000 records");
        options.addOption("n", "nodeid", true, "Node id");
        options.addOption("t", "table", true, "Table name");
        options.addOption("p", "param", true, "Parameter name");
        options.addOption("val", "values", true, "Parameter values");
        options.addOption("lts", "list-tablespaces", false, "List available tablespaces");
        options.addOption("ln", "list-nodes", false, "List available nodes");
        options.addOption("sts", "show-tablespace", false, "Show full informations about a tablespace (needs -s option)");
        options.addOption("lt", "list-tables", false, "List tablespace tables (needs -s option)");
        options.addOption("st", "show-table", false, "Show full informations about a table (needs -s and -t options)");
        options.addOption("ar", "add-replica", false, "Add a replica to the tablespace (needs -s and -r options)");
        options.addOption("rr", "remove-replica", false, "Remove a replica from the tablespace (needs -s and -r options)");
        options.addOption("adt", "create-tablespace", false, "Create a tablespace (needs -ns and -nl options)");
        options.addOption("at", "alter-tablespace", false, "Alter a tablespace (needs -s, -param and --values options)");
        options.addOption("d", "describe", false, "Checks and describes a raw file");
        options.addOption("ft", "filetype", true, "Checks and describes a raw file (valid options are txlog, datapage, tablecheckpoint, indexcheckpoint, tablesmetadata");
        options.addOption("mdf", "metadatafile", true, "Tables metadata file, required for 'datapage' filetype");
        options.addOption("tsui", "tablespaceuuid", true, "Tablespace UUID, used for describing raw files");
        org.apache.commons.cli.CommandLine commandLine;
        try {
            commandLine = parser.parse(options, args);
        } catch (ParseException error) {
            println("Syntax error: " + error);
            failAndPrintHelp(options);
            return;
        }
        if (args.length == 0) {
            failAndPrintHelp(options);
            return;
        }
        String schema = commandLine.getOptionValue("schema", TableSpace.DEFAULT);
        String tablespaceuuid = commandLine.getOptionValue("tablespaceuuid", "");
        final boolean verbose = commandLine.hasOption("verbose");
        if (!verbose) {
            LogManager.getLogManager().reset();
        }
        String file = commandLine.getOptionValue("file", "");
        String tablesmetadatafile = commandLine.getOptionValue("metadatafile", "");
        String table = commandLine.getOptionValue("table", "");
        boolean describe = commandLine.hasOption("describe");
        String filetype = commandLine.getOptionValue("filetype", "");
        if (describe) {
            try {
                if (file.isEmpty()) {
                    throw new IllegalArgumentException("file option is required");
                }
                describeRawFile(tablespaceuuid, table, tablesmetadatafile, file, filetype);
            } catch (Exception error) {
                if (verbose) {
                    error.printStackTrace();
                } else {
                    println("error:" + error);
                }
                exitCode = 1;
            }
            return;
        }
        String url = commandLine.getOptionValue("url", "jdbc:herddb:server:localhost:7000");
        String username = commandLine.getOptionValue("username", ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT);
        String password = commandLine.getOptionValue("password", ClientConfiguration.PROPERTY_CLIENT_PASSWORD_DEFAULT);
        String query = commandLine.getOptionValue("query", "");
        boolean backup = commandLine.hasOption("backup");
        boolean restore = commandLine.hasOption("restore");
        String newschema = commandLine.getOptionValue("newschema", "");
        String leader = commandLine.getOptionValue("newleader", "");
        String script = commandLine.getOptionValue("script", "");
        String tablespacemapperfile = commandLine.getOptionValue("tablespacemapper", "");
        int dumpfetchsize = Integer.parseInt(commandLine.getOptionValue("dumpfetchsize", 100000 + ""));
        final boolean ignoreerrors = commandLine.hasOption("ignoreerrors");
        boolean sqlconsole = commandLine.hasOption("sqlconsole");
        final boolean frommysqldump = commandLine.hasOption("mysql");
        final boolean rewritestatements = commandLine.hasOption("rewritestatements") || !tablespacemapperfile.isEmpty() || frommysqldump;
        boolean autotransaction = commandLine.hasOption("autotransaction") || frommysqldump;
        int autotransactionbatchsize = Integer.parseInt(commandLine.getOptionValue("autotransactionbatchsize", 100000 + ""));
        if (!autotransaction) {
            autotransactionbatchsize = 0;
        }
        String nodeId = commandLine.getOptionValue("nodeid", "");
        String param = commandLine.getOptionValue("param", "");
        String values = commandLine.getOptionValue("values", "");
        boolean listTablespaces = commandLine.hasOption("list-tablespaces");
        boolean listNodes = commandLine.hasOption("list-nodes");
        boolean showTablespace = commandLine.hasOption("show-tablespace");
        boolean listTables = commandLine.hasOption("list-tables");
        boolean showTable = commandLine.hasOption("show-table");
        if (showTable) {
            if (table.equals("")) {
                println("Specify the table (-t <table>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }
        boolean createTablespace = commandLine.hasOption("create-tablespace");
        if (createTablespace) {
            if (newschema.equals("")) {
                println("Specify the tablespace name (--newschema <schema>)");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (leader.equals("")) {
                println("Specify the leader node (--newleader <nodeid>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }
        boolean alterTablespace = commandLine.hasOption("alter-tablespace");
        if (alterTablespace) {
            if (commandLine.getOptionValue("schema", null) == null) {
                println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s " + TableSpace.DEFAULT + "\" instead");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (param.equals("")) {
                println("Specify the parameter (--param <par>)");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (values.equals("")) {
                println("Specify values (--values <vals>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }
        boolean addReplica = commandLine.hasOption("add-replica");
        if (addReplica) {
            if (commandLine.getOptionValue("schema", null) == null) {
                println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s " + TableSpace.DEFAULT + "\" instead");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (nodeId.equals("")) {
                println("Specify the node (-n <nodeid>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }
        boolean removeReplica = commandLine.hasOption("remove-replica");
        if (removeReplica) {
            if (commandLine.getOptionValue("schema", null) == null) {
                println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s " + TableSpace.DEFAULT + "\" instead");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (nodeId.equals("")) {
                println("Specify the node (-n <nodeid>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }
        TableSpaceMapper tableSpaceMapper = buildTableSpaceMapper(tablespacemapperfile);
        try (HerdDBDataSource datasource = new HerdDBDataSource()) {
            datasource.setUrl(url);
            datasource.setUsername(username);
            datasource.setPassword(password);
            try (Connection connection = datasource.getConnection();
                Statement statement = connection.createStatement()) {
                connection.setSchema(schema);
                if (sqlconsole) {
                    runSqlConsole(connection, statement, PRETTY_PRINT);
                } else if (backup) {
                    performBackup(statement, schema, file, options, connection, dumpfetchsize);
                } else if (restore) {
                    performRestore(file, leader, newschema, options, statement, connection);
                } else if (!query.isEmpty()) {
                    executeStatement(verbose, ignoreerrors, false, false, query, statement, tableSpaceMapper, false, PRETTY_PRINT);
                } else if (!file.isEmpty()) {
                    executeSqlFile(autotransactionbatchsize, connection, file, verbose, ignoreerrors, frommysqldump, rewritestatements, statement, tableSpaceMapper, PRETTY_PRINT);
                } else if (!script.isEmpty()) {
                    executeScript(connection, datasource, statement, script);
                } else if (listTablespaces) {
                    printTableSpaces(verbose, ignoreerrors, statement, tableSpaceMapper);
                } else if (listNodes) {
                    printNodes(verbose, ignoreerrors, statement, tableSpaceMapper);
                } else if (showTablespace) {
                    printTableSpaceInfos(verbose, ignoreerrors, statement, tableSpaceMapper, schema);
                } else if (listTables) {
                    listTables(verbose, ignoreerrors, statement, tableSpaceMapper, schema);
                } else if (showTable) {
                    printTableInfos(verbose, ignoreerrors, statement, tableSpaceMapper, schema, table);
                } else if (addReplica) {
                    changeReplica(verbose, ignoreerrors, statement, tableSpaceMapper, schema, nodeId, ChangeReplicaAction.ADD);
                } else if (removeReplica) {
                    changeReplica(verbose, ignoreerrors, statement, tableSpaceMapper, schema, nodeId, ChangeReplicaAction.REMOVE);
                } else if (createTablespace) {
                    createTablespace(verbose, ignoreerrors, statement, tableSpaceMapper, newschema, leader);
                } else if (alterTablespace) {
                    alterTablespace(verbose, ignoreerrors, statement, tableSpaceMapper, schema, param, values);
                } else {
                    failAndPrintHelp(options);
                    return;
                }
            }
            exitCode = 0;
        } catch (Exception error) {
            if (verbose) {
                error.printStackTrace();
            } else {
                println("error:" + error);
            }
            exitCode = 1;
        }
    } finally {
        System.exit(exitCode);
    }
}
Also used : Options(org.apache.commons.cli.Options) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) HDBConnection(herddb.client.HDBConnection) HerdDBConnection(herddb.jdbc.HerdDBConnection) HerdDBDataSource(herddb.jdbc.HerdDBDataSource) ScriptException(javax.script.ScriptException) EndOfFileException(org.jline.reader.EndOfFileException) ParseException(org.apache.commons.cli.ParseException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UserInterruptException(org.jline.reader.UserInterruptException) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 68 with ParseException

use of org.apache.commons.cli.ParseException in project iobserve-analysis by research-iobserve.

the class PerOpteryxHeadless method start.

@Override
public Object start(final IApplicationContext context) throws Exception {
    String[] args = Platform.getCommandLineArgs();
    final CommandLineParser parser = new BasicParser();
    try {
        final CommandLine commandLine = parser.parse(PerOpteryxHeadless.createOptions(), args);
        final String workingDir = commandLine.getOptionValue(INPUT_WORKING_DIR_OPTION);
        LOG.info("Working dir: " + workingDir);
        this.launchPeropteryx(workingDir);
    } catch (final ParseException exp) {
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("peropteryx-headless", PerOpteryxHeadless.createOptions());
        return -1;
    }
    this.project.delete(true, new NullProgressMonitor());
    return IApplication.EXIT_OK;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) BasicParser(org.apache.commons.cli.BasicParser) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 69 with ParseException

use of org.apache.commons.cli.ParseException in project iobserve-analysis by research-iobserve.

the class PlanningMain method main.

public static void main(final String[] args) throws IOException, InitializationException, ModelHandlingErrorException {
    final CommandLineParser parser = new DefaultParser();
    final String workingDir;
    final String perOpteryxDir;
    final CommandLine commandLine;
    try {
        for (final String arg : args) {
            if (PlanningMain.LOGGER.isInfoEnabled()) {
                PlanningMain.LOGGER.info("arg: " + arg);
            }
        }
        commandLine = parser.parse(PlanningMain.createOptions(), args);
        workingDir = commandLine.getOptionValue(PlanningMain.INPUT_WORKING_DIR_OPTION);
        perOpteryxDir = commandLine.getOptionValue(PlanningMain.PEROPTERYX_DIR_OPTION);
        if (PlanningMain.LOGGER.isInfoEnabled()) {
            PlanningMain.LOGGER.info("Working dir: " + workingDir + ", PerOpteryx dir: " + perOpteryxDir);
        }
    } catch (final ParseException exp) {
        // LOG.error("CLI error: " + exp.getMessage());
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("planning", PlanningMain.createOptions());
        return;
    }
    final URI modelURI = URI.createFileURI(workingDir);
    if (PlanningMain.LOGGER.isInfoEnabled()) {
        PlanningMain.LOGGER.info("modelURI: " + modelURI);
    }
    final URI perOpteryxURI = URI.createFileURI(perOpteryxDir);
    if (PlanningMain.LOGGER.isInfoEnabled()) {
        PlanningMain.LOGGER.info("perOpteryxURI: " + perOpteryxURI);
        PlanningMain.LOGGER.info("lqnsURI: " + perOpteryxURI);
    }
    PalladioEclipseEnvironment.INSTANCE.setup();
    if (!commandLine.hasOption(PlanningMain.CREATE_RESOURCEENVIRONMENT_OPTION)) {
        if (PlanningMain.LOGGER.isInfoEnabled()) {
            PlanningMain.LOGGER.info("Executing optimization...");
        }
        final AdaptationData adaptationData = new AdaptationData();
        adaptationData.setRuntimeModelURI(modelURI);
        final PlanningData planningData = new PlanningData();
        planningData.setAdaptationData(adaptationData);
        planningData.setOriginalModelDir(modelURI);
        planningData.setPerOpteryxDir(perOpteryxURI);
        // Process model
        final ModelTransformer transformer = new ModelTransformer(planningData);
        transformer.transformModel();
        // Execute PerOpteryx
        final int result = 0;
        if (result == 0) {
            if (PlanningMain.LOGGER.isInfoEnabled()) {
                PlanningMain.LOGGER.info("Optimization was successful.");
            }
        } else {
            if (PlanningMain.LOGGER.isInfoEnabled()) {
                PlanningMain.LOGGER.info("Optimization failed.");
            }
        }
    } else {
        if (PlanningMain.LOGGER.isInfoEnabled()) {
            PlanningMain.LOGGER.info("Creating ResourceEnvironment...");
        }
        final PCMModelHandler modelHandler = new PCMModelHandler(new File(workingDir));
        ModelHelper.fillResourceEnvironmentFromCloudProfile(org.eclipse.emf.common.util.URI.createFileURI(workingDir), modelHandler);
        if (PlanningMain.LOGGER.isInfoEnabled()) {
            PlanningMain.LOGGER.info("ResourceEnvironment successfully created.");
        }
    }
}
Also used : PCMModelHandler(org.iobserve.model.PCMModelHandler) PlanningData(org.iobserve.planning.data.PlanningData) URI(org.eclipse.emf.common.util.URI) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) ModelTransformer(org.iobserve.planning.ModelTransformer) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) AdaptationData(org.iobserve.adaptation.data.AdaptationData) File(java.io.File) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 70 with ParseException

use of org.apache.commons.cli.ParseException in project cytoscape-impl by cytoscape.

the class Parser method parseCommandLine.

private void parseCommandLine(String[] args) {
    // try to parse the cmd line
    CommandLineParser parser = new PosixParser();
    CommandLine line = null;
    // first load the simple exit options
    try {
        line = parser.parse(options, args);
    } catch (ParseException e) {
        logger.error("Parsing command line failed: " + e.getMessage());
        printHelp();
        props.setProperty("tempHideWelcomeScreen", "true");
        shutdown.exit(1);
        return;
    }
    if (line.hasOption("h")) {
        printHelp();
        shutdown.exit(0);
        props.setProperty("tempHideWelcomeScreen", "true");
        return;
    }
    if (line.hasOption("v")) {
        logger.info("Cytoscape version: " + version.getVersion());
        System.out.println("Cytoscape version: " + version.getVersion());
        props.setProperty("tempHideWelcomeScreen", "true");
        shutdown.exit(0);
        return;
    }
    // always load any properties specified
    if (line.hasOption("P"))
        startupConfig.setProperties(line.getOptionValues("P"));
    // it the only argument is a session file, load it
    if (line.getOptions().length == 0 && line.getArgs().length == 1 && line.getArgs()[0].endsWith(".cys")) {
        startupConfig.setSession(line.getArgs()[0]);
        return;
    }
    // Either load the session ...
    if (line.hasOption("s")) {
        startupConfig.setSession(line.getOptionValue("s"));
    // ... or all the rest.
    } else {
        if (line.hasOption("N"))
            startupConfig.setNetworks(line.getOptionValues("N"));
        if (line.hasOption("V"))
            startupConfig.setVizMapProps(line.getOptionValues("V"));
    }
    // Do we have any command scripts requested?
    if (line.hasOption("S"))
        startupConfig.setCommandScript(line.getOptionValue("S"));
    // Do we want to change the port number for REST server?
    if (line.hasOption("R"))
        startupConfig.setRestPort(line.getOptionValue("R"));
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) 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