简体   繁体   中英

Run a batch of CLI commands

edit - this is a duplicate from here

I would like to create a gui wrapper (in C#) to an existing CLI application (I only have the binaries of this CLI application). in order to do that I need to run the CLI application, and then parse its output, give more commands (witch depends on the output) and so on.

I know that I can use System.Diagnostics.Process.Start(...) to run a single command. but is there any way to communicate with that thread? or is there any other way to do this in C#?

I suggest using CliWrap for these purposes. It supports running processes, argument formatting, cancellation, piping, and a lot of other things. Much smoother than working with System.Diagnostics.Process directly.

Some examples:

using CliWrap;
using CliWrap.Buffered;

var result = await Cli.Wrap("path/to/exe")
    .WithArguments("--foo bar")
    .ExecuteBufferedAsync();

// Result contains:
// -- result.StandardOutput  (string)
// -- result.StandardError   (string)
// -- result.ExitCode        (int)
// -- result.StartTime       (DateTimeOffset)
// -- result.ExitTime        (DateTimeOffset)
// -- result.RunTime         (TimeSpan)
var cmd = "Hello world" | Cli.Wrap("foo")
    .WithArguments("print random") | Cli.Wrap("bar")
    .WithArguments("reverse") | (Console.WriteLine, Console.Error.WriteLine);

await cmd.ExecuteAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM