Exemple #1
0
        private static Token GetTokenForArgumentByName(ICollection<Token> tokens, ParsableArgumentAttribute parsableArgument)
        {
            var shortName = parsableArgument.ShortName.ToString(CultureInfo.InvariantCulture);
            var longName = parsableArgument.Name;

            var tokensArray = tokens as Token[] ?? tokens.ToArray();

            // Match by name. 
            // shortnames are case sensitive
            // long names are not
            var token =
                tokensArray.FirstOrDefault( x =>
                        !x.Taken && x.Type == TokenType.Field && (x.Value.ToString().Equals(shortName, StringComparison.Ordinal) 
                        || x.Value.ToString().Equals(longName, StringComparison.OrdinalIgnoreCase)));

            return token;
        }
Exemple #2
0
        private static Token GetTokenForArgumentByPosition(ICollection<Token> tokens, ParsableArgumentAttribute parsableArgument)
        {
            var impliedPosition = parsableArgument.ImpliedPosition;

            var tokensArray = tokens as Token[] ?? tokens.ToArray();
            Token token = null;

            // match by position - postives
            if (impliedPosition > 0)
            {
                token = tokensArray.FirstOrDefault(x =>
                    !x.Taken && x.Type == TokenType.Value && x.Index == impliedPosition);
            }
            else
            {
                // match by position - negatives
                token = tokensArray.FirstOrDefault(x =>
                    !x.Taken && x.Type == TokenType.Value && (x.Index - tokens.Count - 1) == impliedPosition);
            }


            return token;
        }
Exemple #3
0
        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;
        }