Exemplo n.º 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>());
        }
Exemplo n.º 2
0
        public void it_should_return_the_options_from_the_config_file()
        {
            var result = Yaml.FromFile(Path.Combine("YamlTests", "valid_config.yaml"));

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("Name", result.First().Name);
            Assert.AreEqual(false, result.First().IsTyped);
            Assert.AreEqual("foo", result.First().Value);
        }
Exemplo n.º 3
0
        public void it_should_return_the_options_from_the_config_file()
        {
            var result = Yaml.FromFile(Path.Combine("YamlTests", "config_with_section_and_dictionary.yaml"), "Section");

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("Roles", result.First().Name);
            Assert.AreEqual(true, result.First().IsTyped);
            var dictionary = result.First().Value as Dictionary <string, string>;

            Assert.AreEqual(new Dictionary <string, string> {
                { "accounting", "$admins" },
                { "it-experts", "$experts" }
            },
                            dictionary);
        }
Exemplo n.º 4
0
        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());
        }
Exemplo n.º 5
0
 public static TOptions Parse <TOptions>(string configFile, string sectionName = "") where TOptions : class, new()
 {
     return(Yaml.FromFile(configFile, sectionName).ApplyTo <TOptions>());
 }