Exemplo n.º 1
0
 internal static Token Token(this SymbolResult symbolResult)
 {
     return(symbolResult switch
     {
         CommandResult commandResult => commandResult.Token,
         OptionResult optionResult => optionResult.Token ?? CreateImplicitToken(optionResult.Option),
         _ => throw new ArgumentOutOfRangeException(nameof(symbolResult))
     });
Exemplo n.º 2
0
        private static void PopulateDefaultValues(
            CommandResult parentCommandResult,
            ISymbol symbol)
        {
            var symbolResult = parentCommandResult.Children.ResultFor(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));

                    parentCommandResult.Children.Add(optionResult);
                    optionResult.AddToken(token);

                    break;

                case Argument argument when argument.HasDefaultValue:

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

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

                    parentCommandResult.Children.Add(argumentResult);
                    parentCommandResult.AddToken(implicitToken);

                    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));
            }
        }
Exemplo n.º 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,
                                null,
                                commandResult);

                            var childArgumentResult = optionResult.GetOrCreateDefaultArgumentResult(
                                option.Argument);

                            optionResult.Children.Add(childArgumentResult);
                            commandResult.Children.Add(optionResult);
                            _rootCommandResult.AddToSymbolMap(optionResult);

                            break;

                        case Argument argument when argument.HasDefaultValue:

                            var argumentResult = commandResult.GetOrCreateDefaultArgumentResult(
                                argument);

                            commandResult.Children.Add(argumentResult);
                            _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));
                    }
                }
            }
        }
Exemplo n.º 4
0
        internal static ArgumentConversionResult ConvertIfNeeded(
            this OptionResult optionResult,
            Type type)
        {
            if (optionResult is null)
            {
                throw new ArgumentNullException(nameof(optionResult));
            }

            return(optionResult.ArgumentConversionResult
                   .ConvertIfNeeded(optionResult, type));
        }
Exemplo n.º 5
0
        protected override void VisitOptionNode(OptionNode optionNode)
        {
            if (_innermostCommandResult !.Children.ResultFor(optionNode.Option) is null)
            {
                var optionResult = new OptionResult(
                    optionNode.Option,
                    optionNode.Token,
                    _innermostCommandResult
                    );

                _innermostCommandResult.Children.Add(optionResult);
            }
        }
Exemplo n.º 6
0
        private void ValidateOptionResult(OptionResult optionResult)
        {
            var argument = optionResult.Option.Argument;

            var arityFailure = ArgumentArity.Validate(
                optionResult,
                argument,
                argument.Arity.MinimumNumberOfValues,
                argument.Arity.MaximumNumberOfValues);

            if (arityFailure != null)
            {
                _errors.Add(
                    new ParseError(arityFailure.ErrorMessage !, optionResult));
            }

            if (optionResult.Option is Option option)
            {
                for (var i = 0; i < option.Validators.Count; i++)
                {
                    var validate = option.Validators[i];
                    var message  = validate(optionResult);

                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        _errors.Add(new ParseError(message !, optionResult));
                    }
                }
            }

            for (var i = 0; i < optionResult.Children.Count; i++)
            {
                var result = optionResult.Children[i];
                if (result is ArgumentResult argumentResult)
                {
                    ValidateArgumentResult(argumentResult);
                }
            }
        }
        private void ValidateOptionResult(OptionResult optionResult)
        {
            var argument = optionResult.Option.Argument;

            var arityFailure = ArgumentArity.Validate(
                optionResult,
                argument,
                argument.Arity.MinimumNumberOfValues,
                argument.Arity.MaximumNumberOfValues);

            if (arityFailure != null)
            {
                _errors.Add(
                    new ParseError(arityFailure.ErrorMessage !, optionResult));
            }

            if (optionResult.Option is Option option)
            {
                foreach (var validate in option.Validators)
                {
                    var message = validate(optionResult);

                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        _errors.Add(new ParseError(message !, optionResult));
                    }
                }
            }

            foreach (var argumentResult in optionResult
                     .Children
                     .OfType <ArgumentResult>())
            {
                ValidateArgumentResult(argumentResult);
            }
        }
Exemplo n.º 8
0
 public static T GetValueOrDefault <T>(this OptionResult optionResult) =>
 optionResult.ConvertIfNeeded(typeof(T))
 .GetValueOrDefault <T>();
Exemplo n.º 9
0
 public static object?GetValueOrDefault(this OptionResult optionResult) => optionResult.GetValueOrDefault <object?>();
Exemplo n.º 10
0
 internal static ArgumentConversionResult ConvertIfNeeded(
     this OptionResult optionResult,
     Type type) =>
 optionResult.ArgumentConversionResult
 .ConvertIfNeeded(optionResult, type);
Exemplo n.º 11
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));
                    }
                }
            }
        }
Exemplo n.º 12
0
 protected override void VisitOptionResult(OptionResult optionResult)
 {
     OptionResults.Add(optionResult.Option, optionResult);
 }
Exemplo n.º 13
0
 public static object?GetValueOrDefault(this OptionResult optionResult)
 {
     return(optionResult.GetValueOrDefault <object?>());
 }