public static void ShowParsedCommand(IConsoleCommand consoleCommand, TextWriter consoleOut)
        {
            if (!consoleCommand.TraceCommandAfterParse)
            {
                return;
            }

            var skippedProperties = new [] {
                "Command",
                "OneLineDescription",
                "LongDescription",
                "Options",
                "TraceCommandAfterParse",
                "RemainingArgumentsCount",
                "RemainingArgumentsHelpText",
                "RequiredOptions"
            };

            var properties = consoleCommand.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                             .Where(p => !skippedProperties.Contains(p.Name));

            var fields = consoleCommand.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)
                         .Where(p => !skippedProperties.Contains(p.Name));

            var allValuesToTrace = new Dictionary <string, string>();

            foreach (var property in properties)
            {
                var value = property.GetValue(consoleCommand, new object[0]);
                allValuesToTrace[property.Name] = value?.ToString() ?? "null";
            }

            foreach (var field in fields)
            {
                allValuesToTrace[field.Name] = MakeObjectReadable(field.GetValue(consoleCommand));
            }

            consoleOut.WriteLine();

            string introLine = $"Executing {consoleCommand.Command}";

            if (string.IsNullOrEmpty(consoleCommand.OneLineDescription))
            {
                introLine = introLine + ":";
            }
            else
            {
                introLine = introLine + " (" + consoleCommand.OneLineDescription + "):";
            }

            consoleOut.WriteLine(introLine);

            foreach (var value in allValuesToTrace.OrderBy(k => k.Key))
            {
                consoleOut.WriteLine("    " + value.Key + " : " + value.Value);
            }

            consoleOut.WriteLine();
        }
Exemplo n.º 2
0
        private ICommandHelp GetCommandHelpInternal(IConsoleCommand consoleCommand)
        {
            var commandHelp = new CommandHelp();

            if (consoleCommand != null)
            {
                var cmd  = consoleCommand.GetType();
                var attr = cmd.GetCustomAttributes(typeof(ConsoleCommandAttribute), false).FirstOrDefault() as ConsoleCommandAttribute ?? new ConsoleCommandAttribute(CreateCommandFromClass(cmd.Name), Constants.CommandCategoryKeys.General, $"Prompt_{cmd.Name}_Description");
                commandHelp.Name        = attr.Name;
                commandHelp.Description = LocalizeString(attr.DescriptionKey, consoleCommand.LocalResourceFile);
                var commandParameters = cmd.GetFields(BindingFlags.NonPublic | BindingFlags.Static)
                                        .Select(x => x.GetCustomAttributes(typeof(ConsoleCommandParameterAttribute), false).FirstOrDefault())
                                        .Cast <ConsoleCommandParameterAttribute>().ToList();
                if (commandParameters.Any())
                {
                    var options = commandParameters.Where(attribute => attribute != null).Select(attribute => new CommandOption
                    {
                        Name         = attribute.Name,
                        Required     = attribute.Required,
                        DefaultValue = attribute.DefaultValue,
                        Description  =
                            LocalizeString(attribute.DescriptionKey, consoleCommand.LocalResourceFile)
                    }).ToList();
                    commandHelp.Options = options;
                }
                commandHelp.ResultHtml = consoleCommand.ResultHtml;
            }
            else
            {
                commandHelp.Error = LocalizeString("Prompt_CommandNotFound");
            }
            return(commandHelp);
        }
Exemplo n.º 3
0
        public ICommandHelp GetCommandHelp(IConsoleCommand consoleCommand)
        {
            var cacheKey = $"{consoleCommand.GetType().Name}-{System.Threading.Thread.CurrentThread.CurrentUICulture.Name}";

            return(DataCache.GetCachedData <ICommandHelp>(new CacheItemArgs(cacheKey, CacheItemPriority.Low),
                                                          c => GetCommandHelpInternal(consoleCommand)));
        }
 private static void ValidateConsoleCommand(IConsoleCommand command)
 {
     if (string.IsNullOrEmpty(command.Command))
     {
         throw new InvalidOperationException($"Command {command.GetType().Name} did not call IsCommand in its constructor to indicate its name and description.");
     }
 }
Exemplo n.º 5
0
        static ConsoleCommandInfo BuildCommandInfo(IConsoleCommand command)
        {
            var info = BuildCommandInfo(command.GetType());

            info.Instance = command;
            return(info);
        }
Exemplo n.º 6
0
        private void LoadAllCommandsFromAssembly(Assembly assembly)
        {
            Trace.Assert(assembly != null);

            foreach (Type t in GetAllTypesThatImplement(assembly, typeof(IConsoleCommand)))
            {
                IConsoleCommand command = (IConsoleCommand)kernel.Get(t);
                commands.Add(command.GetName(), t);
                if (command.GetType().Assembly.FullName.IndexOf("js.net.test.module") < 0)
                {
                    helpManager.AddHelpForConsoleCommand(command);
                }
            }
        }
Exemplo n.º 7
0
        private void Execute(List <string> args)
        {
            if (args.Count == 0)
            {
                PrintHelp();
                return;
            }
            IConsoleCommand command = GetCommand(args[0]);

            if (command == null)
            {
                _log.Error("Could not find command '" + args[0] + "'");
                PrintHelp();
                return;
            }
            args.RemoveAt(0);

            _log.Debug("Executing command [" + command.GetType() + "]");
            command.Execute(args);
        }
 private static void ValidateConsoleCommand(IConsoleCommand command)
 {
     if (string.IsNullOrEmpty(command.Command))
     {
         throw new InvalidOperationException(string.Format(
             "Command {0} did not call IsCommand in its constructor to indicate its name and description.",
             command.GetType().Name));
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Gets the command name for the IConsoleCommand command.
 /// </summary>
 /// <param name="command">The IConsoleCommand command.</param>
 /// <returns>The name of the command.</returns>
 public static string GetCommandName(this IConsoleCommand command)
 {
     return(command?.GetType().Name.Replace("Command", ""));
 }
Exemplo n.º 10
0
        public static void ShowParsedCommand(IConsoleCommand consoleCommand, TextWriter consoleOut)
        {

            if (!consoleCommand.TraceCommandAfterParse || consoleCommand.IsHidden)
            {
                return;
            }

            string[] skippedProperties = {
                "IsHidden",
                "Command",
                "OneLineDescription",
                "LongDescription",
                "Options",
                "Console",
                "TraceCommandAfterParse",
                "RemainingArgumentsCount",
                "RemainingArgumentsHelpText",
                "ShowHelpWithoutFurtherArgs",
                "RequiredOptions"
            };

            var properties = consoleCommand.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => !skippedProperties.Contains(p.Name));

            var fields = consoleCommand.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => !skippedProperties.Contains(p.Name));

            Dictionary<string,string> allValuesToTrace = new Dictionary<string, string>();

            foreach (var property in properties)
            {
                object value = property.GetValue(consoleCommand, new object[0]);
                allValuesToTrace[property.Name] = value != null ? value.ToString() : "null";
            }

            foreach (var field in fields)
            {
                allValuesToTrace[field.Name] = MakeObjectReadable(field.GetValue(consoleCommand));
            }

            consoleOut.WriteLine();

            string introLine = String.Format("Executing {0}", ConsoleUtil.FormatCommandName(consoleCommand.Command));

            if (string.IsNullOrEmpty(consoleCommand.OneLineDescription))
            {
                introLine = introLine + ":";
                Console.WriteLine(introLine);
            }
            else
            {
                var description =  consoleCommand.OneLineDescription;
                PrintCommandConsoleFriendly(consoleOut, introLine, description, introLine.Length, offset => "{0} ({1}):");
            }

            foreach(var value in allValuesToTrace.OrderBy(k => k.Key))
                consoleOut.WriteLine("    " + value.Key + " : " + value.Value);

            consoleOut.WriteLine();
        }