Exemplo n.º 1
0
        private static string GenerateCommandUsage(Type type, MethodInfo info)
        {
            StringBuilder sb = new StringBuilder();

            CliContainerAttribute containerAttribute = type.GetCustomAttribute <CliContainerAttribute>();
            CliCommandAttribute   commandAttribute   = info.GetCustomAttribute <CliCommandAttribute>();

            sb.AppendLine($"Help for {GlobalConfig.CmdName} {containerAttribute.Name} {commandAttribute.Name}");
            sb.AppendLine("");
            sb.AppendLine("Usage (<param> is required, [param] is optional):");
            sb.AppendLine("");


            sb.Append($"{GlobalConfig.CmdName} ");
            sb.Append($"{containerAttribute.Name} ");


            sb.Append($"{commandAttribute.Name} ");

            foreach (ParameterInfo pInfo in info.GetParameters())
            {
                if (DBNull.Value.Equals(pInfo.DefaultValue))
                {
                    sb.Append($"<{pInfo.ParameterType.ToString().Replace(pInfo.ParameterType.Namespace.ToString() + ".", "")} {pInfo.Name}> ");
                }
                else
                {
                    sb.Append($"[{pInfo.ParameterType.ToString().Replace(pInfo.ParameterType.Namespace.ToString() + ".", "")} {pInfo.Name}] ");
                }
            }

            return(sb.ToString());
        }
Exemplo n.º 2
0
        private static string GenerateContainerUsage(Type type)
        {
            StringBuilder         sb = new StringBuilder();
            CliContainerAttribute containerAttribute = type.GetCustomAttribute <CliContainerAttribute>();

            sb.AppendLine($"Help for: {GlobalConfig.CmdName} {containerAttribute.Name}");
            sb.AppendLine("");
            sb.AppendLine("Available Subcommands:");
            sb.AppendLine("");

            foreach (MethodInfo info in type.GetMethods())
            {
                CliCommandAttribute commandAttribute = info.GetCustomAttribute <CliCommandAttribute>();
                if (commandAttribute != null)
                {
                    sb.AppendLine($"\t-{commandAttribute.Name}");
                }
            }

            sb.AppendLine("");
            sb.AppendLine("In order to retrieve help for a command enter:");
            sb.AppendLine($"{GlobalConfig.CmdName} {containerAttribute.Name} <command> help");

            return(sb.ToString());
        }
Exemplo n.º 3
0
        private object CreateCommand(string name)
        {
            var commandTypes = GetCommandTypes();

            foreach (var commandType in commandTypes)
            {
                var commandAttribute = CliCommandAttribute.Extract(commandType);
                if (commandAttribute != null)
                {
                    if (string.Equals(commandAttribute.Name, name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(_serviceProvider.GetService(commandType));
                    }
                }
            }
            return(null);
        }
Exemplo n.º 4
0
 private static void ProcessMethods(Type type, string[] args)
 {
     if (args.Length < 1 || args[0] == "help")
     {
         Console.WriteLine(GenerateContainerUsage(type));
     }
     else
     {
         foreach (MethodInfo info in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
         {
             CliCommandAttribute commandAttribute = info.GetCustomAttribute <CliCommandAttribute>();
             if (commandAttribute != null)
             {
                 if (commandAttribute.Name == args[0])
                 {
                     string[] newArgs = new string[args.Length - 1];
                     Array.Copy(args, 1, newArgs, 0, newArgs.Length);
                     ProcessMethodExecution(type, info, newArgs);
                 }
             }
         }
     }
 }