private static bool SetPropertyValue(Parsable parsable, Token token, IEnumerable<Token> tokens, ParsableArgumentAttribute parsableArgument, PropertyInfo prop) { if (token == null) { if (parsableArgument.DefaultValue != null) { prop.SetValue(parsable, parsableArgument.DefaultValue); return true; } return false; // couldnt find matching token, return false. } Token tokenValue = null; if(token.Type == TokenType.Field) { if (prop.PropertyType == typeof (bool)) { // check if we have been provided a "true" or "false" value. tokenValue = tokens.FirstOrDefault(x => !x.Taken && x.Index == token.Index + 1 && x.Type == TokenType.Value); var optionValue = true; if (tokenValue != null && !Boolean.TryParse(tokenValue.Value.ToString(), out optionValue)) { // tokenValue did not contain a valid bool so do not use its value, set it back to null so it will not be flagged as Taken. tokenValue = null; optionValue = true; } prop.SetValue(parsable, optionValue); // set property to true if flag was provided. } else { tokenValue = tokens.FirstOrDefault(x => !x.Taken && x.Index == token.Index + 1 && x.Type == TokenType.Value); if (tokenValue == null) throw new CliParseException(string.Format(CultureInfo.CurrentCulture, "Missing value for ParsableArgument {0}", token.Value)); PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(parsable)[prop.Name]; prop.SetValue(parsable, propertyDescriptor.Converter != null ? propertyDescriptor.Converter.ConvertFrom(tokenValue.Value) : tokenValue); } } if (token.Type == TokenType.Value) { PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(parsable)[prop.Name]; prop.SetValue(parsable, propertyDescriptor.Converter != null ? propertyDescriptor.Converter.ConvertFrom(token.Value) : token.Value); } token.Taken = true; if (tokenValue != null) tokenValue.Taken = true; return true; }
private static bool IsHelpToken(Token token, Parsable parsable) { var parsableClass = Helper.GetObjectAttribute(parsable, typeof(ParsableClassAttribute)) as ParsableClassAttribute ?? new ParsableClassAttribute(""); var defaultHelpArgs = parsableClass.ShowHelpParameters; return token.Type == TokenType.Field && defaultHelpArgs.Contains(token.Value.ToString()); }