【问题标题】:Run a batch of CLI commands运行一批 CLI 命令
【发布时间】:2014-03-12 15:52:13
【问题描述】:

编辑 - 这是来自here的副本

我想为现有的 CLI 应用程序创建一个 gui 包装器(在 C# 中)(我只有这个 CLI 应用程序的二进制文件)。为此,我需要运行 CLI 应用程序,然后解析其输出,提供更多命令(取决于输出)等等。

我知道我可以使用 System.Diagnostics.Process.Start(...) 来运行单个命令。但是有什么方法可以与该线程进行通信吗?或者有没有其他方法可以在 C# 中做到这一点?

【问题讨论】:

  • 重定向标准输入/输出,例如stackoverflow.com/questions/21270842/…
  • 听起来你需要附加到进程的 IO 流。不知道这是否有助于你在谷歌上搜索。寻找类似stdin stdout system.diagnostics.process.start c#
  • 这将使我(希望)能够访问输出。但是之后如何向流程发送新的输入?写入流?
  • 是的,写入输入流

标签: c# windows shell command-line-interface


【解决方案1】:

我建议将CliWrap 用于这些目的。它支持运行进程、参数格式化、取消、管道和许多其他事情。比直接使用System.Diagnostics.Process 流畅得多。

一些例子:

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2023-03-28
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    相关资源
    最近更新 更多