示例#1
0
        /// <inheritdoc />
        public virtual bool TryValidateInput(IInputArguments input, out string error)
        {
            if (input.IsReady)
            {
                // validate positional arguments
                if (input.HasPositionalArgs)
                {
                    // check if input allowed
                    if (!this.AllowsPositionalInput)
                    {
                        error = $"invalid input arguments ({input.TokenString}), token {this.Name} doesn't allow input.";
                        return(false);
                    }

                    // check argument count
                    if (input.PositionalArgs.Length > this.MaxPositionalArgs)
                    {
                        error = $"invalid input arguments ({input.TokenString}), token {this.Name} doesn't allow more than {this.MaxPositionalArgs} argument{(this.MaxPositionalArgs == 1 ? "" : "s")}.";
                        return(false);
                    }

                    // check values
                    if (input.TokenString.Value != InternalConstants.TokenPlaceholder)
                    {
                        InvariantHashSet validInputs = this.GetValidPositionalArgs();
                        if (validInputs?.Any() == true)
                        {
                            if (input.PositionalArgs.Any(arg => !validInputs.Contains(arg)))
                            {
                                string raw    = input.TokenString.Raw;
                                string parsed = input.TokenString.Value;
                                error = $"invalid input arguments ({(raw != parsed ? $"{raw} => {parsed}" : parsed)}) for {this.Name} token, expected any of '{string.Join("', '", validInputs.OrderByHuman())}'";
                                return(false);
                            }
                        }
                    }
                }

                // validate named arguments
                if (input.HasNamedArgs)
                {
                    if (this.ValidNamedArguments != null)
                    {
                        if (!this.ValidNamedArguments.Any())
                        {
                            error = $"invalid named argument '{input.NamedArgs.First().Key}' for {this.Name} token, which does not accept any named arguments.";
                            return(false);
                        }

                        string invalidKey = (from arg in input.NamedArgs where !this.ValidNamedArguments.Contains(arg.Key) select arg.Key).FirstOrDefault();
                        if (invalidKey != null)
                        {
                            error = $"invalid named argument '{invalidKey}' for {this.Name} token, expected any of '{string.Join("', '", this.ValidNamedArguments.OrderByHuman())}'";
                            return(false);
                        }
                    }
                }
            }

            // no issues found
            error = null;
            return(true);
        }