public void LoadKeyValuePairsFromCommandLineArgumentsWithSwitchMappings()
        {
            var args = new string[]
                {
                    "-K1=Value1",
                    "--Key2=Value2",
                    "/Key3=Value3",
                    "--Key4", "Value4",
                    "/Key5", "Value5",
                    "/Key6=Value6"
                };
            var switchMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { "-K1", "LongKey1" },
                    { "--Key2", "SuperLongKey2" },
                    { "--Key6", "SuchALongKey6"}
                };
            var cmdLineConfig = new CommandLineConfigurationSource(args, switchMappings);

            cmdLineConfig.Load();

            Assert.Equal("Value1", cmdLineConfig.Get("LongKey1"));
            Assert.Equal("Value2", cmdLineConfig.Get("SuperLongKey2"));
            Assert.Equal("Value3", cmdLineConfig.Get("Key3"));
            Assert.Equal("Value4", cmdLineConfig.Get("Key4"));
            Assert.Equal("Value5", cmdLineConfig.Get("Key5"));
            Assert.Equal("Value6", cmdLineConfig.Get("SuchALongKey6"));
        }
Esempio n. 2
0
        public static void WithCustomConfigFile()
        {
            var args = new[] { "-c", "settings2.xml", "-n", "Richard" };
            var commandLineSource = new CommandLineConfigurationSource <Config>(args);
            // Since the XML config file is processed internally before the command
            // line source, the config filename must be generated out-of-band.
            Func <string> getConfigFileFromCommandLine =
                () => commandLineSource.Config.ConfigFile;

            var configManager = new ConfigurationManager <Config>(
                new XmlFileConfigurationSource <Config>(getConfigFileFromCommandLine)
            {
                PrimarySource = true      // Make sure command line source does not mark properties as changed
            }, commandLineSource);

            Console.WriteLine("Loading config file from {0}", getConfigFileFromCommandLine());

            Console.WriteLine(configManager.Out);

            Console.WriteLine("Since the XML file is marked as the primary source, " +
                              "the command line source is not counted when determining which " +
                              "properties have changed.");

            PrintPropertiesChanged(configManager);
        }
        public void LoadKeyValuePairsFromCommandLineArgumentsWithSwitchMappings()
        {
            var args = new string[]
            {
                "-K1=Value1",
                "--Key2=Value2",
                "/Key3=Value3",
                "--Key4", "Value4",
                "/Key5", "Value5",
                "/Key6=Value6"
            };
            var switchMappings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "-K1", "LongKey1" },
                { "--Key2", "SuperLongKey2" },
                { "--Key6", "SuchALongKey6" }
            };
            var cmdLineConfig = new CommandLineConfigurationSource(args, switchMappings);

            cmdLineConfig.Load();

            Assert.Equal("Value1", cmdLineConfig.Get("LongKey1"));
            Assert.Equal("Value2", cmdLineConfig.Get("SuperLongKey2"));
            Assert.Equal("Value3", cmdLineConfig.Get("Key3"));
            Assert.Equal("Value4", cmdLineConfig.Get("Key4"));
            Assert.Equal("Value5", cmdLineConfig.Get("Key5"));
            Assert.Equal("Value6", cmdLineConfig.Get("SuchALongKey6"));
        }
 public void Do()
 {
     var arguments = new[] {"/?", "--help", "-h", "/platform:x86"};
     var source = new CommandLineConfigurationSource( arguments );
     source.AddSwitch( "Default", "/?", "help", "h", "platform" );
     IConfigurationSection section = source.Sections["Default"];
     Console.WriteLine( source );
 }
        public static void AddAsConfiguraionTo(
            this CommandLineConfigurationSource argsContext,
            IConfigurationBuilder configBuilder)
        {
            var configArgs         = GetConfigArgs(argsContext.Args);
            var configKeyValuePair = configArgs.ToKeyValuePair(Program.CommandLineArgsConfigValueSeparator);

            configBuilder.AddInMemoryCollection(configKeyValuePair);
        }
Esempio n. 6
0
        public void Do()
        {
            var arguments = new[] { "/?", "--help", "-h", "/platform:x86" };
            var source    = new CommandLineConfigurationSource(arguments);

            source.AddSwitch("Default", "/?", "help", "h", "platform");
            IConfigurationSection section = source.Sections["Default"];

            Console.WriteLine(source);
        }
        public void OverrideValueWhenKeyIsDuplicated()
        {
            var args = new string[]
                {
                    "/Key1=Value1",
                    "--Key1=Value2"
                };
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            cmdLineConfig.Load();

            Assert.Equal("Value2", cmdLineConfig.Get("Key1"));
        }
        public void OverrideValueWhenKeyIsDuplicated()
        {
            var args = new string[]
            {
                "/Key1=Value1",
                "--Key1=Value2"
            };
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            cmdLineConfig.Load();

            Assert.Equal("Value2", cmdLineConfig.Get("Key1"));
        }
        public void ThrowExceptionWhenAnArgumentCannotBeRecognized()
        {
            var args = new string[]
            {
                "ArgWithoutPrefixAndEqualSign"
            };
            var expectedMsg = new FormatException(
                Resources.FormatError_UnrecognizedArgumentFormat("ArgWithoutPrefixAndEqualSign")).Message;
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            var exception = Assert.Throws <FormatException>(() => cmdLineConfig.Load());

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void ThrowExceptionWhenValueForAKeyIsMissing()
        {
            var args = new string[]
            {
                "--Key1", "Value1",
                "/Key2"     /* The value for Key2 is missing here */
            };
            var expectedMsg   = new FormatException(Resources.FormatError_ValueIsMissing("/Key2")).Message;
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            var exception = Assert.Throws <FormatException>(() => cmdLineConfig.Load());

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void ThrowExceptionWhenShortSwitchNotDefined()
        {
            var args = new string[]
            {
                "-Key1", "Value1",
            };
            var switchMappings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "-Key2", "LongKey2" }
            };
            var expectedMsg   = new FormatException(Resources.FormatError_ShortSwitchNotDefined("-Key1")).Message;
            var cmdLineConfig = new CommandLineConfigurationSource(args, switchMappings);

            var exception = Assert.Throws <FormatException>(() => cmdLineConfig.Load());

            Assert.Equal(expectedMsg, exception.Message);
        }
Esempio n. 12
0
        protected virtual IConfigurationSourceContainer CreateConfiguration(
            MigrationTool tool, CommandCode commandCode, string[] commandArgs)
        {
            var configuration = CreateConfiguration();

            CommandLineConfigurationSource commandLineConfigSource;
            string configFile;

            if (commandArgs != null &&
                commandArgs.Any())
            {
                commandLineConfigSource = new CommandLineConfigurationSource(commandArgs);
                commandLineConfigSource.Load();
                commandLineConfigSource.TryGet(MigrationTool.Constants.ConfigFileOption, out configFile);
            }
            else
            {
                commandLineConfigSource = null;
                configFile = null;
            }

            if (commandCode != CommandCode.CommitConfiguration)
            {
                if (!string.IsNullOrEmpty(configFile))
                {
                    configuration.AddIniFile(tool.ResolvePath(configFile));
                }
                else
                {
                    configFile = tool.ResolvePath(MigrationTool.Constants.DefaultConfigFile);
                    if (File.Exists(configFile))
                    {
                        configuration.AddIniFile(configFile);
                    }
                }
            }

            if (commandLineConfigSource != null)
            {
                configuration.Add(commandLineConfigSource);
            }

            return(configuration);
        }
Esempio n. 13
0
        public void IConfigurationProvider_CommandLine()
        {
            var args = new[] { "--name", "SinxHe" };
            var map  = new Dictionary <string, string>
            {
                ["--name"] = "Name"
            };
            var source = new CommandLineConfigurationSource
            {
                Args           = args,
                SwitchMappings = map
            };
            var provider = new CommandLineConfigurationProvider(args, map);

            provider.TryGet("Name", out var name);
            Assert.Null(name);
            provider.Load();
            provider.TryGet("Name", out name);
            Assert.Equal(name, "SinxHe");
        }
        public void LoadKeyValuePairsFromCommandLineArgumentsWithoutSwitchMappings()
        {
            var args = new string[]
                {
                    "Key1=Value1",
                    "--Key2=Value2",
                    "/Key3=Value3",
                    "--Key4", "Value4",
                    "/Key5", "Value5"
                };
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            cmdLineConfig.Load();

            Assert.Equal("Value1", cmdLineConfig.Get("Key1"));
            Assert.Equal("Value2", cmdLineConfig.Get("Key2"));
            Assert.Equal("Value3", cmdLineConfig.Get("Key3"));
            Assert.Equal("Value4", cmdLineConfig.Get("Key4"));
            Assert.Equal("Value5", cmdLineConfig.Get("Key5"));
        }
        public void LoadKeyValuePairsFromCommandLineArgumentsWithoutSwitchMappings()
        {
            var args = new string[]
            {
                "Key1=Value1",
                "--Key2=Value2",
                "/Key3=Value3",
                "--Key4", "Value4",
                "/Key5", "Value5"
            };
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            cmdLineConfig.Load();

            Assert.Equal("Value1", cmdLineConfig.Get("Key1"));
            Assert.Equal("Value2", cmdLineConfig.Get("Key2"));
            Assert.Equal("Value3", cmdLineConfig.Get("Key3"));
            Assert.Equal("Value4", cmdLineConfig.Get("Key4"));
            Assert.Equal("Value5", cmdLineConfig.Get("Key5"));
        }
Esempio n. 16
0
        public static void WithDefaultConfigFile()
        {
            var args = new string[] { };
            var commandLineSource = new CommandLineConfigurationSource <Config>(args);
            // Since the XML config file is processed internally before the command
            // line source, the config filename must be generated out-of-band.
            Func <string> getConfigFileFromCommandLine =
                () => commandLineSource.Config.ConfigFile;

            var configManager = new ConfigurationManager <Config>(
                new XmlFileConfigurationSource <Config>(getConfigFileFromCommandLine)
            {
                PrimarySource = true
            }, commandLineSource);

            Console.WriteLine("Loading config file from {0}", getConfigFileFromCommandLine());
            Console.WriteLine(configManager.Out);

            PrintPropertiesChanged(configManager);
        }
        public void ThrowExceptionWhenShortSwitchNotDefined()
        {
            var args = new string[]
                {
                    "-Key1", "Value1",
                };
            var switchMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { "-Key2", "LongKey2" }
                };
            var expectedMsg = new FormatException(Resources.FormatError_ShortSwitchNotDefined("-Key1")).Message;
            var cmdLineConfig = new CommandLineConfigurationSource(args, switchMappings);

            var exception = Assert.Throws<FormatException>(() => cmdLineConfig.Load());

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void ThrowExceptionWhenValueForAKeyIsMissing()
        {
            var args = new string[]
                {
                    "--Key1", "Value1",
                    "/Key2" /* The value for Key2 is missing here */
                };
            var expectedMsg = new FormatException(Resources.FormatError_ValueIsMissing("/Key2")).Message;
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            var exception = Assert.Throws<FormatException>(() => cmdLineConfig.Load());

            Assert.Equal(expectedMsg, exception.Message);
        }
        public void ThrowExceptionWhenAnArgumentCannotBeRecognized()
        {
            var args = new string[]
                {
                    "ArgWithoutPrefixAndEqualSign"
                };
            var expectedMsg = new FormatException(
                Resources.FormatError_UnrecognizedArgumentFormat("ArgWithoutPrefixAndEqualSign")).Message;
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            var exception = Assert.Throws<FormatException>(() => cmdLineConfig.Load());

            Assert.Equal(expectedMsg, exception.Message);
        }