Esempio n. 1
0
        public CommandRunData LoadCommand(string[] args)
        {
            var optionPrefixes      = new[] { '/', '-' };
            ConsoleCommandInfo info = null;

            if (args.IsNullOrEmpty() || string.IsNullOrEmpty(args[0]))
            {
                CommandsByType.TryGetValue(Config.DefaultCommand, out info);
                if (info != null)
                {
                    args = new[] { info.Attribute.FirstPrototype }.Union(args).ToArray();
                }
            }

            var commandName = args[0].Trim();

            if (optionPrefixes.Any(p => p == commandName[0]))
            {
                //did the user type /help first?
                if (CommandsByName.TryGetValue(commandName.Substring(1), out info) &&
                    info == CommandsByType[typeof(HelpCommand)])
                {
                    commandName = commandName.TrimStart(optionPrefixes);
                }
                else
                {
                    CommandsByType.TryGetValue(Config.DefaultCommand, out info);
                    if (info != null)
                    {
                        args = new[] { info.Attribute.FirstPrototype }.Union(args).ToArray();
                    }
                }
            }

            var options = new OptionSet();

            if (info != null || CommandsByName.TryGetValue(commandName, out info))
            {
                if (info.CommandType == typeof(HelpCommand))
                {
                    ConsoleCommandInfo infoForHelp;
                    if (args.Length > 1 &&
                        CommandsByName.TryGetValue(args[1].TrimStart(optionPrefixes), out infoForHelp))
                    {
                        var instance = (IConsoleCommand)infoForHelp.CommandType.Resolve();
                        return(new CommandRunData {
                            Command = HelpCommand.ForCommand(infoForHelp, instance)
                        });
                    }
                    return(new CommandRunData {
                        Command = HelpCommand.ForCommands(Commands)
                    });
                }

                return(GetCommandRunData(info, args, options));
            }

            Writer.WriteLine("Unknown command: " + commandName);
            return(new CommandRunData {
                Command = HelpCommand.ForCommands(Commands)
            });
        }