Example #1
0
        internal void ParseLine(string line)
        {
            ParsingResult result = new ParsingResult();

            foreach (var command in KnownCommands)
            {
                result = command.Parse(line);
                if (result.Type == ParsingResultType.Success || result.Type == ParsingResultType.SuccessReachedEnd ||
                    result.Type == ParsingResultType.ParsingFailure || result.Type == ParsingResultType.MissingParam)
                {
                    break;
                }
            }

            if (result.Type == ParsingResultType.WrongCommand)
            {
                string command       = line;
                var    parsingResult = ParsingHelpers.TryAnyCommandBody(line);
                if (parsingResult.WasSuccessful && parsingResult.Value.Length > 0)
                {
                    command = parsingResult.Value;
                }

                GConsole.WriteLine(GConsole.ColorifyText(1, String.Format(Resources.text.UnknownCommand, command)));
            }
        }
Example #2
0
        public AutoCompletionResult AutoComplete(string line)
        {
            var result = ParsingHelpers.AutoCompleteCommandBody(line, this);

            if (result.WasSuccessful == AutoCompletionResultType.FailureAlreadyComplete)
            {
                foreach (var command in GConsole.Instance.Commands)
                {
                    var resultSecond = ParsingHelpers.AutoCompleteCommandBody(line.Substring(result.RemainderStartPosition), command);
                    if (resultSecond.WasSuccessful == AutoCompletionResultType.SuccessOneOption)
                    {
                        var finalResult = new AutoCompletionResult();
                        finalResult.WasSuccessful = AutoCompletionResultType.SuccessOneOption;
                        finalResult.Results.Add(line.Substring(0, result.RemainderStartPosition - 1) + " " + resultSecond.Results[0]);
                        return(finalResult);
                    }
                }

                foreach (var keyword in GConsole.Instance.Keywords)
                {
                    var resultSecond = ParsingHelpers.AutoCompleteString(line.Substring(result.RemainderStartPosition - 1), keyword.Name);
                    if (resultSecond.WasSuccessful == AutoCompletionResultType.SuccessOneOption)
                    {
                        var finalResult = new AutoCompletionResult();
                        finalResult.WasSuccessful = AutoCompletionResultType.SuccessOneOption;
                        finalResult.Results.Add(line.Substring(0, result.RemainderStartPosition - 1) + " " + resultSecond.Results[0]);
                        return(finalResult);
                    }
                }
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// Parse a simple command with flags and no paramters.
        /// </summary>
        /// <param name="line">Input line.</param>
        /// <param name="command">Command to parse.</param>
        /// <param name="flags">Out Flags containted in the input line. See BoolFlag.</param>
        /// <returns>Result of the parsing.
        /// Possible result types are Success, WrongCommand and ParsingFailure.
        /// See ParsingResult.</returns>
        public static ParsingResult ParseSimpleCommandWithFlags(string line, ICommand command, out IEnumerable <BoolFlag> flags)
        {
            string remainder   = "";
            var    resultFlags = new List <BoolFlag>();

            flags = resultFlags;

            var result = ParseSimpleCommandBody(line, command, out remainder);

            // Flags are optional
            if (result.Type == ParsingResultType.SuccessReachedEnd)
            {
                result.Type = ParsingResultType.Success;
                return(result);
            }

            var flagsResult = ParsingHelpers.TryFlags(remainder);

            if (flagsResult.WasSuccessful == false || flagsResult.Remainder.AtEnd == false)
            {
                GConsole.WriteLine(Resources.text.FailureParsingFlags, remainder, command.Name());
                result.Type = ParsingResultType.ParsingFailure;
                return(result);
            }

            var parsedFlags = flagsResult.Value.ToList();

            foreach (var flag in command.GetFlags())
            {
                if (flag.FindInList(ref parsedFlags))
                {
                    resultFlags.Add(flag);
                }
            }

            flags = resultFlags;

            if (parsedFlags.Count > 0)
            {
                GConsole.WriteLine(Resources.text.UnknownFlags, String.Join(",", parsedFlags), command.Name());
                result.Type = ParsingResultType.ParsingFailure;
                return(result);
            }

            result.Type = ParsingResultType.Success;
            return(result);
        }
Example #4
0
        public ParsingResult Parse(string line)
        {
            string parameter = "";
            var    result    = ParsingHelpers.ParseSimpleCommandWithOneParameter(line, this, out parameter);

            if (result.Type == ParsingResultType.Success)
            {
                if (GConsole.Instance.Commands.FindMan(parameter) == false)
                {
                    if (GConsole.Instance.Keywords.FindMan(parameter) == false)
                    {
                        GConsole.WriteLine(GConsole.ColorifyText(1,
                                                                 String.Format(Resources.text.UnknownCommandOrKeywordForMan, parameter)));
                    }
                }
            }
            return(result);
        }
Example #5
0
        public ParsingResult Parse(string line)
        {
            var result = ParsingHelpers.ParseSimpleCommand(line, this);

            if (result.Type == ParsingResultType.Success)
            {
                GConsole.WriteLine(-1.0f, "{0} {1} {2}",
                                   GConsole.ColorifyText(1, "Currently available commands follow. You can use"),
                                   GConsole.ColorifyText(0, Resources.text.ManCommandName),
                                   GConsole.ColorifyText(1, "to learn more about them."));

                foreach (var command in GConsole.Instance.Commands)
                {
                    if (command.Available() == true)
                    {
                        GConsole.WriteLine(-1.0f, "\t{0}",
                                           GConsole.ColorifyText(0, command.Name()));
                    }
                }
            }

            return(result);
        }
Example #6
0
        /// <summary>
        /// Parse a command with parameters and flags.
        /// </summary>
        /// <param name="line">Input line.</param>
        /// <param name="command">Command to parse.</param>
        /// <param name="hasRequiredParams">Whether the command has at least on required parameters.</param>
        /// <returns>Result of the parsing.
        /// Possible result types are Success, WrongCommand, MissingParam and ParsingFailure.
        /// It splits all the parameters by space into ParsingResult.Parameters.
        /// See ParsingResult.</returns>
        public static ParsingResult ParseCommand(string line, ICommand command, bool hasRequiredParams)
        {
            string remainder = "";

            var result = ParseSimpleCommandBody(line, command, out remainder);

            if (result.Type == ParsingResultType.SuccessReachedEnd)
            {
                if (hasRequiredParams == true)
                {
                    result.Type = ParsingResultType.MissingParam;
                    GConsole.WriteLine(-1.0f, Resources.text.MissingParam, command.Name());
                    return(result);
                }
                else
                {
                    result.Type = ParsingResultType.Success;
                    return(result);
                }
            }

            if (result.Type != ParsingResultType.Success)
            {
                return(result);
            }

            var paramResult = TryTextParam(remainder);

            if (paramResult.WasSuccessful == false && hasRequiredParams == true)
            {
                GConsole.WriteLine(Resources.text.FailureParsingRequiredParam, command.Name());
                result.Type = ParsingResultType.ParsingFailure;
                return(result);
            }

            string parameters = paramResult.Value.TrimEnd();

            result.Parameters = parameters.Split(new char[] { ' ', '\t' }).ToList();

            if (paramResult.Remainder.AtEnd == true)
            {
                result.Type = ParsingResultType.Success;
                return(result);
            }

            remainder = remainder.Substring(paramResult.Remainder.Position - 1).TrimEnd();

            var flagsResult = ParsingHelpers.TryFlags(remainder);

            if (flagsResult.WasSuccessful == false || flagsResult.Remainder.AtEnd == false)
            {
                GConsole.WriteLine(Resources.text.FailureParsingFlags, remainder, command.Name());
                result.Type = ParsingResultType.ParsingFailure;
                return(result);
            }

            var parsedFlags = flagsResult.Value.ToList();

            foreach (var flag in command.GetFlags())
            {
                if (flag.FindInList(ref parsedFlags))
                {
                    result.Flags.Add(flag);
                }
            }

            if (parsedFlags.Count > 0)
            {
                GConsole.WriteLine(Resources.text.UnknownFlags, String.Join(",", parsedFlags), command.Name());
                result.Type = ParsingResultType.ParsingFailure;
                return(result);
            }

            result.Type = ParsingResultType.Success;
            return(result);
        }
Example #7
0
 public AutoCompletionResult AutoComplete(string line)
 {
     return(ParsingHelpers.AutoCompleteCommandBody(line, this));
 }