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 void ParseProperties(IEnumerable <ArgumentMap> argumentsMaps, bool enablePositionalArgs, ParseResult.CommandParse commandParse, IEnumerable <ArgumentRaw> argumentsRaw)
        {
            var parseds = this.argumentParser.Parse(argumentsRaw, enablePositionalArgs, argumentsMaps);

            commandParse.AddProperties(parseds.Where(f => f.ParsingStates.HasFlag(ArgumentParsedState.Valid)));

            // Don't considere invalid args in this situation:
            // -> ArgumentMappingType.HasNoInput && ArgumentMappingState.ArgumentIsNotRequired
            // "ArgumentMappingType.HasNoInput": Means that args don't have input.
            // "ArgumentMappingState.ArgumentIsNotRequired": Means that args is optional.
            // in this situation the args is not consider invalid.
            commandParse.AddPropertiesInvalid(parseds.Where(f => f.ParsingStates.HasFlag(ArgumentParsedState.IsInvalid) && !f.ParsingStates.HasFlag(ArgumentParsedState.ArgumentIsNotRequired)));
        }
Exemplo n.º 4
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);
        }