private bool ShowActionHelper(string[] args) { bool isShowed = false; var types = (from assembly in AppDomain.CurrentDomain.GetAssemblies() from type in assembly.GetTypes() where type.IsSubclassOf(typeof(Command)) select type).ToList(); foreach (Type type in types) { if (type.IsSubclassOf(typeof(Command))) { var commonOptionAttribute = type.GetCustomAttribute(typeof(CommandRoutePrefix)); if (commonOptionAttribute == null) { continue; } CommandRoutePrefix p = (CommandRoutePrefix)commonOptionAttribute; if (args[0].Equals(p.Prefix, StringComparison.InvariantCultureIgnoreCase)) { var command = CommandContainer.Instance.Container.Resolve(type) as Command; command.HelpHeader(); command.ActionHelpHeader(); Console.WriteLine("Options available for {0} command\n", args[0]); var methods = type.GetMethods(); foreach (MethodInfo methodInfo in methods) { Attribute a = methodInfo.GetCustomAttribute(typeof(CommandRoute)); if (a != null) { CommandRoute r = (CommandRoute)a; string actionLine = string.Format("{0} {1} {2}", ApplicationName, args[0], r.Action); string commandHelpText = p.CommonOptions + r.Options; string optionLine = commandHelpText.Replace("/", " /"); PrintWithSecondColumnAlignment(actionLine, optionLine, " "); Console.WriteLine(); } } command.ActionHelpExample(); isShowed = true; } } } return(isShowed); }
public void Run <TOption>(string[] args) { bool validate = false; var types = (from assembly in AppDomain.CurrentDomain.GetAssemblies() from type in assembly.GetTypes() where type.IsSubclassOf(typeof(Command)) select type).ToList(); foreach (Type type in types) { var commonOptionAttribute = type.GetCustomAttribute(typeof(CommandRoutePrefix)); if (commonOptionAttribute == null) { continue; } CommandRoutePrefix p = (CommandRoutePrefix)commonOptionAttribute; // check for the command if (args[0].Equals(p.Prefix, StringComparison.InvariantCultureIgnoreCase)) { var methods = type.GetMethods(); foreach (MethodInfo methodInfo in methods) { Attribute a = methodInfo.GetCustomAttribute(typeof(CommandRoute)); if (a != null) { CommandRoute r = (CommandRoute)a; // check the action if (args[1].Equals(r.Action, StringComparison.InvariantCultureIgnoreCase)) { string commandArgs = args.ToCommandStringLine(); string commandOpts = p.CommonOptions + r.Options; MatchCollection routeCollection = commandOpts.ToCommandOptionCollection(); if (routeCollection.Count.Equals(0)) { validate = true; } for (var i = 0; i < routeCollection.Count; i++) { if (commandArgs.Contains(routeCollection[i].Value)) { validate = true; } else { string missingOption = routeCollection[i].Value; throw new OptionNotFound(missingOption); } } if (validate) { var command = CommandContainer.Instance.Container.Resolve(type); try { methodInfo.Invoke(command, new object[] { args.ToOption <TOption>() }); } catch (Exception e) { throw e.InnerException; } return; } } } } throw new CommandNotFound(string.Format("{0} {1}", args[0], args[1])); } } }