Search in sources :

Example 56 with ParseException

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

the class ShellPluginConfigurationCommand method getPluginClass.

public static <T> Class<? extends T> getPluginClass(final String tableName, final Shell shellState, final Class<T> clazz, final Property pluginProp) {
    Iterator<Entry<String, String>> props;
    try {
        props = shellState.getConnector().tableOperations().getProperties(tableName).iterator();
    } catch (AccumuloException | TableNotFoundException e) {
        return null;
    }
    while (props.hasNext()) {
        final Entry<String, String> ent = props.next();
        if (ent.getKey().equals(pluginProp.toString())) {
            Class<? extends T> pluginClazz;
            String[] args = new String[2];
            try {
                Options o = new Options();
                o.addOption(OptUtil.tableOpt());
                args[0] = "-t";
                args[1] = tableName;
                CommandLine cl = new BasicParser().parse(o, args);
                pluginClazz = shellState.getClassLoader(cl, shellState).loadClass(ent.getValue()).asSubclass(clazz);
            } catch (ClassNotFoundException e) {
                LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Class not found {}", e.getMessage());
                return null;
            } catch (ParseException e) {
                LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Error parsing table: {} {}", Arrays.toString(args), e.getMessage());
                return null;
            } catch (TableNotFoundException e) {
                LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Table not found: {} {}", tableName, e.getMessage());
                return null;
            } catch (Exception e) {
                LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Error: {}", e.getMessage());
                return null;
            }
            return pluginClazz;
        }
    }
    return null;
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) Options(org.apache.commons.cli.Options) AccumuloException(org.apache.accumulo.core.client.AccumuloException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) ParseException(org.apache.commons.cli.ParseException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) BasicParser(org.apache.commons.cli.BasicParser) Entry(java.util.Map.Entry) CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException)

Example 57 with ParseException

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

the class DMLDebuggerInterface method getDebuggerCommand.

/**
 * Read, process and return command from debugger CLI
 * @return CommandLine Current debug command (enter by user)
 * @throws DMLDebuggerException if DMLDebuggerException occurs
 */
public CommandLine getDebuggerCommand() throws DMLDebuggerException {
    CommandLine cmd = null;
    String[] args = null;
    // Display input prompt
    writeToStandardOutput("(SystemMLdb) ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
        // read command line argument(s)
        // To add 'up/down'-feature, use jline library
        String line = br.readLine();
        if (line != null && !line.isEmpty()) {
            args = line.split(" ");
            if (args[0].startsWith("-")) {
                // So as to avoid parsing '-i' command
                writelnToStandardError("Error reading command line arguments. Try \"help\".");
                return cmd;
            }
            args[0] = "-" + args[0];
        }
    } catch (IOException ae) {
        writelnToStandardError("Error reading command line arguments. Try \"help\".");
        return cmd;
    }
    CommandLineParser CLIparser = new GnuParser();
    try {
        // parse current command
        cmd = CLIparser.parse(getOptions(), args);
    } catch (ParseException pe) {
        System.err.println("Undefined command (or command arguments). Try \"help\".");
    }
    return cmd;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) GnuParser(org.apache.commons.cli.GnuParser) IOException(java.io.IOException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 58 with ParseException

use of org.apache.commons.cli.ParseException in project core by jcryptool.

the class HelpCommand method execute.

public void execute(CommandLine commandLine) {
    StringBuilder result = new StringBuilder();
    try {
        if (commandLine.hasOption(EXAMPLES_SHORT_OPT)) {
            if (commandLine.hasOption(COMMANDLIST_SHORT_OPT)) {
                String mask = Messages.HelpCommand_chooseOnlyOneOptionHint;
                throw new ParseException(String.format(mask, EXAMPLES_SHORT_OPT, COMMANDLIST_SHORT_OPT));
            }
            if (commandLine.getArgs().length == 1) {
                String commandName = commandLine.getArgs()[0];
                Command command = getCommands().get(commandName);
                List<Example> examples = getExamples(commandName, command);
                if (examples.size() != 0) {
                    result.append(getExampleString(examples, commandName));
                } else {
                    throw new ParseException(Messages.HelpCommand_noExampleSupport);
                }
            } else {
                if (commandLine.getArgs().length > 1)
                    throw new ParseException(Messages.HelpCommand_tooManyArgs);
                if (commandLine.getArgs().length < 1)
                    throw new ParseException(Messages.HelpCommand_tooFewArgs);
            }
        } else if (commandLine.hasOption(COMMANDLIST_SHORT_OPT)) {
            if (commandLine.getArgs().length == 0) {
                for (Command command : CommandFactory.loadUniqueExtensions()) {
                    printCommand(result, command);
                }
            } else {
                throw new ParseException(Messages.HelpCommand_tooManyArgs);
            }
        } else if (commandLine.getArgs().length == 1) {
            String commandName = commandLine.getArgs()[0];
            Command command = getCommands().get(commandName);
            result.append(getShortHelpFor(commandName, command, reverseCommandline(this.getCommandName(), commandLine)));
        } else if (commandLine.getArgs().length == 0) {
            result.append(getGeneralHelptext());
        } else {
            throw new ParseException(Messages.HelpCommand_tooManyArgsSyntaxhelpRef);
        }
    } catch (ParseException e) {
        result.append(e.getMessage());
    }
    this.result = result.toString();
}
Also used : AbstractCommand(org.jcryptool.commands.core.api.AbstractCommand) Example(org.jcryptool.commands.core.ExtendedHelpCommand.Example) ParseException(org.apache.commons.cli.ParseException)

Example 59 with ParseException

use of org.apache.commons.cli.ParseException in project core by jcryptool.

the class CommandsUiStartup method earlyStartup.

public void earlyStartup() {
    final IOConsole ioConsole = new IOConsole(Messages.CommandsUiStartup_consolename, null);
    ioConsole.setConsoleWidth(0);
    ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { ioConsole });
    CommandsUIPlugin.getDefault().setIoConsole(ioConsole);
    final CommandEvaluator evaluator = new CommandEvaluator();
    final IOConsolePromptShell monitor = new IOConsolePromptShell(ioConsole) {

        @Override
        protected void sendPrompt(final IOConsoleOutputStream o) throws IOException {
            Display.getDefault().syncExec(new Runnable() {

                public void run() {
                    Color prevColor = o.getColor();
                    int prevStyle = o.getFontStyle();
                    o.setColor(Display.getDefault().getSystemColor(SWT.COLOR_RED));
                    try {
                        o.write(PROMPT);
                    } catch (IOException e) {
                        LogUtil.logError(CommandsUIPlugin.PLUGIN_ID, e);
                    }
                    o.setColor(prevColor);
                    o.setFontStyle(prevStyle);
                }
            });
        }

        @Override
        protected void initializeConsole(IOConsoleOutputStream o) {
            super.initializeConsole(o);
            try {
                o.write(Messages.CommandsUiStartup_welcome + Messages.CommandsUiStartup_welcome_tip);
            } catch (IOException e) {
                LogUtil.logError(CommandsUIPlugin.PLUGIN_ID, e);
            }
        }
    };
    Observer monitorObserver = new Observer() {

        public void update(Observable o, Object arg) {
            String line = monitor.getLine();
            String result = Messages.CommandsUiStartup_eval_error;
            try {
                result = evaluator.evaluate(line).getResult();
            } catch (ParseException e) {
                LogUtil.logError(CommandsUIPlugin.PLUGIN_ID, e);
            }
            IOConsoleOutputStream outStream = ioConsole.newOutputStream();
            try {
                // $NON-NLS-1$
                outStream.write(result + "\n\n");
            } catch (IOException e) {
                LogUtil.logError(CommandsUIPlugin.PLUGIN_ID, e);
            }
            try {
                outStream.close();
            } catch (IOException e) {
                LogUtil.logError(CommandsUIPlugin.PLUGIN_ID, e);
            }
        }
    };
    monitor.addObserver(monitorObserver);
    monitor.startMonitoring();
}
Also used : CommandEvaluator(org.jcryptool.commands.core.evaluator.CommandEvaluator) IOConsoleOutputStream(org.eclipse.ui.console.IOConsoleOutputStream) IOConsolePromptShell(org.jcryptool.commands.ui.eclipseconsole.console.IOConsolePromptShell) Color(org.eclipse.swt.graphics.Color) Observer(java.util.Observer) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) IOConsole(org.eclipse.ui.console.IOConsole) Observable(java.util.Observable)

Example 60 with ParseException

use of org.apache.commons.cli.ParseException in project incubator-gobblin by apache.

the class CliOptions method parseArgs.

/**
 * Parse command line arguments and return a {@link java.util.Properties} object for the Gobblin job found.
 * @param caller Class of the calling main method. Used for error logs.
 * @param args Command line arguments.
 * @param conf Hadoop configuration object
 * @return Instance of {@link Properties} for the Gobblin job to run.
 * @throws IOException
 */
public static Properties parseArgs(Class<?> caller, String[] args, Configuration conf) throws IOException {
    try {
        // Parse command-line options
        if (conf != null) {
            args = new GenericOptionsParser(conf, args).getCommandLine().getArgs();
        }
        CommandLine cmd = new DefaultParser().parse(options(), args);
        if (cmd.hasOption(HELP_OPTION.getOpt())) {
            printUsage(caller);
            System.exit(0);
        }
        String jobConfigLocation = JOB_CONFIG_OPTION.getLongOpt();
        if (!cmd.hasOption(jobConfigLocation)) {
            printUsage(caller);
            System.exit(1);
        }
        // Load job configuration properties
        Properties jobConfig;
        if (conf == null) {
            jobConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(jobConfigLocation));
        } else {
            jobConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(jobConfigLocation), conf);
            JobConfigurationUtils.putConfigurationIntoProperties(conf, jobConfig);
        }
        return jobConfig;
    } catch (ParseException | ConfigurationException e) {
        throw new IOException(e);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException) Properties(java.util.Properties) GenericOptionsParser(org.apache.hadoop.util.GenericOptionsParser) 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