Tyrrrz / CliWrap

Library for running command-line processes
MIT License
4.36k stars 268 forks source link

Add a method to construct `PipeSource` from delegate #144

Closed 0xced closed 2 years ago

0xced commented 2 years ago

A real-world usage would be piping the construction of a DotGraph object to the dot command line tool like this:

Cli.Wrap("dot").WithStandardInputPipe(PipeSource.FromDelegate(writer => graph.Build(writer)));

βœŠπŸ‡ΊπŸ‡¦

Tyrrrz commented 2 years ago

Thanks for the PR.

Is this more or less equivalent to using half-duplex stream as input while filling it on a separate thread?

0xced commented 2 years ago

I think I have addressed all your points in 1fd00971f3d7d23bed88f523298f364bab172152. And the code is indeed much better like this. πŸ˜‰

0xced commented 2 years ago

I have applied your suggested renaming in 8e561fb78960149e825e90d6443045b77d5b5746 and it looks indeed better!

As for the TextWriter pipe sources, I have kept them because I really like to be able to create a one-liner pipe source instead of a two-liner. Also, I was impressed by the compiler who perfectly understands if you want to use PipeSource.Create(async (stream, _) => …) vs PipeSource.Create(async (writer, _) => …) without having to explicitly specify the Stream or TextWriter type.

Anyway, I don't really mind and if you think it's too much for CliWrap, please go ahead and remove them, I won't be offended. πŸ˜‰

Tyrrrz commented 2 years ago

It should still be a one-liner in your case (the disposal is already performed upstream):

Cli.Wrap("dot").WithStandardInputPipe(PipeSource.Create(stream => graph.Build(new TextWriter(stream))));

I just don't want to deal with buffer size and other options unless I absolutely need to.

Tyrrrz commented 2 years ago

Thanks, I'll take it from here :)

0xced commented 2 years ago

Thanks, I'll take it from here :)

Awesome, thanks! πŸ‘

0xced commented 2 years ago

Thank you for releasing a new version so fast. You can see it in action in my RidGraph micro project.

https://github.com/0xced/RidGraph/blob/a8809ab79200265b0946173c3e8dfe0077c640a8/src/Program.cs#L30-L38

what-the-diff[bot] commented 1 year ago