public static IEnumerable <OptionSource> Get <TOptions>() where TOptions : new() { var defaultOptions = new TOptions(); return(typeof(TOptions).GetProperties() .Select(property => OptionSource.Typed("<DEFAULT>", property.Name, property.GetValue(defaultOptions, null)))); }
public static IEnumerable <OptionSource> FromFile(string fileName, string sectionName = null) { var options = new List <OptionSource>(); if (!File.Exists(fileName)) { throw new FileNotFoundException(fileName); } var yamlStream = new YamlStream(); var reader = new StringReader(File.ReadAllText(fileName)); try { yamlStream.Load(reader); } catch (Exception ex) { throw new OptionException(String.Format("An invalid configuration file has been specified. {0}{1}", Environment.NewLine, ex.Message), "config"); } var yamlNode = (YamlMappingNode)yamlStream.Documents[0].RootNode; if (!String.IsNullOrEmpty(sectionName)) { Func <KeyValuePair <YamlNode, YamlNode>, bool> predicate = x => x.Key.ToString() == sectionName && x.Value.GetType() == typeof(YamlMappingNode); var nodeExists = yamlNode.Children.Any(predicate); if (nodeExists) { yamlNode = (YamlMappingNode)yamlNode.Children.First(predicate).Value; } } foreach (var yamlElement in yamlNode.Children) { var yamlScalarNode = yamlElement.Value as YamlScalarNode; var yamlSequenceNode = yamlElement.Value as YamlSequenceNode; if (yamlSequenceNode != null) { var values = yamlSequenceNode.Children.Select(x => ((YamlScalarNode)x).Value); try { //TODO GFY DO WE PREFER STRINGS OR TYPES HERE? options.Add(OptionSource.String("Config File", yamlElement.Key.ToString(), values.ToArray())); } catch (InvalidCastException) { var message = String.Format("Please ensure that {0} is a valid YAML array.{1}", yamlElement.Key, Environment.NewLine); throw new OptionException(message, yamlElement.Key.ToString()); } } else if (yamlScalarNode != null) { options.Add(OptionSource.String("Config File", yamlElement.Key.ToString(), yamlElement.Value.ToString())); } } return(options); }
public static IEnumerable <OptionSource> Parse <TOptions>(Func <string, string> nameTranslator) where TOptions : class { return (from property in typeof(TOptions).GetProperties() let environmentVariableName = nameTranslator(property.Name) let environmentVariableValue = Environment.GetEnvironmentVariable(environmentVariableName.ToUpper()) where !String.IsNullOrEmpty(environmentVariableValue) select OptionSource.String("Environment Variable", property.Name, environmentVariableValue)); }
public static OptionSource ParseReferenceEnvironmentVariable(OptionSource source) { if (!source.IsReference) { return(source); } source.Value = Environment.GetEnvironmentVariable(_regex.Match(source.Value.ToString()).Groups[1].Value); return(source); }
public static IEnumerable <OptionSource> Parse <T>(string[] args) { var ret = new List <OptionSource>(); if (args == null || args.Length == 0) { return(ret); } foreach (var argument in ParseArgs <T>(args)) { ret.Add(OptionSource.String("Command Line", argument.Item1, argument.Item2)); } return(ret); }