예제 #1
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 {} &&
예제 #2
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 {} &&
예제 #3
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,
                                    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,
                                commandResult);

                            commandResult.Children.Add(argumentResult);
                            commandResult.AddToken(implicitToken);
                            argumentResult.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,
                                o));
                    }
                }
            }
        }