Exemplo n.º 1
0
        private ParseResult.Level GetLevelWithMethods(bool hasPropertyMap, IEnumerable <IGrouping <object, ActionParsed> > commandsGroups)
        {
            ParseResult.Level level = null;
            if (commandsGroups.Any())
            {
                level = new ParseResult.Level();

                foreach (var commandGroup in commandsGroups)
                {
                    var commandParse = new ParseResult.CommandParse();
                    commandParse.Command = (CommandBase)commandGroup.Key;
                    level.Add(commandParse);

                    bool isBestMethodButHasError;
                    var  bestMethod = this.GetBestMethod(commandGroup, out isBestMethodButHasError);
                    if (isBestMethodButHasError)
                    {
                        commandParse.AddMethodInvalid(bestMethod);
                    }
                    else
                    {
                        commandParse.AddMethod(bestMethod);
                    }
                }
            }

            return(level);
        }
Exemplo n.º 2
0
        private ParseResult.Level GetLevelWithPropertiesDefaultOrRequired(IEnumerable <ParseResult.Level> levels, IEnumerable <CommandMap> map)
        {
            ParseResult.Level level = null;

            var properties        = levels.SelectMany(l => l.Commands.SelectMany(c => c.Properties));
            var propertiesInvalid = levels.SelectMany(l => l.Commands.SelectMany(c => c.PropertiesInvalid));
            var listMapsRequiredOrDefaultNotMapped = new Dictionary <CommandBase, List <ArgumentMap> >();

            foreach (var cmd in map)
            {
                foreach (var propMap in cmd.Properties)
                {
                    if (propMap.HasDefaultValue || !propMap.IsOptional)
                    {
                        var notExistsInInvalid = propertiesInvalid.Empty(f => f.Map == propMap);
                        var notExistsInValid   = properties.Empty(f => f.Map == propMap);
                        if (notExistsInInvalid && notExistsInValid)
                        {
                            if (!listMapsRequiredOrDefaultNotMapped.ContainsKey(cmd.Command))
                            {
                                listMapsRequiredOrDefaultNotMapped[cmd.Command] = new List <ArgumentMap>();
                            }

                            listMapsRequiredOrDefaultNotMapped[cmd.Command].Add(propMap);
                        }
                    }
                }
            }

            if (listMapsRequiredOrDefaultNotMapped.Any())
            {
                level = new ParseResult.Level();

                foreach (var keyValue in listMapsRequiredOrDefaultNotMapped)
                {
                    var defaultsOrRequireds = argumentParser.CreateArgumentsDefaultValueOrRequired(keyValue.Value);
                    argumentParser.SetState(defaultsOrRequireds);

                    var commandParse = new ParseResult.CommandParse();
                    commandParse.Command = keyValue.Key;
                    commandParse.AddProperties(defaultsOrRequireds.Where(f => f.ParsingType == ArgumentParsedType.DefaultValue));
                    commandParse.AddPropertiesInvalid(defaultsOrRequireds.Where(f => f.ParsingType == ArgumentParsedType.HasNoInput));
                    level.Add(commandParse);
                }
            }

            return(level);
        }
Exemplo n.º 3
0
        private ActionParsed GetBestValidMethodInLevel(ParseResult.Level level)
        {
            // Select the best valid method that has the major arguments inputed
            var methodsValids = level.Commands.SelectMany(c => c.Methods);

            if (methodsValids.Empty())
            {
                return(null);
            }

            // never has error because here only valid methods
            bool isBestMethodButHasError;
            var  bestMethodInLevel = this.GetBestMethod(methodsValids, out isBestMethodButHasError);

            return(bestMethodInLevel);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Find the best valid property reference by raw.
        /// OBS: This rule does not exist for the methods by which the parameters
        /// of the best methods are already references.
        /// </summary>
        /// <param name="level">Level to search</param>
        /// <param name="raw">Raw reference</param>
        /// <returns>The best argument parsed</returns>
        private ArgumentParsed GetFirstValidArgumentParsedByRawInLevel(ParseResult.Level level, ArgumentRaw raw)
        {
            var list = new List <ArgumentParsed>();

            foreach (var cmd in level.Commands)
            {
                foreach (var prop in cmd.Properties)
                {
                    if (prop.AllRaw.Any() && prop.AllRaw.First() == raw)
                    {
                        list.Add(prop);
                    }
                }
            }

            return(list.OrderByDescending(a => a.AllRaw.Count()).FirstOrDefault());
        }
Exemplo n.º 5
0
        private bool IsEmptyLevel(ParseResult.Level level)
        {
            var isEmpty = true;

            foreach (var cmd in level.Commands)
            {
                if (cmd.Methods.Any() ||
                    cmd.MethodsInvalid.Any() ||
                    cmd.Properties.Any() ||
                    cmd.PropertiesInvalid.Any())
                {
                    isEmpty = false;
                    break;
                }
            }

            return(isEmpty);
        }
Exemplo n.º 6
0
        private void RemoveIncompatibleValidMethods(ParseResult.Level level, ActionParsed reference)
        {
            // Remove all valid methos there are incompatible with best valid method
            foreach (var cmd in level.Commands)
            {
                foreach (var validMethod in cmd.Methods.ToList())
                {
                    if (validMethod == reference)
                    {
                        continue;
                    }

                    var argsA = reference.Arguments;
                    var argsB = validMethod.Arguments;

                    if (!this.AllRawAreEqualsOfArgumentParsed(argsA, argsB))
                    {
                        cmd.RemoveMethod(validMethod);
                    }
                }
            }
        }
Exemplo n.º 7
0
        private ParseResult.Level GetLevelWithProperties(IEnumerable <CommandMap> commandsMap, IEnumerable <ArgumentRaw> arguments)
        {
            var level = new ParseResult.Level();

            // Step1: Create commands
            foreach (var commandMap in commandsMap)
            {
                var commandParse = new ParseResult.CommandParse();
                commandParse.Command = commandMap.Command;
                level.Add(commandParse);

                this.ParseProperties(commandMap.Properties, commandMap.Command.EnablePositionalArgs, commandParse, arguments);
            }

            // Step2: Create arguments references by raw
            var allArgsRawIsMapped = true;
            var argReferences      = new List <ArgumentParsed>();

            foreach (var raw in arguments)
            {
                if (argReferences.Empty(f => f.AllRaw.Any(r => r == raw)))
                {
                    var argParsed = this.GetFirstValidArgumentParsedByRawInLevel(level, raw);
                    if (argParsed != null)
                    {
                        if (argReferences.Empty(arg => arg == argParsed))
                        {
                            argReferences.Add(argParsed);
                        }
                    }
                    else
                    {
                        allArgsRawIsMapped = false;
                        break;
                    }
                }
            }

            // Step3: Clean commands if all valid raw was found
            if (allArgsRawIsMapped && argReferences.Count > 0)
            {
                foreach (var cmd in level.Commands)
                {
                    // Remove all invalid properties because all input was mapped
                    // and all are valid
                    var propsInvalidWithoutRequireds = cmd.PropertiesInvalid.Where(f => f.ParsingType != ArgumentParsedType.HasNoInput).ToList();
                    foreach (var invalid in propsInvalidWithoutRequireds)
                    {
                        cmd.RemovePropertyInvalid(invalid);
                    }

                    // Remove all valid properties that are imcompatible with
                    // reference
                    var props = cmd.Properties.Where(f => f.ParsingType != ArgumentParsedType.DefaultValue).ToList();
                    foreach (var arg in props)
                    {
                        var isCompatibleWithSomeRefs = argReferences.Any(argRef => this.AllRawAreEqualsInArgumentParsed(argRef, arg));
                        if (!isCompatibleWithSomeRefs)
                        {
                            cmd.RemoveProperty(arg);
                        }
                    }
                }
            }
            else
            {
                // Step4: Remove all valid properties that was mapped if not exists invalid
                //        properties. This is necessary to force "not found" error when
                //        some argument raw doesn't was found.
                foreach (var cmd in level.Commands)
                {
                    if (cmd.PropertiesInvalid.Empty())
                    {
                        var props = cmd.Properties.Where(f => f.ParsingType != ArgumentParsedType.DefaultValue).ToList();
                        foreach (var arg in props)
                        {
                            cmd.RemoveProperty(arg);
                        }
                    }
                }
            }

            return(level);
        }
Exemplo n.º 8
0
        private bool RemoveInvalidImplicitDefaultMethodsIfExists(string[] args, ParseResult.Level levelOfMethods, bool hasPropertyMap)
        {
            var defaultsInvalidMethodsWithImplicitInput = levelOfMethods.Commands
                                                          .SelectMany(f => f.MethodsInvalid)
                                                          .Where(f => this.IsMethodImplicit(f))
                                                          .ToList();

            // Step4: Exists action specify with name in input: my-action --p1 2
            if (defaultsInvalidMethodsWithImplicitInput.Count == 0)
            {
                return(false);
            }

            // Step5: Exists default action without name in input: --p1 2 (action omitted)
            var countRemove = 0;

            foreach (var defaultMethod in defaultsInvalidMethodsWithImplicitInput)
            {
                var countParameter      = defaultMethod.ActionMap.ArgumentsMaps.Count();
                var removeInvalidMethod = false;

                // 1) the default method dosen't have arguments
                // 2) and have one or more input arguments
                if (countParameter == 0 && args.Length > 0)
                {
                    removeInvalidMethod = true;
                }
                // 1) if default method has parameter, but exists error
                //    else ignore this default method and use your parameters
                //    to be used as input for the properties. This rule mantain
                //    the fluent match.
                // 2) Is obrigatory exists an less one property map to ignore
                //    this method.
                // scenary:
                // ----
                //  default(int a)
                //  string PropertyValue
                // ----
                //$ --property-value "invalid-value-for-default-method"
                // expected: PropertyValue = invalid-value-for-default-method
                else if (countParameter > 0 && hasPropertyMap)
                {
                    removeInvalidMethod = true;
                }

                if (removeInvalidMethod)
                {
                    foreach (var cmd in levelOfMethods.Commands)
                    {
                        foreach (var method in cmd.MethodsInvalid.ToList())
                        {
                            if (method == defaultMethod)
                            {
                                cmd.RemoveInvalidMethod(method);
                                countRemove++;
                            }
                        }
                    }
                }
            }

            var allWasRemoved = countRemove == defaultsInvalidMethodsWithImplicitInput.Count;

            return(allWasRemoved);
        }