use of org.apache.commons.cli.UnrecognizedOptionException in project nutch by apache.
the class MimeTypeIndexingFilter method main.
/**
* Main method for invoking this tool
* @param args run with no arguments to print help
* @throws IOException if there is a fatal I/O error processing the input args
* @throws IndexingException if there is a fatal error whils indexing
*/
public static void main(String[] args) throws IOException, IndexingException {
Option helpOpt = new Option("h", "help", false, "show this help message");
@SuppressWarnings("static-access") Option rulesOpt = OptionBuilder.withArgName("file").hasArg().withDescription("Rules file to be used in the tests relative to the conf directory").isRequired().create("rules");
Options options = new Options();
options.addOption(helpOpt).addOption(rulesOpt);
CommandLineParser parser = new GnuParser();
HelpFormatter formatter = new HelpFormatter();
String rulesFile;
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption("help") || !line.hasOption("rules")) {
formatter.printHelp("org.apache.nutch.indexer.filter.MimeTypeIndexingFilter", options, true);
return;
}
rulesFile = line.getOptionValue("rules");
} catch (UnrecognizedOptionException e) {
formatter.printHelp("org.apache.nutch.indexer.filter.MimeTypeIndexingFilter", options, true);
return;
} catch (Exception e) {
LOG.error(StringUtils.stringifyException(e));
e.printStackTrace();
return;
}
MimeTypeIndexingFilter filter = new MimeTypeIndexingFilter();
Configuration conf = NutchConfiguration.create();
conf.set(MimeTypeIndexingFilter.MIMEFILTER_REGEX_FILE, rulesFile);
filter.setConf(conf);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null && !line.isEmpty()) {
Metadata metadata = new Metadata();
metadata.set(Response.CONTENT_TYPE, line);
ParseImpl parse = new ParseImpl("text", new ParseData(new ParseStatus(), "title", new Outlink[0], metadata));
NutchDocument doc = filter.filter(new NutchDocument(), parse, new Text("http://www.example.com/"), new CrawlDatum(), new Inlinks());
if (doc != null) {
System.out.print("+ ");
System.out.println(line);
} else {
System.out.print("- ");
System.out.println(line);
}
}
}
use of org.apache.commons.cli.UnrecognizedOptionException in project onebusaway-gtfs-modules by OneBusAway.
the class GtfsMergerMain method run.
/**
***************************************************************************
* {@link Runnable} Interface
***************************************************************************
*/
public void run(String[] args) throws IOException {
if (needsHelp(args)) {
printHelp();
System.exit(0);
}
try {
CommandLine cli = _parser.parse(_options, args, true);
runApplication(cli, args);
} catch (MissingOptionException ex) {
System.err.println("Missing argument: " + ex.getMessage());
printHelp();
} catch (MissingArgumentException ex) {
System.err.println("Missing argument: " + ex.getMessage());
printHelp();
} catch (UnrecognizedOptionException ex) {
System.err.println("Unknown argument: " + ex.getMessage());
printHelp();
} catch (AlreadySelectedException ex) {
System.err.println("Argument already selected: " + ex.getMessage());
printHelp();
} catch (ParseException ex) {
System.err.println(ex.getMessage());
printHelp();
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of org.apache.commons.cli.UnrecognizedOptionException in project zm-mailbox by Zimbra.
the class ItemDataFile method main.
public static void main(String[] args) {
String cset = null;
Options opts = new Options();
CommandLineParser parser = new GnuParser();
opts.addOption("a", "assemble", false, "assemble backup");
opts.addOption("c", "charset", true, "path charset");
opts.addOption("e", "extract", false, "extract backup");
opts.addOption("h", "help", false, "help");
opts.addOption("l", "list", false, "list backup");
opts.addOption("n", "nometa", false, "ignore metadata");
opts.addOption("p", "path", true, "extracted backup path");
opts.addOption("t", "types", true, "item types");
ZimbraLog.toolSetupLog4j("ERROR", null);
try {
CommandLine cl = parser.parse(opts, args);
String path = ".";
String file = null;
boolean meta = true;
Set<MailItem.Type> types = null;
if (cl.hasOption('c')) {
cset = cl.getOptionValue('c');
}
if (cl.hasOption('n')) {
meta = false;
}
if (cl.hasOption('p')) {
path = cl.getOptionValue('p');
}
if (cl.hasOption('t')) {
try {
types = MailItem.Type.setOf(cl.getOptionValue('t'));
} catch (IllegalArgumentException e) {
throw MailServiceException.INVALID_TYPE(e.getMessage());
}
}
if (cl.hasOption('h') || cl.getArgs().length != 1) {
usage(opts);
}
file = cl.getArgs()[0];
if (cl.hasOption('a')) {
create(path, types, cset, new FileOutputStream(file));
} else if (cl.hasOption('e')) {
extract(new FileInputStream(file), meta, types, cset, path);
} else if (cl.hasOption('l')) {
list(file.equals("-") ? System.in : new FileInputStream(file), types, cset, System.out);
} else {
usage(opts);
}
} catch (Exception e) {
if (e instanceof UnrecognizedOptionException)
usage(opts);
else
e.printStackTrace(System.out);
System.exit(1);
}
}
use of org.apache.commons.cli.UnrecognizedOptionException in project commons by terran4j.
the class CommandLineService method execute.
private void execute(String groupName, String commandName, CommandDefine command, String[] args, PrintStream out) throws BusinessException {
Options options = new Options();
List<CommandOptionDefine> optionConfigs = command.getOptions();
if (optionConfigs != null && optionConfigs.size() > 0) {
for (CommandOptionDefine optionConfig : optionConfigs) {
String key = optionConfig.getKey();
String name = optionConfig.getName();
String desc = optionConfig.getDesc();
OptionType type = optionConfig.getType();
if (type == OptionType.BOOLEAN) {
options.addOption(key, name, false, desc);
} else if (type == OptionType.PROPERTIES) {
Option option = //
Option.builder(key).argName("property=value").numberOfArgs(5).valueSeparator('=').desc(//
desc).build();
options.addOption(option);
} else {
Builder builder = Option.builder(key).hasArg();
if (!StringUtils.isEmpty(name)) {
builder = builder.longOpt(name);
}
Option option = builder.desc(desc).build();
options.addOption(option);
}
}
}
CommandLine commandLine = null;
try {
commandLine = parser.parse(options, args);
} catch (UnrecognizedOptionException e) {
String optionKey = e.getOption();
out.println("无效的命令行选项: " + optionKey);
out.println(getHelp(groupName, commandName));
return;
} catch (ParseException e) {
out.println("解析命令出错:" + e.getMessage());
out.println(getHelp(groupName, commandName));
return;
}
CommandExecutor executor = command.getExecutor();
CommandInterpreter ci = new CommandInterpreterImpl(out, commandLine, command);
if (optionConfigs != null && optionConfigs.size() > 0) {
for (CommandOptionDefine optionConfig : optionConfigs) {
if (optionConfig.isRequired() && optionConfig.getType() != OptionType.BOOLEAN) {
String key = optionConfig.getKey();
String name = optionConfig.getName();
String value = ci.getOption(key);
if (value == null) {
throw //
new CommandException(CommandErrorCode.OPTION_KEY_EMPTY.getName()).put("group", //
groupName).put("commandName", //
commandName).put("optionKey", //
key).put("optionName", //
name).setMessage("命令 [${group} ${commandName}] 需要 ${optionName} 选项," + "请在命令后面附上 -${optionKey} [${optionName}]");
}
}
}
}
executor.execute(ci);
}
use of org.apache.commons.cli.UnrecognizedOptionException in project Manga by herrlock.
the class IgnoreUnknownParser method parse.
@Override
public CommandLine parse(final Options options, final String[] arguments, final Properties properties, final boolean stopAtNonOption) throws ParseException {
List<String> argumentList = new ArrayList<>(Arrays.asList(arguments));
CommandLine parse = null;
while (parse == null && !argumentList.isEmpty()) {
/* parse == null and argumentList not empty */
try {
String[] argumentListArray = argumentList.toArray(new String[argumentList.size()]);
parse = super.parse(options, argumentListArray, properties, false);
} catch (UnrecognizedOptionException ex) {
argumentList.remove(ex.getOption());
}
}
/* parse != null or argumentList is empty */
if (parse == null) {
/* argumentList is empty */
parse = super.parse(options, new String[0], properties, false);
}
return parse;
}
Aggregations