Esempio n. 1
0
        private static void Validate(CommandValidatorContext <QueryWebCommandOptions> context)
        {
            if (String.IsNullOrEmpty(context.CommandOptions.SearchEngineQuery) && String.IsNullOrWhiteSpace(context.CommandOptions.WebsiteAddress))
            {
                context.AddError(new OptionMissingError(context.GetOptionName(a => a.WebsiteAddress)));
            }

            if (!String.IsNullOrWhiteSpace(context.CommandOptions.WebsiteAddress))
            {
                if (
                    !Uri.TryCreate(context.CommandOptions.WebsiteAddress, UriKind.Absolute, out var uri) ||
                    (
                        uri.Scheme != Uri.UriSchemeHttp &&
                        uri.Scheme != Uri.UriSchemeHttps
                    )
                    )
                {
                    context.AddError(
                        new InvalidOptionError(
                            context.GetOptionName(a => a.WebsiteAddress),
                            $"The website address '{context.CommandOptions.WebsiteAddress}' is not a valid web URL."
                            )
                        );
                }
            }
        }
        private static void Validate(CommandValidatorContext <DateCommandOptions> context)
        {
            if (context.CommandOptions.DifferenceToDate != null)
            {
                if (context.CommandOptions.DifferenceToDate.Value < context.CommandOptions.Date)
                {
                    context.AddError(
                        new InvalidOptionError(
                            context.GetOptionName(a => a.DifferenceToDate),
                            $"The given date must be later than {context.CommandOptions.Date}."
                            )
                        );
                }

                if (context.CommandOptions.DisplayInUtc)
                {
                    context.AddError(
                        new InvalidOptionError(
                            context.GetOptionName(a => a.DisplayInUtc),
                            "The option --utc can not be used when the option --differenceTo is used."
                            )
                        );
                }
            }
        }
        private static void Validate(CommandValidatorContext <FileReplaceCommandOptions> context)
        {
            if (!context.CommandOptions.InFile.Exists)
            {
                context.AddError(
                    new InvalidOptionError(
                        context.GetOptionName(a => a.InFile),
                        "The specified file does not exist."
                        )
                    );
            }

            if (context.CommandOptions.OutFile.Exists && !context.CommandOptions.OverrideOutFile)
            {
                context.AddError(
                    new InvalidOptionError(
                        context.GetOptionName(a => a.OutFile),
                        "The specified file already exists. Use the option --override to override the file."
                        )
                    );
            }
        }
Esempio n. 4
0
        public void AddError_ShouldAddErrorToParseResult()
        {
            var parser        = A.Fake <Parser>();
            var commandParser = A.Fake <CommandParser <Command1Options> >(ob => ob.WithArgumentsForConstructor(() => new CommandParser <Command1Options>(parser)));
            var parseResult   = A.Fake <ParseResult>();

            var context = new CommandValidatorContext <Command1Options>(commandParser, parseResult);

            var missingCommandError = new MissingCommandError();

            context.AddError(missingCommandError);

            A.CallTo(() => parseResult.AddError(missingCommandError)).MustHaveHappened();
        }