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;
}
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;
}
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();
}
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();
}
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);
}
}
Aggregations