Search in sources :

Example 1 with OptionHandlerFilter

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;
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) ConfigManagerApplicationConfig(org.apache.hyracks.control.common.application.ConfigManagerApplicationConfig) HashMap(java.util.HashMap) IConfigurator(org.apache.hyracks.api.config.IConfigurator) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) IOption(org.apache.hyracks.api.config.IOption) HashSet(java.util.HashSet) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) CmdLineParser(org.kohsuke.args4j.CmdLineParser) OptionHandlerFilter(org.kohsuke.args4j.OptionHandlerFilter) Section(org.apache.hyracks.api.config.Section) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IApplicationConfig(org.apache.hyracks.api.config.IApplicationConfig) Set(java.util.Set) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) Logger(java.util.logging.Logger) Ini(org.ini4j.Ini) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) CmdLineException(org.kohsuke.args4j.CmdLineException) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) CompositeMap(org.apache.commons.collections4.map.CompositeMap) Profile(org.ini4j.Profile) IConfigManager(org.apache.hyracks.api.config.IConfigManager) Comparator(java.util.Comparator) Collections(java.util.Collections) SortedMap(java.util.SortedMap) CmdLineParser(org.kohsuke.args4j.CmdLineParser) ArrayList(java.util.ArrayList) IOption(org.apache.hyracks.api.config.IOption) Section(org.apache.hyracks.api.config.Section) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) CompositeMap(org.apache.commons.collections4.map.CompositeMap) SortedMap(java.util.SortedMap) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1