Пример #1
0
        private void ParseSettings()
        {
            foreach (Setting setting in _settings.Values)
            {
                if (!setting.IsProcessed)
                {
                    throw new SettingNotProcessedException(setting);
                }

                if (setting.Value == null)
                {
                    ISettingParser parser = _parserResolver.Resolve(setting.Type);
                    try
                    {
                        setting.Value = parser.Parse(setting.Raw, setting.Type);
                    }
                    catch (Exception ex)
                    {
                        string formattedExceptionMessage = String.Format("Cannot parse settting with key:[{0}] and value:[{1}]. Please see inner expection for details.", setting.Key, setting.Value);
                        throw new ConfigurationException(formattedExceptionMessage, ex);
                    }
                    ;
                }
            }
        }
Пример #2
0
        private static void EmptyParsing(ISettingParser parser, TestClass source)
        {
            var expected = parser.GetNeededSettings(source).Count;

            parser.Parse(source, "");
            Assert.AreEqual(expected, parser.GetNeededSettings(source).Count);
        }
Пример #3
0
 private static void FlagParsing(ISettingParser parser, TestClass source)
 {
     Assert.AreEqual(false, source.FlagValue);
     parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.FlagValue)}");
     Assert.AreEqual(true, source.FlagValue);
     parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.FlagValue)} false");
     Assert.AreEqual(false, source.FlagValue);
 }
        private ISettingParser GetElementParser(Type settingType, Type genericTypePart)
        {
            ISettingParser elementParser = _resolver.Resolve(genericTypePart);

            if (elementParser == null)
            {
                throw new ArgumentException("Cannot find a ISettingParser to parse the element of generic list " + settingType.FullName);
            }
            return(elementParser);
        }
Пример #5
0
 /// <summary>
 /// Returns information either about the settings in general, or the specified setting.
 /// </summary>
 /// <param name="parser"></param>
 /// <param name="name">The setting to target. Can be null if wanting to list out every setting.</param>
 /// <returns>Help information about either the setting or all settings.</returns>
 public static IResult GetHelp(ISettingParser parser, string name)
 {
     if (string.IsNullOrWhiteSpace(name))
     {
         var values = parser.GetSettings().Select(x => x.Names.Count < 2 ? x.MainName : $"{x.MainName} ({string.Join(", ", x.Names.Skip(1))})");
         return(new HelpResult($"All Settings:{Environment.NewLine}\t{string.Join($"{Environment.NewLine}\t", values)}"));
     }
     return(parser.TryGetSetting(name, PrefixState.NotPrefixed, out var setting)
                         ? new HelpResult(setting.Information)
                         : Result.FromError($"'{name}' is not a valid setting."));
 }
Пример #6
0
        private static void BasicParsing(ISettingParser parser, TestClass source)
        {
            parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.StringValue)} StringValueTest");
            Assert.AreEqual("StringValueTest", source.StringValue);
            parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.IntValue)} 1");
            Assert.AreEqual(1, source.IntValue);
            parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.BoolValue)} true");
            Assert.AreEqual(true, source.BoolValue);

            parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.UlongValue)} 18446744073709551615");
            Assert.AreEqual(ulong.MaxValue, source.UlongValue);
            parser.Parse(source, $"{parser.MainPrefix}{nameof(TestClass.DateTimeValue)} 05/06/2018");
            Assert.AreEqual(new DateTime(2018, 5, 6), source.DateTimeValue);
        }
        public object Parse(string raw, Type settingType)
        {
            Type[] typeGenericArgs = settingType.GetGenericArguments();
            IList  list            = Activator.CreateInstance(typeof(List <>).MakeGenericType(typeGenericArgs)) as IList;

            if (!raw.IsNullOrEmpty())
            {
                string    wrappedRaw = String.Format("<root>{0}</root>", raw);
                XDocument doc        = XDocument.Parse(wrappedRaw);
                var       items      = from item in doc.Root.Descendants()
                                       select item.Value;

                ISettingParser elementParser = GetElementParser(settingType, typeGenericArgs[0]);
                foreach (string item in items)
                {
                    list.Add(elementParser.Parse(item, typeGenericArgs[0]));
                }
            }

            return(CreateReadOnlyCollection(typeGenericArgs, list));
        }
Пример #8
0
        private static void ComplicatedParsing(ISettingParser parser, TestClass source)
        {
            var testStr = "Space \"Double deep quote\" Test";
            var args    = $"{parser.MainPrefix}{nameof(TestClass.StringValue)} \"{testStr}\" " +
                          $"{parser.MainPrefix}{nameof(TestClass.FlagValue2)} " +
                          $"{parser.MainPrefix}{nameof(TestClass.BoolValue)} true " +
                          $"{parser.MainPrefix}{nameof(TestClass.StringValue2)} \"{testStr}\" " +
                          $"{parser.MainPrefix}{nameof(TestClass.UlongValue)} asdf " +
                          $"{parser.MainPrefix}help {nameof(TestClass.FlagValue2)} " +
                          $"{parser.MainPrefix}{nameof(TestClass.DateTimeValue)} \"1/1/2016 12:00:00 PM\" " +
                          $"extra";
            var result = parser.Parse(source, args);

            Assert.AreEqual(5, result.Successes.Count);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(1, result.UnusedParts.Count);
            Assert.AreEqual(1, result.Help.Count);
            Assert.AreEqual(testStr, source.StringValue);
            Assert.AreEqual(testStr, source.StringValue2);
            Assert.AreEqual(true, source.FlagValue2);
            Assert.AreEqual(true, source.BoolValue);
            Assert.AreEqual(default, source.UlongValue);
Пример #9
0
 public StaticSettingHelpCommand(ISettingParser parent) : base(parent)
 {
 }
Пример #10
0
 /// <summary>
 /// Creates an instance of <see cref="HelpCommand"/>.
 /// </summary>
 /// <param name="parent"></param>
 public HelpCommand(ISettingParser parent) : base(new[] { "help", "h" })
 {
     Parent     = parent;
     IsOptional = true;
     HasBeenSet = true;
 }
 public void Register(ISettingParser parser)
 {
     ConfigGuard.ArgumentNotNull(parser, "parser", "Cannot add a null parser");
     _parsers.Add(parser);
 }
Пример #12
0
 public void Register(ISettingParser parser)
 {
     ConfigGuard.ArgumentNotNull(parser, "parser", "Cannot add a null parser");
     _parsers.Add(parser);
 }