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>()); }
public OptionsDumper(IEnumerable <Type> optionSections) { _optionSections = optionSections; _sortOrder = _optionSections.SelectMany(optionSection => optionSection.GetProperties() .Select(p => NameTranslators.CombineByPascalCase(p.Name, " ").ToUpper())) .ToList(); }
public static string DumpOptions() { if (_effectiveOptions == null) { return("No options have been parsed"); } var dumpOptionsBuilder = new StringBuilder(); var defaultOptionsHeading = "DEFAULT OPTIONS:"; var displayingModifiedOptions = true; dumpOptionsBuilder.AppendLine("MODIFIED OPTIONS:"); dumpOptionsBuilder.AppendLine(); if (_effectiveOptions.Count(x => !x.Source.ToLower().Contains("default")) == 0) { dumpOptionsBuilder.AppendLine("NONE"); dumpOptionsBuilder.AppendLine(); dumpOptionsBuilder.AppendLine(defaultOptionsHeading); dumpOptionsBuilder.AppendLine(); displayingModifiedOptions = false; } foreach (var option in _effectiveOptions.OrderBy(x => x.Source.ToLower().Contains("default") ? 1 : 0)) { if (option.Source.ToLower().Contains("default") && displayingModifiedOptions) { dumpOptionsBuilder.AppendLine(); dumpOptionsBuilder.AppendLine(defaultOptionsHeading); dumpOptionsBuilder.AppendLine(); displayingModifiedOptions = false; } var value = option.Value; if (option.Mask && displayingModifiedOptions) { value = "****"; } var optionName = NameTranslators.CombineByPascalCase(option.Name, " ").ToUpper(); var valueToDump = value == null ? String.Empty : value.ToString(); if (value is Array) { valueToDump = String.Empty; var collection = value as Array; if (collection.Length > 0) { valueToDump = "[ " + String.Join(", ", (IEnumerable <object>)value) + " ]"; } } dumpOptionsBuilder.AppendLine(String.Format("\t{0,-25} {1} ({2})", optionName + ":", String.IsNullOrEmpty(valueToDump) ? "<empty>" : valueToDump, option.Source)); } return(dumpOptionsBuilder.ToString()); }
public void should_return_the_environment_variables() { Environment.SetEnvironmentVariable("EVENTSTORE_NAME", "foo", EnvironmentVariableTarget.Process); var envVariable = Environment.GetEnvironmentVariable("EVENTSTORE_NAME"); var result = EnvironmentVariables.Parse <TestType>(x => NameTranslators.PrefixEnvironmentVariable(x, "EVENTSTORE_")); Assert.AreEqual(1, result.Count()); Assert.AreEqual("Name", result.First().Name); Assert.AreEqual(false, result.First().IsTyped); Assert.AreEqual("foo", result.First().Value.ToString()); Environment.SetEnvironmentVariable("EVENTSTORE_NAME", null, EnvironmentVariableTarget.Process); }
public void should_return_null_if_referenced_environment_variable_does_not_exist() { Environment.SetEnvironmentVariable("EVENTSTORE_NAME", "${env:TEST_REFERENCE_VAR}", EnvironmentVariableTarget.Process); var envVariable = Environment.GetEnvironmentVariable("EVENTSTORE_NAME"); var result = EnvironmentVariables.Parse <TestType>(x => NameTranslators.PrefixEnvironmentVariable(x, "EVENTSTORE_")); Assert.AreEqual(1, result.Count()); Assert.AreEqual("Name", result.First().Name); Assert.AreEqual(false, result.First().IsTyped); Assert.AreEqual(null, result.First().Value); Assert.AreEqual(true, result.First().IsReference); Environment.SetEnvironmentVariable("EVENTSTORE_NAME", null, EnvironmentVariableTarget.Process); }
private static IEnumerable <IEnumerable <OptionSource> > GetConfig(string[] args) { var commandline = CommandLine.Parse <SomeOptionType>(args).Normalize(); var commanddict = commandline.ToDictionary(x => x.Name); yield return(commandline); yield return (EnvironmentVariables.Parse <SomeOptionType>(x => NameTranslators.PrefixEnvironmentVariable(x, "EVENTSTORE"))); var configFile = commanddict.ContainsKey("config") ? commanddict["config"].Value as string : null; if (configFile != null && File.Exists(configFile)) { yield return (Yaml.FromFile(configFile)); } yield return (TypeDefaultOptions.Get <SomeOptionType>()); yield return (MyOwnCustomDefaultsAintThatNeat()); }
static string FormatSourceName(Type source) => $"({(source == typeof(Default) ? "<DEFAULT>" : NameTranslators.CombineByPascalCase(source.Name, " "))})" ;
public void Generate(string[] eventStoreBinaryPaths, string outputPath) { var documentation = String.Empty; foreach (var eventStoreBinaryPath in eventStoreBinaryPaths) { if (!Directory.Exists(eventStoreBinaryPath)) { Console.WriteLine("The path <{0}> does not exist", eventStoreBinaryPath); continue; } foreach (var assemblyFilePath in new DirectoryInfo(eventStoreBinaryPath).GetFiles().Where(x => x.Name.Contains("EventStore") && x.Name.EndsWith("exe"))) { var assembly = Assembly.LoadFrom(assemblyFilePath.FullName); var optionTypes = assembly.GetTypes().Where(x => typeof(IOptions).IsAssignableFrom(x)); foreach (var optionType in optionTypes) { var optionConstructor = optionType.GetConstructor(new Type[] { }); var options = optionConstructor.Invoke(null); var optionDocumentation = String.Format("###{0}{1}", options.GetType().Name, Environment.NewLine); var properties = options.GetType().GetProperties(); var currentGroup = String.Empty; foreach (var property in properties.OrderBy(x => x.Attr <ArgDescriptionAttribute>().Group)) { var parameterRow = String.Empty; if (currentGroup != property.Attr <ArgDescriptionAttribute>().Group) { currentGroup = property.Attr <ArgDescriptionAttribute>().Group; optionDocumentation += String.Format("{0}### {1}{0}{0}", Environment.NewLine, currentGroup); optionDocumentation += String.Format("| Parameter | Environment *(all prefixed with EVENTSTORE_)* | Yaml | Description |{0}", Environment.NewLine); optionDocumentation += String.Format("| --------- | --------------------------------------------- | ---- | ----------- |{0}", Environment.NewLine); } var parameterDefinition = new ArgumentUsageInfo(property); var parameterUsageFormat = "-{0}=VALUE<br/>"; var parameterUsage = String.Empty; foreach (var alias in parameterDefinition.Aliases) { parameterUsage += alias + "<br/>"; } parameterUsage += String.Format(parameterUsageFormat, parameterDefinition.Name); parameterRow += String.Format("|{0}", parameterUsage); parameterRow += String.Format("|{0}", NameTranslators.PrefixEnvironmentVariable(property.Name, "").ToUpper()); parameterRow += String.Format("|{0}", property.Name); var defaultValue = GetValues(property.GetValue(options, null)); var defaultString = defaultValue == "" ? "" : String.Format(" (Default: {0})", defaultValue); string[] possibleValues = null; if (property.PropertyType.IsEnum) { possibleValues = property.PropertyType.GetEnumNames(); } parameterRow += String.Format("|{0}{1} {2}|{3}", property.Attr <ArgDescriptionAttribute>().Description, defaultString, possibleValues != null ? "Possible Values:" + String.Join(",", possibleValues) : String.Empty, Environment.NewLine); optionDocumentation += parameterRow; } optionDocumentation += Environment.NewLine; documentation += optionDocumentation; } } if (!String.IsNullOrEmpty(documentation)) { Console.WriteLine("Writing generated document to {0}", outputPath); File.WriteAllText(outputPath, documentation); } else { Console.Error.WriteLine("The generated document is empty, please ensure that the event store binary paths that you supplied are correct."); } } }