/// <summary> /// Adds an option to the template. /// </summary> /// <param name="nameTemplate">A '|' separated list of valid names for this option.</param> /// <param name="description">A description of the option.</param> /// <param name="action">The action to invoke for this option (may be null).</param> /// <returns>This.</returns> public CommandLineTemplate AddOption(string nameTemplate, string description, OptionAction action) { string[] names = nameTemplate.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); OptionTemplate optionTemplate = new OptionTemplate(description, (action != null ? action : (O) => { })); foreach (string name in names) { OptionTemplates.Add(name, optionTemplate); } 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); } } } }