Esempio n. 1
0
        public void ActivateCommand_Activated()
        {
            var input = new ParsedInput("command");
            var res   = _resolver.Resolve(input);

            var act = _activator.Activate(res);

            act.Command.Should().NotBeNull();
            act.Command.GetType().Should().Be <Command>();
        }
        public async Task <int> Run(string inputStr)
        {
            try
            {
                // create new cancellation token source

                _currentCancellationTokenSource = new CancellationTokenSource();

                // ensure only one execution at a time

                if (IsRunning)
                {
                    throw new InvalidOperationException("Another command is already executing");
                }
                IsRunning = true;

                // parse input

                var input = new ParsedInput(inputStr);

                if (!input.IsValid)
                {
                    // process input parsing errors

                    foreach (var err in input.Errors)
                    {
                        switch (err.Type)
                        {
                        case ParsedInputValidationErrorType.NoCommandElement:
                            _interface.WriterCollection.Error.WriteLine("Invalid input :: no command element defined");
                            break;

                        case ParsedInputValidationErrorType.InvalidAlias:
                            _interface.WriterCollection.Error.WriteLine($"Invalid alisas :: [{err.Element.StartPosition}] {err.Element.Raw} - {err.Message}");
                            break;

                        default:
                            throw new ArgumentOutOfRangeException($"Value \"{err.Type}\" not defined for switch.");
                        }
                    }

                    // list unexpected values

                    foreach (var elem in input.Elements.Where(e => e.ElementType == InputElementType.Unexpected))
                    {
                        _interface.WriterCollection.Error.WriteLine($"Unexpected element :: [{elem.StartPosition}] {elem.Raw}");
                    }

                    return((int)ExecutionReturnCode.UnexpectedElements);
                }

                // resolve command

                var resCmd = _resolver.Resolve(input);

                if (!resCmd.IsValid)
                {
                    foreach (var err in resCmd.Errors)
                    {
                        switch (err.Type)
                        {
                        case CommandResolutionErrorType.CommandNotFound:
                            _interface.WriterCollection.Error.WriteLine($"Command, \"{input.GetCommandElement().Value}\" not found");
                            break;

                        case CommandResolutionErrorType.ArgumentNotFound:
                        case CommandResolutionErrorType.UnexpectedValue:
                        case CommandResolutionErrorType.DuplicateArgument:
                            _interface.WriterCollection.Error.WriteLine($"{err.Message} :: [{err.Element.StartPosition}] {err.Element.Raw}");
                            break;

                        default:
                            throw new ArgumentOutOfRangeException($"Value \"{err.Type}\" not defined for switch.");
                        }
                    }
                    return((int)ExecutionReturnCode.CommandResolutionError);
                }

                // activate and execute

                return(await ExecuteCommand(_activator.Activate(resCmd)));
            }
            finally
            {
                IsRunning = false;
                _currentCancellationTokenSource.Dispose();
            }
        }