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 ConfigurationException("Validation of examples failed.", ex); } } }
public static void Validate(CommandModel model, CommandAppSettings settings) { if (model.Commands.Count == 0 && model.DefaultCommand == null) { throw ConfigurationException.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 ConfigurationException.CommandNameConflict(command, alias); } } } Validate(model.DefaultCommand); foreach (var command in model.Commands) { Validate(command); } if (settings.ValidateExamples) { ValidateExamples(model); } }
public Configurator(ITypeRegistrar registrar) { _registrar = registrar; Commands = new List <ConfiguredCommand>(); Settings = new CommandAppSettings(registrar); Examples = new List <string[]>(); }
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[]>()); }