예제 #1
0
        protected override Task InitializeAsync()
        {
            var context = new ValidationContext();

            var result1 = BusinessRuleValidationResult.CreateErrorWithTag("Error1 message", "A");
            var result2 = BusinessRuleValidationResult.CreateWarningWithTag("Warning1 message", "B");
            var result3 = FieldValidationResult.CreateWarningWithTag("Property1", "Warning2 message", "C");

            var tag = new { Name = "A", Line = new Nullable <int>(2) };

            var result4 = BusinessRuleValidationResult.CreateErrorWithTag("Error2 message with object tag", tag);
            var result5 = BusinessRuleValidationResult.CreateErrorWithTag("Error3 message", "B");

            var result6  = BusinessRuleValidationResult.CreateError("Error3 message");
            var result7  = BusinessRuleValidationResult.CreateError("Error4 message");
            var result8  = FieldValidationResult.CreateWarningWithTag("Property2", "Warning3 message", new { Name = "A", Line = new Nullable <int>(1) });
            var result9  = FieldValidationResult.CreateWarningWithTag("Property2", "Warning4 message", new { Name = "A", Line = new Nullable <int>(2) });
            var result10 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning5 message", new { Name = "A", Line = new Nullable <int>(3) });
            var result11 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning6 message", new { Name = "A", Line = new Nullable <int>(20) });
            var result12 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning7 message", new { Name = "A", Line = new Nullable <int>(12) });
            var result13 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning8 message", new { Name = "A", Line = new Nullable <int>(10) });
            var result14 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning9 message", new { Name = "A", Line = new Nullable <int>(24) });

            context.AddBusinessRuleValidationResult(result1);
            context.AddBusinessRuleValidationResult(result2);
            context.AddFieldValidationResult(result3);
            context.AddBusinessRuleValidationResult(result4);
            context.AddBusinessRuleValidationResult(result5);
            context.AddBusinessRuleValidationResult(result6);
            context.AddBusinessRuleValidationResult(result7);
            context.AddFieldValidationResult(result8);
            context.AddFieldValidationResult(result9);
            context.AddFieldValidationResult(result10);
            context.AddFieldValidationResult(result11);
            context.AddFieldValidationResult(result12);
            context.AddFieldValidationResult(result13);
            context.AddFieldValidationResult(result14);

            ValidationContext = context;

            return(base.InitializeAsync());
        }
예제 #2
0
        public IValidationContext Parse(List <string> commandLineArguments, IContext targetContext)
        {
            var validationContext = new ValidationContext();

            targetContext.OriginalCommandLine = string.Join(" ", commandLineArguments);

            var isHelp = commandLineArguments.Any(commandLineArgument => commandLineArgument.IsHelp());

            if (isHelp)
            {
                targetContext.IsHelp = true;
                return(validationContext);
            }

            var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext);

            var handledOptions = new HashSet <char>();

            Log.Debug("Parsing command line");

            for (var i = 0; i < commandLineArguments.Count; i++)
            {
                var commandLineArgument = commandLineArguments[i];

                try
                {
                    // Allow the first one to be a non-switch
                    if (i == 0)
                    {
                        if (!commandLineArguments[i].IsSwitch())
                        {
                            var emptyOptionDefinition = (from x in optionDefinitions
                                                         where !x.HasSwitch()
                                                         select x).FirstOrDefault();

                            if (emptyOptionDefinition == null)
                            {
                                var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoEmptySwitch"), commandLineArgument);
                                Log.Error(message);
                                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(message));
                                continue;
                            }

                            UpdateContext(targetContext, emptyOptionDefinition, commandLineArgument);
                            handledOptions.Add(emptyOptionDefinition.ShortName);
                            continue;
                        }
                    }

                    if (!commandLineArgument.IsSwitch())
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoSwitch"), commandLineArgument);
                        Log.Warning(message);
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var optionDefinition = (from x in optionDefinitions
                                            where x.IsSwitch(commandLineArgument)
                                            select x).FirstOrDefault();
                    if (optionDefinition == null)
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseSwitchNotRecognized"), commandLineArgument);
                        Log.Warning(message);
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var value = string.Empty;
                    if (!optionDefinition.AcceptsValue)
                    {
                        // Assume boolean switch
                        value = "true";
                    }
                    else
                    {
                        if (commandLineArguments.Count <= i + 1)
                        {
                            var message = string.Format(_languageService.GetString("CommandLine_CannotParseValueMissing"), commandLineArgument);
                            Log.Warning(message);
                            validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                            continue;
                        }

                        value = commandLineArguments[++i];
                    }

                    UpdateContext(targetContext, optionDefinition, value);
                    handledOptions.Add(optionDefinition.ShortName);
                }
                catch (Exception ex)
                {
                    validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(_languageService.GetString("CommandLine_CannotParseExceptionOccurred"), commandLineArgument, ex.Message));
                }
            }

            Log.Debug("Checking if all required options are specified");

            foreach (var optionDefinition in optionDefinitions)
            {
                if (optionDefinition.IsMandatory)
                {
                    if (!handledOptions.Contains(optionDefinition.ShortName))
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_RequiredSwitchNotSpecified"), optionDefinition);
                        Log.Error(message);
                        validationContext.AddFieldValidationResult(FieldValidationResult.CreateError(optionDefinition.GetSwitchDisplay(), message));
                    }
                }
            }

            Log.Debug("Finishing the context");

            targetContext.Finish();

            return(validationContext);
        }