What does Util.Cmd( ..., StreamResults: true) do?
I have a script which calls an external program that takes a while and writes quite a few lines to console and I thought that this option would start displaying the output at once rather than waiting until the program had finished.
Later on when I was had more throughly debugged the script I change it not to dump the output from the cmd.
But this seems to have the effect of not actually running the command!.
System.IO.File.Delete("testfile"); var output = Util.Cmd("echo hello > testfile", streamResults: true); System.IO.File.Exists("testfile").Dump(); output.Dump(); System.IO.File.Exists("testfile").Dump();
False (0 items) True
showing that the testfile is not created until after the output is dumped (or until I called something like output.ToArray())
Can anyone explain?
That's correct behavior. If streamResults is true, you get back a lazy IEnumerable with standard streaming/iterator/pipe semantics. This means that you can access results before the command has completed - and is potentially more efficient in that the results are not stored in memory - however you have to start enumerating the results in order to start the operation (also, the command may block if you don't enumerate the results fast enough).
I think I have been exceedingly dense. I sat looking in ILSpy at the code
and thought, it doesn't actually pass streamResults to CmdCore, so how can it stop the process from starting?
Completely overlooked the return value which makes all the difference!