Mapps arguments to parameters
Наследование: ICommandMapper
Пример #1
0
        private Tuple <CommandResult, List <object>, CommandInfo> ValidateDo(string commandline)
        {
            // Prepare
            var result = new CommandResult();

            // Parse input
            List <KeyValuePair <string, object> > args = _parser.Parse(commandline).ToList();

            if (!args.Any())
            {
                // Parsing error
                result.State = ResultState.ParsingError;
                return(new Tuple <CommandResult, List <object>, CommandInfo>(result, null, null));
            }

            // System commands
            if (args.First().Key == "ncommandsystem")
            {
                // Show help / info: overview of available functions
                if (args.Skip(1).FirstOrDefault().Key?.ToLower() == "help")
                {
                    result.State       = ResultState.ShowHelpOverview;
                    result.CommandInfo = new CommandInfo()
                    {
                        CommandName = "ncommandsystem.help"
                    };
                    result.Result = _lookup.Commands;
                    return(new Tuple <CommandResult, List <object>, CommandInfo>(result, null, null));
                }
                if (args.Skip(1).FirstOrDefault().Key?.ToLower() == "exit" || args.Skip(1).FirstOrDefault().Key?.ToLower() == "quit" || args.Skip(1).FirstOrDefault().Key?.ToLower() == "cancel")
                {
                    result.State = ResultState.Canceled;
                    return(new Tuple <CommandResult, List <object>, CommandInfo>(result, null, null));
                }
            }

            // Command by name
            var command = _lookup.GetCommand(args.First().Key);

            result.CommandInfo = command;
            if (command == null)
            {
                result.State = ResultState.UnknownCommand;
                return(new Tuple <CommandResult, List <object>, CommandInfo>(result, null, null));
            }

            // Find missing arguments
            var mapper = new CommandMapper();
            var mapped = mapper.Map(args.Skip(1), command.Arguments);

            if (mapped.State == ResultState.MissingArguments)
            {
                result.State                   = ResultState.MissingArguments;
                result.MissingArguments        = mapped.MissingArguments;
                result.MissingDefaultArguments = mapped.MissingDefaultArguments;
                return(new Tuple <CommandResult, List <object>, CommandInfo>(result, null, null));
            }

            // Result
            result.State = ResultState.Success;
            return(new Tuple <CommandResult, List <object>, CommandInfo>(result, mapped.ResultArguments, command));
        }
Пример #2
0
        private Tuple<CommandResult, List<object>, CommandInfo> ValidateDo(string commandline)
        {
            // Prepare
            var result = new CommandResult();

            // Parse input
            List<KeyValuePair<string, object>> args = _parser.Parse(commandline).ToList();
            if (!args.Any())
            {
                // Parsing error
                result.State = ResultState.ParsingError;
                return new Tuple<CommandResult, List<object>, CommandInfo>(result, null, null);
            }

            // System commands
            if (args.First().Key == "ncommandsystem")
            {
                // Show help / info: overview of available functions
                if (args.Skip(1).FirstOrDefault().Key?.ToLower() == "help")
                {
                    result.State = ResultState.ShowHelpOverview;
                    result.CommandInfo = new CommandInfo() { CommandName = "ncommandsystem.help" };
                    result.Result = _lookup.Commands;
                    return new Tuple<CommandResult, List<object>, CommandInfo>(result, null, null);
                }
                if (args.Skip(1).FirstOrDefault().Key?.ToLower() == "exit" || args.Skip(1).FirstOrDefault().Key?.ToLower() == "quit" || args.Skip(1).FirstOrDefault().Key?.ToLower() == "cancel")
                {
                    result.State = ResultState.Canceled;
                    return new Tuple<CommandResult, List<object>, CommandInfo>(result, null, null);
                }
            }

            // Command by name
            var command = _lookup.GetCommand(args.First().Key);
            result.CommandInfo = command;
            if (command == null)
            {
                result.State = ResultState.UnknownCommand;
                return new Tuple<CommandResult, List<object>, CommandInfo>(result, null, null);
            }

            // Find missing arguments
            var mapper = new CommandMapper();
            var mapped = mapper.Map(args.Skip(1), command.Arguments);
            if (mapped.State == ResultState.MissingArguments)
            {
                result.State = ResultState.MissingArguments;
                result.MissingArguments = mapped.MissingArguments;
                result.MissingDefaultArguments = mapped.MissingDefaultArguments;
                return new Tuple<CommandResult, List<object>, CommandInfo>(result, null, null);
            }

            // Result
            result.State = ResultState.Success;
            return new Tuple<CommandResult, List<object>, CommandInfo>(result, mapped.ResultArguments, command);
        }