Esempio n. 1
0
        private void ParseOption(
            CommandTreeParserContext context,
            CommandTreeTokenStream stream,
            CommandTreeToken token,
            CommandTree node,
            bool isLongOption)
        {
            // Consume the option token.
            stream.Consume(isLongOption ? CommandTreeToken.Kind.LongOption : CommandTreeToken.Kind.ShortOption);

            if (context.State == State.Normal)
            {
                // Find the option.
                var option = node.FindOption(token.Value, isLongOption);
                if (option != null)
                {
                    node.Mapped.Add(new MappedCommandParameter(
                                        option, ParseOptionValue(context, stream, token, node, option)));

                    return;
                }

                // Help?
                if (_help?.IsMatch(token.Value) == true)
                {
                    node.ShowHelp = true;
                    return;
                }
            }

            if (context.State == State.Remaining)
            {
                ParseOptionValue(context, stream, token, node, null);
                return;
            }

            if (context.ParsingMode == ParsingMode.Strict)
            {
                throw ParseException.UnknownOption(context.Arguments, token);
            }
            else
            {
                ParseOptionValue(context, stream, token, node, null);
            }
        }
Esempio n. 2
0
        private static string ParseOptionValue(
            CommandTreeParserContext context,
            CommandTreeTokenStream stream,
            CommandTreeToken token,
            CommandTree current,
            CommandParameter parameter)
        {
            var value = (string)null;

            // Parse the value of the token (if any).
            var valueToken = stream.Peek();

            if (valueToken?.TokenKind == CommandTreeToken.Kind.String)
            {
                var parseValue = true;
                if (token.TokenKind == CommandTreeToken.Kind.ShortOption && token.IsGrouped)
                {
                    parseValue = false;
                }

                if (context.State == State.Normal && parseValue)
                {
                    // Is this a command?
                    if (current.Command.FindCommand(valueToken.Value) == null)
                    {
                        if (parameter != null)
                        {
                            if (parameter.ParameterKind == ParameterKind.Single)
                            {
                                value = stream.Consume(CommandTreeToken.Kind.String).Value;
                            }
                            else if (parameter.ParameterKind == ParameterKind.Flag)
                            {
                                // Flags cannot be assigned a value.
                                throw ParseException.CannotAssignValueToFlag(context.Arguments, token);
                            }
                        }
                        else
                        {
                            // Unknown parameter value.
                            value = stream.Consume(CommandTreeToken.Kind.String).Value;

                            // In relaxed parsing mode?
                            if (context.ParsingMode == ParsingMode.Relaxed)
                            {
                                context.AddRemainingArgument(token.Value, value);
                            }
                        }
                    }
                }
                else
                {
                    context.AddRemainingArgument(token.Value, parseValue ? valueToken.Value : null);
                }
            }
            else
            {
                if (context.State == State.Remaining || context.ParsingMode == ParsingMode.Relaxed)
                {
                    context.AddRemainingArgument(token.Value, null);
                }
            }

            // No value?
            if (context.State == State.Normal)
            {
                if (value == null && parameter != null)
                {
                    if (parameter.ParameterKind == ParameterKind.Flag)
                    {
                        value = "true";
                    }
                    else
                    {
                        switch (parameter)
                        {
                        case CommandOption option:
                            throw ParseException.OptionHasNoValue(context.Arguments, token, option);

                        default:
                            // This should not happen at all. If it does, it's because we've added a new
                            // option type which isn't a CommandOption for some reason.
                            throw new InvalidOperationException($"Found invalid parameter type '{parameter.GetType().FullName}'.");
                        }
                    }
                }
            }

            return(value);
        }
Esempio n. 3
0
        private void ParseString(
            CommandTreeParserContext context,
            CommandTreeTokenStream stream,
            CommandTree node)
        {
            if (context.State == State.Remaining)
            {
                stream.Consume(CommandTreeToken.Kind.String);
                return;
            }

            var token = stream.Expect(CommandTreeToken.Kind.String);

            // Command?
            var command = node.Command.FindCommand(token.Value);

            if (command != null)
            {
                if (context.State == State.Normal)
                {
                    node.Next = ParseCommand(context, node.Command, node, stream);
                }
                return;
            }

            // Current command has no arguments?
            if (!node.HasArguments())
            {
                throw ParseException.UnknownCommand(_configuration, node, context.Arguments, token);
            }

            // Argument?
            var parameter = node.FindArgument(context.CurrentArgumentPosition);

            if (parameter == null)
            {
                // No parameters left. Any commands after this?
                if (node.Command.Children.Count > 0 || node.Command.IsDefaultCommand)
                {
                    throw ParseException.UnknownCommand(_configuration, node, context.Arguments, token);
                }

                throw ParseException.CouldNotMatchArgument(context.Arguments, token);
            }

            // Yes, this was an argument.
            if (parameter.ParameterKind == ParameterKind.Vector)
            {
                // Vector
                var current    = stream.Current;
                var aggregator = new List <string>(); // TODO: Allocations
                while (current?.TokenKind == CommandTreeToken.Kind.String)
                {
                    var value = stream.Consume(CommandTreeToken.Kind.String).Value;
                    node.Mapped.Add(new MappedCommandParameter(parameter, value));
                    current = stream.Current;
                }
            }
            else
            {
                // Scalar
                var value = stream.Consume(CommandTreeToken.Kind.String).Value;
                node.Mapped.Add(new MappedCommandParameter(parameter, value));
                context.IncreaseArgumentPosition();
            }
        }