/// <summary>
        /// Attempts to get the setting with the specified name.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="state"></param>
        /// <param name="setting"></param>
        /// <returns></returns>
        public bool TryGetSetting(string name, PrefixState state, out T setting)
        {
            bool InternalTryGetSetting(string deprefixedName, out T returned)
            {
                var valid = NameMap.TryGetValue(deprefixedName, out var guid);

                returned = valid ? SettingMap[guid] : default;
                return(valid);
            }

            foreach (var prefix in Prefixes)
            {
                var deprefixed = ArgumentMappingUtils.Deprefix(prefix, name, state);
                if (deprefixed != null && InternalTryGetSetting(deprefixed, out setting))
                {
                    return(true);
                }
            }
            setting = default;
            return(false);
        }
Пример #2
0
        /// <summary>
        /// Runs a method from the passed in arguments.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="args"></param>
        /// <param name="prefixes">The default prefixes are </param>
        /// <returns></returns>
        public virtual Task RunFromArguments(IServiceProvider services, string[] args, IEnumerable <string> prefixes = default)
        {
            prefixes ??= SettingParser.DefaultPrefixes;
            var parsedArgs = new ParseArgs(args, new[] { '"' }, new[] { '"' }).CreateArgMap((string s, out string result) =>
            {
                foreach (var prefix in prefixes)
                {
                    if (s.StartsWith(prefix))
                    {
                        result = ArgumentMappingUtils.Deprefix(prefix, s, PrefixState.Required);
                        return(true);
                    }
                }
                result = null;
                return(false);
            });

            var dictionary = parsedArgs.ToDictionary(x => x.Setting, x => x.Args, StringComparer.OrdinalIgnoreCase);

            return((dictionary.TryGetValue("method", out var val) ? val : null) switch
            {
                "RedditDirectories" => RedditDirectories(services),
                _ => Default(services),
            });