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 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 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);
        }
示例#8
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);
        }
        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"));
        }
        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);
        }