Ejemplo n.º 1
0
        private void ValidateArgumentResult(ArgumentResult argumentResult)
        {
            if (argumentResult.Argument is Argument argument)
            {
                var parseError =
                    argumentResult.Parent?.UnrecognizedArgumentError(argument) ??
                    argumentResult.CustomError(argument);

                if (parseError != null)
                {
                    _errors.Add(parseError);
                    return;
                }
            }

            if (argumentResult.GetArgumentConversionResult() is FailedArgumentConversionResult failed)
            {
                _errors.Add(
                    new ParseError(
                        failed.ErrorMessage !,
                        argumentResult));
            }
        }
Ejemplo n.º 2
0
        protected override void VisitCommandArgumentNode(
            CommandArgumentNode argumentNode)
        {
            var commandResult = _innermostCommandResult;

            var argumentResult =
                commandResult !.Children
                .OfType <ArgumentResult>()
                .SingleOrDefault(r => r.Symbol == argumentNode.Argument);

            if (argumentResult is null)
            {
                argumentResult =
                    new ArgumentResult(
                        argumentNode.Argument,
                        commandResult);

                commandResult.Children.Add(argumentResult);
            }

            argumentResult.AddToken(argumentNode.Token);
            commandResult.AddToken(argumentNode.Token);
        }
Ejemplo n.º 3
0
        protected override void VisitOptionArgumentNode(
            OptionArgumentNode argumentNode)
        {
            var option = argumentNode.ParentOptionNode.Option;

            var optionResult = _innermostCommandResult !.Children.ResultFor(option);

            var argument = argumentNode.Argument;

            var argumentResult =
                (ArgumentResult?)optionResult !.Children.ResultFor(argument);

            if (argumentResult is null)
            {
                argumentResult =
                    new ArgumentResult(
                        argumentNode.Argument,
                        optionResult);
                optionResult.Children.Add(argumentResult);
            }

            argumentResult.AddToken(argumentNode.Token);
            optionResult.AddToken(argumentNode.Token);
        }
Ejemplo n.º 4
0
        protected override void Stop(SyntaxNode node)
        {
            var helpWasRequested =
                _innermostCommandResult
                ?.Children
                .Any(o => o.Symbol is HelpOption) == true;

            if (helpWasRequested)
            {
                return;
            }

            ValidateCommandHandler();

            PopulateDefaultValues();

            ValidateCommandResult();

            foreach (var optionResult in _rootCommandResult !.AllOptionResults)
            {
                ValidateAndConvertOptionResult(optionResult);
            }

            var argumentResults = _rootCommandResult !
                                  .AllArgumentResults
                                  .ToList();

            if (argumentResults.Count > 0)
            {
                var arguments = _innermostCommandResult !.Command.Arguments.ToArray();

                for (var i = 0; i < arguments.Length; i++)
                {
                    if (argumentResults.Count == i)
                    {
                        var nextArgument       = arguments[i];
                        var nextArgumentResult = new ArgumentResult(
                            nextArgument,
                            _innermostCommandResult);

                        var previousArgumentResult = argumentResults[i - 1];

                        var passedOnTokens = _innermostCommandResult.Tokens.Skip(previousArgumentResult.Tokens.Count);

                        foreach (var token in passedOnTokens)
                        {
                            if (nextArgumentResult.IsArgumentLimitReached)
                            {
                                break;
                            }
                            nextArgumentResult.AddToken(token);
                        }

                        argumentResults.Add(nextArgumentResult);

                        previousArgumentResult.Parent !.Children.Add(nextArgumentResult);

                        _rootCommandResult.AddToSymbolMap(nextArgumentResult);
                    }

                    var argumentResult = argumentResults[i];

                    ValidateAndConvertArgumentResult(argumentResult);

                    if (argumentResult.PassedOnTokens is {} &&
Ejemplo n.º 5
0
        protected override void Stop(SyntaxNode node)
        {
            for (var i = 0; i < _innermostCommandResult !.Children.Count; i++)
            {
                if (_innermostCommandResult !.Children[i].Symbol is HelpOption)
                {
                    return;
                }
            }

            ValidateCommandHandler();

            PopulateDefaultValues();

            ValidateCommandResult();

            foreach (var optionResult in _rootCommandResult !.AllOptionResults)
            {
                ValidateAndConvertOptionResult(optionResult);
            }

            var argumentResults = _rootCommandResult !
                                  .AllArgumentResults
                                  .ToList();

            if (argumentResults.Count > 0)
            {
                var arguments = _innermostCommandResult !.Command.Arguments;

                for (var i = 0; i < arguments.Count; i++)
                {
                    if (argumentResults.Count == i)
                    {
                        var nextArgument       = arguments[i];
                        var nextArgumentResult = new ArgumentResult(
                            nextArgument,
                            _innermostCommandResult);

                        var previousArgumentResult = argumentResults[i - 1];

                        var passedOnTokensCount = _innermostCommandResult.Tokens.Count;

                        for (var j = previousArgumentResult.Tokens.Count; j < passedOnTokensCount; j++)
                        {
                            var token = _innermostCommandResult.Tokens[j];

                            if (nextArgumentResult.IsArgumentLimitReached)
                            {
                                break;
                            }

                            nextArgumentResult.AddToken(token);
                        }

                        argumentResults.Add(nextArgumentResult);

                        previousArgumentResult.Parent !.Children.Add(nextArgumentResult);

                        _rootCommandResult.AddToSymbolMap(nextArgumentResult);
                    }

                    var argumentResult = argumentResults[i];

                    ValidateAndConvertArgumentResult(argumentResult);

                    if (argumentResult.PassedOnTokens is {} &&
Ejemplo n.º 6
0
        private void PopulateDefaultValues()
        {
            var commandResults = _innermostCommandResult
                                 .RecurseWhileNotNull(c => c.Parent as CommandResult);

            foreach (var commandResult in commandResults)
            {
                foreach (var symbol in commandResult.Command.Children)
                {
                    var symbolResult = _rootCommandResult.FindResultFor(symbol);

                    if (symbolResult == null)
                    {
                        switch (symbol)
                        {
                        case Option option when option.Argument.HasDefaultValue:

                            var optionResult = new OptionResult(
                                option,
                                option.CreateImplicitToken());

                            var token = new ImplicitToken(
                                optionResult.GetDefaultValueFor(option.Argument),
                                TokenType.Argument);

                            optionResult.Children.Add(
                                new ArgumentResult(
                                    option.Argument,
                                    token,
                                    optionResult));

                            commandResult.Children.Add(optionResult);
                            optionResult.AddToken(token);
                            _rootCommandResult.AddToSymbolMap(optionResult);

                            break;

                        case Argument argument when argument.HasDefaultValue:

                            var implicitToken = new ImplicitToken(argument.GetDefaultValue(), TokenType.Argument);

                            var argumentResult = new ArgumentResult(
                                argument,
                                implicitToken,
                                commandResult);

                            commandResult.Children.Add(argumentResult);
                            commandResult.AddToken(implicitToken);
                            _rootCommandResult.AddToSymbolMap(argumentResult);

                            break;
                        }
                    }

                    if (symbolResult is OptionResult o &&
                        o.Option.Argument.Type == typeof(bool) &&
                        o.Children.Count == 0)
                    {
                        o.Children.Add(
                            new ArgumentResult(
                                o.Option.Argument,
                                new ImplicitToken(true, TokenType.Argument),
                                o));
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public static T GetValueOrDefault <T>(this ArgumentResult argumentResult) =>
 argumentResult.GetArgumentConversionResult()
 .ConvertIfNeeded(argumentResult, typeof(T))
 .GetValueOrDefault <T>();
Ejemplo n.º 8
0
 public static object?GetValueOrDefault(this ArgumentResult argumentResult) =>
 argumentResult.GetValueOrDefault <object?>();
Ejemplo n.º 9
0
        internal static ArgumentConversionResult Convert(
            ArgumentResult argumentResult,
            IArgument argument)
        {
            var parentResult = argumentResult.Parent;

            if (ShouldCheckArity() &&
                ArgumentArity.Validate(parentResult,
                                       argument,
                                       argument.Arity.MinimumNumberOfValues,
                                       argument.Arity.MaximumNumberOfValues) is FailedArgumentConversionResult failedResult)
            {
                return(failedResult);
            }

            if (parentResult.UseDefaultValueFor(argument))
            {
                var defaultValueFor = parentResult.GetDefaultValueFor(argument);

                return(ArgumentConversionResult.Success(argument, defaultValueFor));
            }

            if (argument is Argument a &&
                a.ConvertArguments != null)
            {
                if (argumentResult.ConversionResult != null)
                {
                    return(argumentResult.ConversionResult);
                }

                var success = a.ConvertArguments(argumentResult, out var value);

                if (value is ArgumentConversionResult conversionResult)
                {
                    return(conversionResult);
                }
                else if (success)
                {
                    return(ArgumentConversionResult.Success(argument, value));
                }
                else
                {
                    return(ArgumentConversionResult.Failure(argument, argumentResult.ErrorMessage ?? $"Invalid: {parentResult.Token} {string.Join(" ", parentResult.Tokens.Select(t => t.Value))}"));
                }
            }

            switch (argument.Arity.MaximumNumberOfValues)
            {
            case 0:
                return(ArgumentConversionResult.Success(argument, null));

            case 1:
                return(ArgumentConversionResult.Success(argument, parentResult.Tokens.Select(t => t.Value).SingleOrDefault()));

            default:
                return(ArgumentConversionResult.Success(argument, parentResult.Tokens.Select(t => t.Value).ToArray()));
            }

            bool ShouldCheckArity()
            {
                return(!(parentResult is OptionResult optionResult &&
                         optionResult.IsImplicit));
            }
        }
        internal virtual ArgumentConversionResult Convert(
            IArgument argument)
        {
            var parentResult = Parent;

            if (ShouldCheckArity() &&
                ArgumentArity.Validate(parentResult,
                                       argument,
                                       argument.Arity.MinimumNumberOfValues,
                                       argument.Arity.MaximumNumberOfValues) is FailedArgumentConversionResult failedResult)
            {
                return(failedResult);
            }

            if (argument is Argument arg)
            {
                if (parentResult !.UseDefaultValueFor(argument))
                {
                    var argumentResult = new ArgumentResult(arg, Parent);

                    var defaultValue = arg.GetDefaultValue(argumentResult);

                    if (string.IsNullOrEmpty(argumentResult.ErrorMessage))
                    {
                        return(ArgumentConversionResult.Success(
                                   argument,
                                   defaultValue));
                    }
                    else
                    {
                        return(ArgumentConversionResult.Failure(
                                   argument,
                                   argumentResult.ErrorMessage !));
                    }
                }

                if (arg.ConvertArguments != null)
                {
                    if (_conversionResult != null)
                    {
                        return(_conversionResult);
                    }

                    var success = arg.ConvertArguments(this, out var value);

                    if (value is ArgumentConversionResult conversionResult)
                    {
                        return(conversionResult);
                    }

                    if (success)
                    {
                        return(ArgumentConversionResult.Success(argument, value));
                    }

                    return(ArgumentConversionResult.Failure(
                               argument,
                               ErrorMessage ?? $"Invalid: {parentResult.Token()} {string.Join(" ", Tokens.Select(t => t.Value))}"));
                }
            }

            switch (argument.Arity.MaximumNumberOfValues)
            {
            case 1:
                return(ArgumentConversionResult.Success(
                           argument,
                           Tokens.Select(t => t.Value).SingleOrDefault()));

            default:
                return(ArgumentConversionResult.Success(
                           argument,
                           Tokens.Select(t => t.Value).ToArray()));
            }

            bool ShouldCheckArity() =>
            !(parentResult is OptionResult optionResult &&
              optionResult.IsImplicit);
        }
Ejemplo n.º 11
0
 protected override void VisitArgumentResult(ArgumentResult argumentResult)
 {
     ArgumentResults.Add(argumentResult.Argument, argumentResult);
 }