public void Execute(string input) { _inputHistory.Add(input); /*TODO: Add method to get previous input history * Examine Powershell commandlet implementation. * should execute just fire a commandlet and then send a callback to the caller (UnityPowerConsole) when the command is done? * TODO: Implement first draft of a Command Manager. Load commands from assembly/namespace, allow execution of commands * TODO: Make a variable context instead of using a dictionary directly * TODO: Handle reflection */ try { var parseResult = PowerParser.ParseInput(input); _lastValue = HandleParseType(parseResult); //_history.Add(parseResult); //_host.Write(parseResult.ParsedType.ToString()); _host.Write(_lastValue?.ToString() ?? _host.FormatColor("NULL", OutputColorType.Accented)); //TODO: Use output formatters - don't output null - let handlers output null explicit if needed } catch (PowerParser.IncompleteParseException exception) { _host.WriteError( $"{exception.Message}\n{_host.FormatColor($"Remainder: {exception.Input.Substring(exception.Position)}", OutputColorType.Default)}"); } catch (Exception exception) { _host.WriteError($"[{exception}]: {exception.Message}"); } }
public void PositionalCommandExecuted() { _context.CommandContext.RegisterCommand <AddNumberCommand>(); var parseResult = PowerParser.ParseInput("Add-Number 2 5.0"); var result = CommandExecuter.Execute(parseResult.Value as pstudio.PowerConsole.Parser.Command, _context, _host); Assert.AreEqual(7.0, result); }
public void NamedCommandExecuted() { _context.CommandContext.RegisterCommand <TestCommand>(); var parseResult = PowerParser.ParseInput("Test-Command \"Hello World\" -SubstringIndex 6"); var result = CommandExecuter.Execute(parseResult.Value as pstudio.PowerConsole.Parser.Command, _context, _host); Assert.AreEqual("World", result); }
public void PipeChainExecuted() { _context.CommandContext.RegisterCommand <AddNumberCommand>(); _context.CommandContext.RegisterCommand <SubtractNumberCommand>(); _context.CommandContext.RegisterCommand <MultiplyNumberCommand>(); _context.CommandContext.RegisterCommand <DivideNumberCommand>(); var parseResult = PowerParser.ParseInput("Add-Number 3 7 | Subtract-Number 5 | Multiply-Number 4 | Divide-Number 40 -FlipArguments"); var result = CommandExecuter.ExecuteChain(parseResult.Value as PipeChain, _context, _host); Assert.AreEqual(2.0, result); }
public void MandatoryCommandExecuted() { _context.CommandContext.RegisterCommand <MandatoryNamedCommand>(); var parseResult = PowerParser.ParseInput("Mandatory-Named -Message 'Goodbye'"); var result = CommandExecuter.Execute(parseResult.Value as pstudio.PowerConsole.Parser.Command, _context, _host); Assert.AreEqual("Hello World", result); parseResult = PowerParser.ParseInput("Mandatory-Named -Message 'Goodbye' -Flag"); result = CommandExecuter.Execute(parseResult.Value as pstudio.PowerConsole.Parser.Command, _context, _host); Assert.AreEqual("Goodbye", result); }
public void PositionalCommandIncorrectPositionalArgumentTypeExecuted() { _context.CommandContext.RegisterCommand <AddNumberCommand>(); var parseResult = PowerParser.ParseInput("Add-Number 2 '2'"); try { var result = CommandExecuter.Execute(parseResult.Value as pstudio.PowerConsole.Parser.Command, _context, _host); Assert.Fail(); } catch (InvalidArgumentTypeException) { } }
public void PositionalCommandMissingArgumentExecuted() { _context.CommandContext.RegisterCommand <AddNumberCommand>(); var parseResult = PowerParser.ParseInput("Add-Number 2 "); try { var result = CommandExecuter.Execute(parseResult.Value as pstudio.PowerConsole.Parser.Command, _context, _host); Assert.Fail(); } catch (MissingMandatoryParameterException) { } }
public void PipeChainExcessArgumentExecuted() { _context.CommandContext.RegisterCommand <AddNumberCommand>(); _context.CommandContext.RegisterCommand <SubtractNumberCommand>(); _context.CommandContext.RegisterCommand <MultiplyNumberCommand>(); _context.CommandContext.RegisterCommand <DivideNumberCommand>(); var parseResult = PowerParser.ParseInput("Add-Number 3 7 | Subtract-Number 5 5"); try { CommandExecuter.ExecuteChain(parseResult.Value as PipeChain, _context, _host); Assert.Fail(); } catch (UnexpectedPositionalArgument) { } }