Пример #1
0
        /// <summary>
        /// Validates the configuration after parsing
        /// </summary>
        /// <param name="countDefaultCommands">Count of default commands found</param>
        /// <returns>Operation result</returns>
        internal Operation.OperationResult Validate(int countDefaultCommands)
        {
            var operationResult = new Operation.OperationResult();

            // Run user actions
            var b = ConfigurationValidationAction?.Invoke(this, operationResult);

            if (b.HasValue && !b.Value)
            {
                operationResult.Messages.Add(Resources.ConfigurationValidationAction);
            }

            // Ensure there is at least one command
            if (!Commands.Any())
            {
                operationResult.Messages.Add(Resources.NoCommandFound);
            }

            // Ensure there is at most one default command
            if (countDefaultCommands > 1)
            {
                operationResult.Messages.Add(Resources.CommandDefaultMostOne);
            }

            // Ensure no command has a space in it
            var spacedParameters = Commands.Where(i => i.Name.Contains(" "));

            spacedParameters.ForEach(i => operationResult.Messages.Add(String.Format(Resources.CommandCanNotHaveSpace, i)));

            // Ensure no command has a reserved keyword
            if (Commands.Any(i => HelpCommandNames.Any(j => String.Compare(j, i.Name, true) == 0)))
            {
                operationResult.Messages.Add(String.Format(Resources.CommandReservedKeywords, String.Join("', '", HelpCommandNames)));
            }

            // Ensure all commands have distinct names
            var groups = Commands.Select(i => i.Name.ToLower()).ToLookup(i => i);

            if (groups.Any(i => i.Count() > 1))
            {
                var duplicate = groups.First(i => i.Count() > 1).Key;
                operationResult.Messages.Add(String.Format(Resources.CommandDuplicate, duplicate));
            }

            // Ensure each command is valid
            Commands.ForEach(i => i.ValidateConfigurationInternal(operationResult));

            return(operationResult);
        }