예제 #1
0
        private static IEnumerable <IEnumerable <OptionSource> > GetConfig <TOptions>(string[] args, string environmentPrefix, string defaultConfigLocation = null) where TOptions : class, IOptions, new()
        {
            var commandline = CommandLine.Parse <TOptions>(args).Normalize();
            var commanddict = commandline.ToDictionary(x => x.Name.ToLower());

            yield return(commandline);

            yield return(EnvironmentVariables.Parse <TOptions>(x => NameTranslators.PrefixEnvironmentVariable(x, environmentPrefix).ToUpper()));

            var configFile = commanddict.ContainsKey("config") ?
                             commanddict["config"].Value as string : null;

            if (configFile == null && File.Exists(defaultConfigLocation))
            {
                configFile = defaultConfigLocation;
                yield return(new OptionSource[] { OptionSource.String("Config File", "config", defaultConfigLocation) });
            }
            if (configFile != null)
            {
                if (!File.Exists(configFile))
                {
                    throw new OptionException(String.Format("The specified config file {0} could not be found", configFile), "config");
                }
                yield return(Yaml.FromFile(configFile));
            }
            yield return(TypeDefaultOptions.Get <TOptions>());
        }