private ParserResult ParseOptions(IEnumerable <string> arguments, Type targetType) { var optionProperties = targetType.GetOptionProperties(); if (optionProperties == null || !optionProperties.Any()) { throw new InvalidOperationException( $"The specified type {targetType.Name} does not have any properties marked with {nameof(OptionAttribute)}."); } var tokens = Tokenizer.Tokenize(arguments); if (tokens == null || !tokens.Any()) { PrintHelpForTypes(new[] { targetType }); return(new NotParsedResult()); } var pairs = TokenPartitioner.Partition(tokens); try { var newObj = Activator.CreateInstance(targetType); OptionMapper.Map(optionProperties, pairs, newObj); return(new ParsedResult(newObj)); } catch (InvalidOperationException) { PrintHelpForTypes(new[] { targetType }); } return(new NotParsedResult()); }
public void ArePropertiesMappedCorrectly() { var optionProperties = typeof(TestClassValid).GetOptionProperties(); var tokens = Tokenizer.Tokenize(TestClassValidArgs); var keyValuePairs = TokenPartitioner.Partition(tokens); var obj = new TestClassValid(); OptionMapper.Map(optionProperties, keyValuePairs, obj); Assert.AreEqual("value1", obj.Option1); Assert.AreEqual("value2", obj.Option2); }
public void MapThrowsNullExceptionOnObject() { OptionMapper.Map(new List <OptionProperty>(), new List <KeyValuePair <string, string> >(), null); }
public void MapThrowsNullExceptionOnOptionProperties() { OptionMapper.Map(null, new List <KeyValuePair <string, string> >(), new object()); }