public static CommandDefinition Define(string commandName, Type type) { CommandDefinition def = new CommandDefinition(); def.Name = commandName; def.CommandType = type; return def; }
private string GetCommandHelpString(CommandDefinition cmdDef) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("\n\n{0}:\t{1}\n", cmdDef.Name, cmdDef.Description); sb.AppendFormat("\nParameters for {0}\n------------------\n", cmdDef.Name); foreach (CommandOption opt in cmdDef.Options.Values) { if (opt.IsOptional) { sb.AppendFormat("[{0}]:\t{1}\n", opt.Name, opt.AdditionalHelp); } else { sb.AppendFormat("{0}:\t{1}\n", opt.Name, opt.AdditionalHelp); } } return sb.ToString(); }
/// <summary> /// Registers a command given that the command has command attribute and /// parameters attributes /// </summary> /// <param name="typCommand"></param> /// <returns></returns> public bool RegisterCommand(Type typCommand) { CommandDefinition cd = new CommandDefinition(); object[] attrs = typCommand.GetCustomAttributes(typeof(CommandAttribute), true); if (attrs.Length > 0) { // Create the command definition CommandAttribute cmdAttr = attrs[0] as CommandAttribute; cd.Name = cmdAttr.CommandName; cd.CommandType = typCommand; cd.Description = cmdAttr.CommandDescription; // Now create the command parameters PropertyInfo[] pInfos = typCommand.GetProperties(); foreach (PropertyInfo pinfo in pInfos) { attrs = pinfo.GetCustomAttributes(typeof(CommandParameterAttribute), true); if (attrs.Length > 0) { CommandParameterAttribute parmAttr = attrs[0] as CommandParameterAttribute; CommandOption opt = CommandOption.Opt(parmAttr.CommandParameter, parmAttr.OptionType, parmAttr.IsOptional); opt.AdditionalHelp = parmAttr.AdditionalHelpText; cd.Options.Add(opt.Name, opt); } } this.CommandDefinitions.Add(cd.Name, cd); return true; } return false; }