/// <summary> /// Adds a command to the template. /// </summary> /// <param name="nameTemplate">A '|' separated list of valid names for this option.</param> /// <param name="description">A description of the command</param> /// <param name="validOptions">A '|' separated list of valid options for this command. Using CommandLineTemplate.IgnoreOptions will accept any option</param> /// <param name="action">The action to invoke for this command (may be null).</param> /// <returns>This.</returns> public CommandLineTemplate AddCommand(string nameTemplate, string description, string validOptions, CommandAction action) { string[] names = nameTemplate.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); CommandTemplate commandTemplate = new CommandTemplate(description, validOptions, (action != null ? action : (O) => { })); foreach (string name in names) { CommandTemplates.Add(name, commandTemplate); } return(this); }
/// <summary> /// Validates the given command against this template and fills in the appropriate actions on the command and /// options. Invalid commands result in exceptions. /// </summary> /// <param name="command"></param> public void Validate(Command command) { System.Diagnostics.Debug.Assert(command != null); // Valid command ? if (!CommandTemplates.ContainsKey(command.Name)) { throw new InvalidCommandException(command); } // Assign command template. CommandTemplate commandTemplate = CommandTemplates[command.Name]; command.Template = commandTemplate; // Check options. foreach (Option option in command.Options) { // Valid option ? if (!OptionTemplates.ContainsKey(option.Name)) { throw new InvalidOptionException(option); } // Assign option template. OptionTemplate optionTemplate = OptionTemplates[option.Name]; option.Template = optionTemplate; } if (commandTemplate.ValidOptions != IgnoreOptions) { string[] options = commandTemplate.ValidOptions.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); foreach (Option option in command.Options) { // Valid options for this command ? if (Array.IndexOf(options, option.Name) < 0) { throw new InvalidOptionException(option); } } } }