是否有有助于构建命令行应用程序的优秀 Java 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/435740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:34:39  来源:igfitidea点击:

Are there good Java libraries that facilitate building command-line applications?

javashellcommand-lineinteractivepicocli

提问by carrier

I need to write a simple command-line application in Java. It would be nice to use a library that takes care of parsing commands and takes care of things like flags and optional/mandatory parameters...

我需要用 Java 编写一个简单的命令行应用程序。使用一个负责解析命令并处理标志和可选/强制参数之类的库会很好......

UPDATE

更新

Something that has built-in TAB completion would be particularly great.

具有内置 TAB 完成功能的东西会特别好。

采纳答案by Kevin ORourke

I've used the Apache Commons CLI libraryfor command-line argument parsing. It's fairly easy to use and has reasonably good documentation.

我使用Apache Commons CLI 库进行命令行参数解析。它相当容易使用并且有相当好的文档

Which library you choose probably comes down to which style of options you prefer ("--gnu-style" or "-javac-style").

您选择哪个库可能取决于您喜欢哪种风格的选项(“--gnu-style”或“-javac-style”)。

回答by Pedro Henriques

Have a look at arg4j.

看看arg4j

回答by Michael Myers

JLinelooks helpful.

JLine看起来很有帮助。

JLine is a Java library for handling console input. It is similar in functionality to BSD editline and GNU readline. People familiar with the readline/editline capabilities for modern shells (such as bash and tcsh) will find most of the command editing features of JLine to be familiar.

JLine 是一个用于处理控制台输入的 Java 库。它在功能上类似于 BSD editline 和 GNU readline。熟悉现代 shell(例如 bash 和 tcsh)的 readline/editline 功能的人会发现 JLine 的大多数命令编辑功能都很熟悉。

And it does list tab completion in its features

它确实在其功能中列出了选项卡完成。 

回答by Julien Hoarau

In our company we are using JewelClithat uses an annotated Java interface definition to describe the Command Line Interface in a declarative style.

在我们公司中,我们使用JewelCli,它使用带注释的 Java 接口定义以声明式的方式描述命令行接口。

Light and simple to use, I'll recommend it.

轻便且易于使用,我会推荐它。

回答by eneset

There is a Java port of GNU getopt libraryfor command line parsing. For command editing and TAB completion there is JLine (as mmyers has already recommended).

有一个用于命令行解析的 GNU getopt 库Java 端口。对于命令编辑和 TAB 完成,有 JLine(正如 mmyers 已经推荐的那样)。

回答by Brett Daniel

I have used the Java Simple Argument Parserextensively. It handles all sorts of option types, has a well-designed and library (including the option to write custom parsers), and provides good documentation both in the JavaDocs and online.

我广泛使用了Java Simple Argument Parser。它处理各种选项类型,具有精心设计的库(包括编写自定义解析器的选项),并在 JavaDocs 和online 中提供良好的文档。

I think your desire for tab completion would be taken care of by the shell rather than the command-line parsing library.

我认为您对制表符完成的渴望将由 shell 而不是命令行解析库来处理。

回答by Parag

I have used JSAP. I have found it to be stable and well documented.

我用过JSAP。我发现它很稳定并且有据可查。

I am not sure I understand what you mean by "built-in TAB completion". Can you elaborate.

我不确定我是否理解“内置 TAB 完成”的意思。能不能详细点。

回答by Ivar

It's also worth looking at --jopt-simple.

也值得一看--jopt-simple

It 'attempts to honor the command line option syntaxes of POSIX getopt() and GNU getopt_long().' It seems to have some community traction, notably being the command line parsing lib of choice for the OpenJDK.

它“尝试遵守 POSIX getopt() 和 GNU getopt_long() 的命令行选项语法。” 它似乎有一些社区吸引力,特别是作为OpenJDK的首选命令行解析库。

Also, as Brett mentioned, tab completion is typically provided by your shell - you would write shell functions to provide that desired functionality.

此外,正如 Brett 所提到的,制表符补全通常由您的 shell 提供 - 您可以编写 shell 函数来提供所需的功能。

p.s. Sorry for the duplicate answer, but this question is almost identical to Java library for parsing command-line parameters?

ps抱歉重复的答案,但这个问题几乎与解析命令行参数的Java库相同

回答by Stefan Asseg

As you're searching for a library that eases developing an interactive shell application, I'd suggest Cliche.

当您正在寻找可简化交互式 shell 应用程序开发的库时,我建议您使用Cliche

回答by CrouZ

I tried a few libraries and ended up using argparse4j. It's a really nice Java port of Python's argparse.

我尝试了一些库并最终使用了argparse4j。这是 Python 的argparse的一个非常好的 Java 端口。