コード例 #1
0
		public void RegisterCommandType(ICommandType commandType)
		{
			foreach (string name in commandType.CommandNames())
				CommandNameToID[name] = TypeID;

			CommandTypes[TypeID++] = commandType;
		}
コード例 #2
0
        public void RegisterCommand(SlackBotCommand command, SlackBotV3 slackBot)
        {
            ICommandType    commandType    = CommandTypeRegistry.GetCommandType(command.Name);
            ICommandHandler commandHandler = commandType.MakeCommandHandler(slackBot);

            CommandContainer[GetCommandKey(command)] = commandHandler;
        }
コード例 #3
0
        public IExecutor GetExecutor(ICommandType commandType)
        {
            var executorInterface = typeof(IExecutor <>).MakeGenericType(commandType.GetType());

            return(Assembly.GetCallingAssembly().GetTypes()
                   .Where(T => T.IsClass)
                   .Where(T => executorInterface.IsAssignableFrom(T))
                   .Select((T => container.GetInstance(T))).Cast <IExecutor>().First());
        }
コード例 #4
0
        public bool TryGetCurrentCommandType(ITelegramUser user, out ICommandType commandType)
        {
            if (!userState.ContainsKey(user))
            {
                commandType = null;
                return(false);
            }

            var type = userState[user].GetType()
                       .GetInterfaces()
                       .Where(T => T.IsGenericType).
                       First(T => T.GetGenericTypeDefinition().Equals(typeof(ICommandState <>)))
                       .GetGenericArguments().First();

            commandType = (ICommandType)Activator.CreateInstance(type);
            return(true);
        }
コード例 #5
0
        private string GetAfterCommandContent(OperateRecordManager operateRecordManager)
        {
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < operateRecordManager.OperateList.Count; i++)
            {
                ICommandType commandType = operateRecordManager.OperateList[i];
                if (i < operateRecordManager.OperateList.Count - 1)
                {
                    stringBuilder.Append(commandType.GetAfterCommand() + ";\r\n");
                }
                else
                {
                    stringBuilder.Append(commandType.GetAfterCommand() + ".\r\n");
                }
            }

            return(stringBuilder.ToString());
        }
コード例 #6
0
        private ParsingResult ParseImpl(IEnumerator <string> argumentsEnumerator, ICommandType commandType)
        {
            var commandObjectBuilder = ExtractCommandLineOptions(commandType, argumentsEnumerator);

            if (commandObjectBuilder == null)
            {
                return(new ParsingResult(null, null, CommandParsingResultCode.CommandParametersNotValid));
            }
            var validation = commandObjectBuilder.Validate(_serviceProvider);

            if (!validation.IsValid)
            {
                _logger.ParsedCommandIsNotValid();
                var helpWriter = _serviceProvider.GetRequiredService <IHelpWriter>();
                helpWriter.WriteHelpForCommand(commandType);
                return(new ParsingResult(commandObjectBuilder.GenerateCommandObject(), validation.ValidationErrors, CommandParsingResultCode.CommandParametersNotValid));
            }
            return(new ParsingResult(commandObjectBuilder.GenerateCommandObject(), null, CommandParsingResultCode.Success));
        }
コード例 #7
0
 /// <summary>
 ///     Configure the command with the <see cref="ICommandType" /> representing the command.
 /// </summary>
 /// <param name="commandType">The <see cref="CommandType" /> of the command.</param>
 public virtual void Configure(ICommandType commandType)
 {
     CommandType = commandType;
 }
コード例 #8
0
        private ICommandObjectBuilder ExtractCommandLineOptions(ICommandType commandType, IEnumerator <string> argumentsEnumerator)
        {
            var commandObjectBuilder = commandType.CreateCommandObjectBuilder(_serviceProvider);

            if (commandObjectBuilder == null)
            {
                return(null);
            }
            var alwaysPutInArgumentList = false;

            while (true)
            {
                var argument = argumentsEnumerator.GetNextCommandLineItem();
                if (argument == null)
                {
                    break;
                }
                if (argument.Equals(Constants.EndOfOptions))
                {
                    alwaysPutInArgumentList = true;
                    continue;
                }

                if (alwaysPutInArgumentList || !argument.StartsWith(StringComparison.OrdinalIgnoreCase, Constants.OptionStarter))
                {
                    commandObjectBuilder.AddArguments(argument);
                    continue;
                }

                var starterLength = 2;
                Func <ICommandObjectBuilder, string, ICommandOption> commandOptionFinder = (co, optionName) => co.FindOption(optionName);
                if (!argument.StartsWith(Constants.LongNameOptionStarter))
                {
                    starterLength       = Constants.ShortNameOptionStarter.Length;
                    commandOptionFinder = (co, optionName) => co.FindOptionByShortName(optionName);
                }
                var    optionText = argument.Substring(starterLength);
                string value      = null;
                var    splitIndex = optionText.IndexOf(Constants.OptionSplitter);
                if (splitIndex > 0)
                {
                    value      = optionText.Substring(splitIndex + 1);
                    optionText = optionText.Substring(0, splitIndex);
                }

                var option = commandOptionFinder(commandObjectBuilder, optionText);
                if (option == null)
                {
                    var console = _serviceProvider.GetRequiredService <IConsole>();
                    console.WriteLineError(Constants.ExceptionMessages.FormatParserOptionNotFoundForCommand(commandType.Metadata.Name, optionText));
                    return(null);
                }

                if (option.ShouldProvideValue)
                {
                    value = value ?? argumentsEnumerator.GetNextCommandLineItem();
                }

                option.AssignValue(value);
            }
            return(commandObjectBuilder);
        }
コード例 #9
0
 private void WriteDescriptionForOneCommand(ICommandType commandType, int maxNameLength)
 {
     _console.Write($" {{0, -{maxNameLength}}}   ", commandType.Metadata.Name);
     // Starting index of the description
     _console.WriteLine(commandType.Metadata.Description);
 }
コード例 #10
0
 /// <summary>
 /// The parameter constructor
 /// </summary>
 /// <param name="commandType">The command type that will be executed</param>
 public Command(ICommandType commandType)
 {
     _commandType = commandType;
 }