Tyrrrz/CliFx

`FakeInMemoryConsole.Clear()` has no effect

markopolo89 opened this issue · 4 comments

Version

2.2.6

Details

Sorry if this is not the correct place for this post - I'm not sure if this is a bug.
I’ve been using your excellent framework to process command line style messages transmitted across a websocket interface. In this scenario, rather than view command exceptions and other messages on the console output it would be convenient to intercept these and send back to the client. For this purpose, I have tried replacing the console window with a FakeInMemoryConsole. This works well, however I have noticed that the clear command does not work. It would be great to be able to check the fake console for reported messages and send them to the client, then clear for next time.

Also, I realise that FakeInMemoryConsole is intended primarily for test purposes but this seems to be the best way to meet my requirement. I was wondering if there is another way to capture the console output?

Steps to reproduce

  • Provide FakeInMemoryConsole object when building the application.
  • Call command incorrectly so an exception is produced.
  • Intercept the exception message.
  • Call the clear method on the FakeInMemoryConsole object.
  • FakeInMemoryConsole object does not clear.

Hi.

Indeed, the Clear() method doesn't do anything in FakeConsole. It may make sense to make it virtual and override in FakeInMemoryConsole to clear the in-memory streams.

That said, I'm not sure if it's the best solution for your problem. Can you give some example of what you're trying to achieve? I think, in your situation, I would not write to the console at all, but instead use some sort of logging abstraction and configure it to send data to your websocket interface.

Thanks for your quick response. Yes, I agree that this is not the best solution.

If, for example, a command argument is incorrect and I would like to raise a command exception, I would like to be able to intercept this at a high level and send the error message to the client across the web-socket interface.

Even in a scenario, where the execute function is not being called because a parameter is missing, again I would like to intercept the "Missing parameter" message and transmit back to the client. I think this would be useful.

In general, what I am trying to achieve is to have the benefits of the CliFx parsing and error reporting functionality but to have access to the meaningful messages that get output to the console window and in the event of an error transmit them across to the client using the websocket interface.

I see what you mean. This is kind of an unusual use case, I would say. Maybe for your case it would make sense to create a custom IConsole that uses a custom Stream that sends data off to the websocket interface you have? And then some sort of AggregateConsole that combines it together with the existing IConsole instance you get from CliFx. You would need to provide your own ConsoleWriter, but it should be fairly simple by replicating this internal method:

internal static ConsoleWriter Create(IConsole console, Stream stream) => new(
console,
Stream.Synchronized(stream)
) {AutoFlush = true};

Thanks for the suggestion. I'll investigate this further. As an interim measure I have replicated the FakeInMemoryConsole and added a clear method to clear the memory streams. This gets me moving for my current task.