private static void ValidateExamples(CommandModel model, CommandAppSettings settings)
    {
        var examples = new List <string[]>();

        examples.AddRangeIfNotNull(model.Examples);

        // Get all examples.
        var queue = new Queue <ICommandContainer>(new[] { model });

        while (queue.Count > 0)
        {
            var current = queue.Dequeue();

            foreach (var command in current.Commands)
            {
                examples.AddRangeIfNotNull(command.Examples);
                queue.Enqueue(command);
            }
        }

        // Validate all examples.
        foreach (var example in examples)
        {
            try
            {
                var parser = new CommandTreeParser(model, settings, ParsingMode.Strict);
                parser.Parse(example);
            }
            catch (Exception ex)
            {
                throw new CommandConfigurationException("Validation of examples failed.", ex);
            }
        }
    }
Example #2
0
    public Configurator(ITypeRegistrar registrar)
    {
        _registrar = registrar;

        Commands = new List <ConfiguredCommand>();
        Settings = new CommandAppSettings(registrar);
        Examples = new List <string[]>();
    }
Example #3
0
 public CommandModel(
     CommandAppSettings settings,
     CommandInfo?defaultCommand,
     IEnumerable <CommandInfo> commands,
     IEnumerable <string[]> examples)
 {
     ApplicationName = settings.ApplicationName;
     ParsingMode     = settings.ParsingMode;
     DefaultCommand  = defaultCommand;
     Commands        = new List <CommandInfo>(commands ?? Array.Empty <CommandInfo>());
     Examples        = new List <string[]>(examples ?? Array.Empty <string[]>());
 }
    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);
        }
    }