Example #1
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),
            });