用另一种语言包装命令行工具的模式

发布于 2024-08-27 12:27:38 字数 139 浏览 4 评论 0原文

我目前正在编写一些 Java 来封装一个广泛的命令行工具。感觉我正在写很多类似的代码。

我想知道是否有任何已建立的模式用于包装命令行工具 - 传递参数和处理输出等。

Java 中的具体示例显然会很棒,但也欢迎任何一般性建议或指示。

I'm currently writing some Java to wrap around an extensive command line tool. It feels like I'm writing a lot of similar code.

I'm wondering if there are any established patterns for wrapping command line tools - passing arguments and handling output and so on.

Specific examples in Java would obviously be great, but any general suggestions or pointers are welcome too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-09-03 12:27:38

Apache Commons Exec 可能对您有一些帮助。这是一个小库,旨在简化 Java 中外部进程的执行。

如果您需要广泛的支持,还有 Apache Commons CLI 用于处理命令行解析。

Apache Commons Exec might be of some help to you. This is a little library aimed at simplyfying executing of external processes in Java.

There is also Apache Commons CLI for dealing with command line parsing if you need an extensive support for one.

黑色毁心梦 2024-09-03 12:27:38

如果您主要包装命令行工具,那么您可能需要考虑使用 groovy 来代替 Java 或作为 Java 的补充。 在 groovy 中执行命令很简单作为:

def p = '我的命令'.execute()

print p.text //打印输出

您还可以将这些方法链接在一起,如下所示:

打印'我的命令'.execute().text

If you are primarily wrapping a command line tool, then you might want to consider using groovy instead of or in addition to Java. Executing a command in groovy is a simple as:

def p = 'my command'.execute()

print p.text //prints output

You could also chain these methods together like this:

print 'my command'.execute().text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文