public static TOptions Parse <TOptions>(string configFile, string sectionName = "") where TOptions : class, new() { var yamlParser = new YamlParser(); var optionsSource = yamlParser.Parse(configFile, sectionName); return(OptionsSourceParser.Parse <TOptions>(optionsSource)); }
public static TOptions Parse <TOptions>(string[] args, string environmentPrefix) where TOptions : class, IOptions, new() { var commandLineParser = new CommandLineParser(); var yamlParser = new YamlParser(); var environmentVariableProvider = new EnvironmentVariableProvider(); var defaultOptionsProvider = new DefaultOptionsProvider(); OptionSource[] defaultOptionSources = defaultOptionsProvider.Get <TOptions>(); OptionSource[] commandLineOptionSources; OptionSource[] configurationFileOptionSources = null; OptionSource[] environmentVariableOptionSources; if (args == null || args.Length == 0) { var optionSources = environmentVariableProvider.Parse <TOptions>(environmentPrefix); _effectiveOptions = OptionsSourceMerger.SequentialMerge( defaultOptionSources, optionSources); return(OptionsSourceParser.Parse <TOptions>(optionSources)); } try { commandLineOptionSources = commandLineParser.Parse <TOptions>(args); } catch (ArgException ex) { throw new OptionException(ex.Message, String.Empty); } if (commandLineOptionSources.Any(x => x.Name == "Config")) { var configOptionSource = commandLineOptionSources.First(x => x.Name == "Config"); if (!string.IsNullOrWhiteSpace(configOptionSource.Value.ToString())) { if (File.Exists(configOptionSource.Value.ToString())) { configurationFileOptionSources = yamlParser.Parse(configOptionSource.Value.ToString(), String.Empty); } else { Application.Exit(ExitCode.Error, string.Format("The specified configuration file {0} was not found.", configOptionSource.Value)); } } } environmentVariableOptionSources = environmentVariableProvider.Parse <TOptions>(environmentPrefix); _effectiveOptions = OptionsSourceMerger.SequentialMerge( defaultOptionSources, configurationFileOptionSources, environmentVariableOptionSources, commandLineOptionSources); OptionsSourceRulesRunner.RunRules(defaultOptionSources.Select(option => option.Name).ToArray(), _effectiveOptions); return(OptionsSourceParser.Parse <TOptions>(_effectiveOptions)); }