Options options = buildOptions();
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(options, args);
Configuration result = new MapConfiguration(new HashMap<String,Object>());
if(cmd.hasOption('B')) {
// check backends
Set<String> existing = Sets.newHashSet(Iterators.transform (backends.iterator(), new BackendIdentifierFunction()));
if(!existing.contains(cmd.getOptionValue('B'))) {
throw new ParseException("the backend " + cmd.getOptionValue('B') + " does not exist");
}
result.setProperty(LoaderOptions.BACKEND, cmd.getOptionValue('B'));
}
if(cmd.hasOption('b')) {
result.setProperty(LoaderOptions.BASE_URI, cmd.getOptionValue('b'));
}
if(cmd.hasOption('z')) {
result.setProperty(LoaderOptions.COMPRESSION, CompressorStreamFactory.GZIP);
}
if(cmd.hasOption('j')) {
result.setProperty(LoaderOptions.COMPRESSION, CompressorStreamFactory.BZIP2);
}
if(cmd.hasOption('c')) {
result.setProperty(LoaderOptions.CONTEXT, cmd.getOptionValue('c'));
}
if(cmd.hasOption('t')) {
RDFFormat fmt = getRDFFormat(cmd.getOptionValue('t'));
if(fmt == null) {
throw new ParseException("unrecognized MIME type: " + cmd.getOptionValue('t'));
}
result.setProperty(LoaderOptions.FORMAT, fmt.getDefaultMIMEType());
}
if(cmd.hasOption('f')) {
result.setProperty(LoaderOptions.FILES, Arrays.asList(cmd.getOptionValues('f')));
}
if(cmd.hasOption('d')) {
result.setProperty(LoaderOptions.DIRS, Arrays.asList(cmd.getOptionValues('d')));
}
if(cmd.hasOption('a')) {
result.setProperty(LoaderOptions.ARCHIVES, Arrays.asList(cmd.getOptionValues('a')));
}
if(cmd.hasOption('s')) {
result.setProperty(LoaderOptions.STATISTICS_ENABLED, true);
result.setProperty(LoaderOptions.STATISTICS_GRAPH, cmd.getOptionValue('s'));
}
if(cmd.hasOption('D')) {
for(Map.Entry e : cmd.getOptionProperties("D").entrySet()) {
result.setProperty(e.getKey().toString(), e.getValue());
}
}
for(LoaderBackend b : backends) {
for(Option option : b.getOptions()) {
if(cmd.hasOption(option.getOpt())) {
String key = String.format("backend.%s.%s", b.getIdentifier(), option.getLongOpt() != null ? option.getLongOpt() : option.getOpt());
if(option.hasArg()) {
if(option.hasArgs()) {
result.setProperty(key, Arrays.asList(cmd.getOptionValues(option.getOpt())));
} else {
result.setProperty(key, cmd.getOptionValue(option.getOpt()));
}
} else {
result.setProperty(key, true);
}
}
}
}