コード例 #1
0
        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"));
        }
コード例 #2
0
        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(6, cmdLineConfig.Data.Count);
            Assert.Equal("Value1", cmdLineConfig.Data["LongKey1"]);
            Assert.Equal("Value2", cmdLineConfig.Data["SuperLongKey2"]);
            Assert.Equal("Value3", cmdLineConfig.Data["Key3"]);
            Assert.Equal("Value4", cmdLineConfig.Data["Key4"]);
            Assert.Equal("Value5", cmdLineConfig.Data["Key5"]);
            Assert.Equal("Value6", cmdLineConfig.Data["SuchALongKey6"]);
        }
コード例 #3
0
        public void OverrideValueWhenKeyIsDuplicated()
        {
            var args = new string[]
                {
                    "/Key1=Value1",
                    "--Key1=Value2"
                };
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            cmdLineConfig.Load();

            Assert.Equal("Value2", cmdLineConfig.Get("Key1"));
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        public void OverrideValueWhenKeyIsDuplicated()
        {
            var args = new string[]
            {
                "/Key1=Value1",
                "--Key1=Value2"
            };
            var cmdLineConfig = new CommandLineConfigurationSource(args);

            cmdLineConfig.Load();

            Assert.Equal(1, cmdLineConfig.Data.Count());
            Assert.Equal("Value2", cmdLineConfig.Data["Key1"]);
        }
コード例 #7
0
        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
        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"));
        }
コード例 #9
0
        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(5, cmdLineConfig.Data.Count);
            Assert.Equal("Value1", cmdLineConfig.Data["Key1"]);
            Assert.Equal("Value2", cmdLineConfig.Data["Key2"]);
            Assert.Equal("Value3", cmdLineConfig.Data["Key3"]);
            Assert.Equal("Value4", cmdLineConfig.Data["Key4"]);
            Assert.Equal("Value5", cmdLineConfig.Data["Key5"]);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: Nyaoso/EntityFramework
        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;
        }