示例#1
0
        public static void Validate(CommandModel model, CommandAppSettings settings)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (settings is null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (model.Commands.Count == 0 && model.DefaultCommand == null)
            {
                throw CommandConfigurationException.NoCommandConfigured();
            }

            foreach (var command in model.Commands)
            {
                // Alias collision?
                foreach (var alias in command.Aliases)
                {
                    if (model.Commands.Any(x => x.Name.Equals(alias, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw CommandConfigurationException.CommandNameConflict(command, alias);
                    }
                }
            }

            Validate(model.DefaultCommand);
            foreach (var command in model.Commands)
            {
                Validate(command);
            }

            if (settings.ValidateExamples)
            {
                ValidateExamples(model, settings);
            }
        }
示例#2
0
        private static void Validate(CommandInfo?command)
        {
            if (command == null)
            {
                return;
            }

            // Get duplicate options for command.
            var duplicateOptions = GetDuplicates(command);

            if (duplicateOptions.Length > 0)
            {
                throw CommandConfigurationException.DuplicateOption(command, duplicateOptions);
            }

            // No children?
            if (command.IsBranch && command.Children.Count == 0)
            {
                throw CommandConfigurationException.BranchHasNoChildren(command);
            }

            // Multiple vector arguments?
            var arguments = command.Parameters.OfType <CommandArgument>();

            if (arguments.Any(x => x.ParameterKind == ParameterKind.Vector))
            {
                // Multiple vector arguments for command?
                if (arguments.Count(x => x.ParameterKind == ParameterKind.Vector) > 1)
                {
                    throw CommandConfigurationException.TooManyVectorArguments(command);
                }

                // Make sure that vector arguments are specified last.
                if (arguments.Last().ParameterKind != ParameterKind.Vector)
                {
                    throw CommandConfigurationException.VectorArgumentNotSpecifiedLast(command);
                }
            }

            // Arguments
            var argumnets = command.Parameters.OfType <CommandArgument>();

            foreach (var argument in arguments)
            {
                if (argument.Required && argument.DefaultValue != null)
                {
                    throw CommandConfigurationException.RequiredArgumentsCannotHaveDefaultValue(argument);
                }
            }

            // Options
            var options = command.Parameters.OfType <CommandOption>();

            foreach (var option in options)
            {
                // Pair deconstructable?
                if (option.Property.PropertyType.IsPairDeconstructable())
                {
                    if (option.PairDeconstructor != null && option.Converter != null)
                    {
                        throw CommandConfigurationException.OptionBothHasPairDeconstructorAndTypeParameter(option);
                    }
                }
                else if (option.PairDeconstructor != null)
                {
                    throw CommandConfigurationException.OptionTypeDoesNotSupportDeconstruction(option);
                }

                // Optional options that are not flags?
                if (option.ParameterKind == ParameterKind.FlagWithValue && !option.IsFlagValue())
                {
                    throw CommandConfigurationException.OptionalOptionValueMustBeFlagWithValue(option);
                }
            }

            // Validate child commands.
            foreach (var childCommand in command.Children)
            {
                Validate(childCommand);
            }
        }