Exemplo n.º 1
0
 public string[] GetNames(ConsoleAppOptions options)
 {
     if (CommandAttribute != null)
     {
         return(CommandAttribute.CommandNames);
     }
     return(new[] { options.NameConverter(MethodInfo.Name) });
 }
Exemplo n.º 2
0
        public ConsoleApp AddSubCommands <T>()
        {
            var methods = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            var rootName = typeof(T).GetCustomAttribute <CommandAttribute>()?.CommandNames[0] ?? options.NameConverter(typeof(T).Name);

            foreach (var method in methods)
            {
                if (method.Name == "Dispose" || method.Name == "DisposeAsync")
                {
                    continue;                                                            // ignore IDisposable
                }
                if (method.GetCustomAttribute <RootCommandAttribute>() != null)
                {
                    var command = new CommandDescriptor(CommandType.DefaultCommand, method);
                    commands.AddRootCommand(command);
                }
                else
                {
                    var command = new CommandDescriptor(CommandType.SubCommand, method, parentCommand: rootName);
                    commands.AddSubCommand(rootName, command);
                }
            }
            return(this);
        }