use of org.kohsuke.args4j.OptionHandlerFilter in project asterixdb by apache.
the class ConfigManager method processCommandLine.
// use of System.err, System.exit()
@SuppressWarnings({ "squid:S106", "squid:S1147" })
private List<String> processCommandLine(Collection<Section> sections, OptionHandlerFilter usageFilter, BiConsumer<IOption, Object> setAction) throws CmdLineException {
final Args4jBean bean = new Args4jBean();
CmdLineParser cmdLineParser = new CmdLineParser(bean);
final List<String> appArgs = new ArrayList<>();
List<IOption> commandLineOptions = new ArrayList<>();
for (Map.Entry<Section, Map<String, IOption>> sectionMapEntry : sectionMap.entrySet()) {
if (!sections.contains(sectionMapEntry.getKey())) {
continue;
}
for (IOption option : sectionMapEntry.getValue().values()) {
if (option.section() != Section.VIRTUAL) {
commandLineOptions.add(option);
}
}
}
commandLineOptions.sort(Comparator.comparing(IOption::cmdline));
commandLineOptions.forEach(option -> cmdLineParser.addOption(new Args4jSetter(option, setAction, false), new Args4jOption(option, this, option.type().targetType())));
if (!argListeners.isEmpty()) {
cmdLineParser.addArgument(new Args4jSetter(o -> appArgs.add(String.valueOf(o)), true, String.class), new Args4jArgument());
}
LOGGER.fine("parsing cmdline: " + Arrays.toString(args));
if (args == null || args.length == 0) {
LOGGER.info("no command line args supplied");
return appArgs;
}
try {
cmdLineParser.parseArgument(args);
} catch (CmdLineException e) {
if (!bean.help) {
ConfigUtils.printUsage(e, usageFilter, System.err);
throw e;
} else {
LOGGER.log(Level.FINE, "Ignoring parse exception due to -help", e);
}
}
if (bean.help) {
ConfigUtils.printUsage(cmdLineParser, usageFilter, System.err);
System.exit(0);
} else if (bean.version) {
System.err.println(versionString);
System.exit(0);
}
return appArgs;
}
Aggregations