如何在没有选择的情况下在Groovy中使用Clibuilder?

发布于 2025-02-06 13:12:59 字数 523 浏览 1 评论 0原文

我正在编写一个简单的groovy脚本,我希望命令行很简单,此外

./someTask.groovy task-profile

,该脚本应该具有- help-h选项,该选项指定所有指定所有不同的值可以接受任务范围



task-profile可以采用以下值 -

  • task-1
  • task-2
  • task-3

- help选项应告诉用户所有可用的任务范围值以及如何使用它们。

很多

我搜索了 没有选项的脚本,但只有位置参数,我可以使用交换语句进行编码,但是我想学习使用clibuilder。任何帮助将不胜感激。

I am writing a simple groovy script for which I want a command line as simple as-

./someTask.groovy task-profile

Also, the script should have a --help or -h option which specifies all the different values task-profile can take.

task-profile can take the following values-

  • task-1
  • task-2
  • task-3

And the --help option should tell the users about all available task-profile values and also how to use them.

I searched a lot but only found examples that had options (eg -a, -b, -c, etc.)

How to write a script that has no option, but only positional parameters, I can hardcode it using switch statements, but I want to learn to use CliBuilder. Any help will be appreciated.

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

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

发布评论

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

评论(1

最好是你 2025-02-13 13:12:59

Clibuilder在Groovy 2.5中更新了 picocli -backed-backed实现(groovy.cli.cli.picocli。 Clibuilder)。 本文显示了许多新功能的详细信息,这些详细信息可在Clibuilder中可用。

但是,即使在此版本的Clibuilder中,公共API也仅支持位置参数列表,并为该列表提供单个描述。

@Unparsed(description = 'positional parameters')
List positionals

不幸的是,Clibuilder API当前没有提供一种在您的应用程序的使用帮助消息中显示单独的位置参数(通过单个描述)的方法。

如果您的目标是具有单独的位置参数,则可能具有单独的类型,单独的默认值等,并让使用帮助消息显示每个位置参数的分开描述,那么您可能需要考虑直接使用Picocli(无粘液器)您的时髦脚本或在您的Groovy应用程序中。

Picocli用户手册具有许多令人讨厌的示例,并在使用picocli在Groovy应用程序中使用picocli,在Groovy应用程序和杂乱无章的剧本中。本文(“ nofollow noreferrer”> groovy脚本)也可能有用。 。

这是一个示例任务构装带有三个位置参数的脚本脚本:

// task-profile.groovy
@Grab('info.picocli:picocli-groovy:4.6.3')
@GrabConfig(systemClassLoader=true)
@Command(name = "task-profile", version = "task-profile 1.0",
        mixinStandardHelpOptions = true, // add --help and --version options
        description = "Task Profile")
@picocli.groovy.PicocliScript2
import groovy.transform.Field
import static picocli.CommandLine.*

@Parameters(index = "0", description = "The first task")
@Field String task1;

@Parameters(index = "1", description = "The second task")
@Field String task2;

@Parameters(index = "2", description = "The third task")
@Field String task3;

// PicocliBaseScript2 prints usage help or version if requested by the user

println "hi, the selected tasks are $task1, $task2 and $task3"

CliBuilder was updated in Groovy 2.5 by adding support for a picocli-backed implementation (groovy.cli.picocli.CliBuilder). This article shows many details of the new features that are now available in CliBuilder.

However, even in this version of CliBuilder, the public API only supports a list of positional parameters, with a single description for that list.

@Unparsed(description = 'positional parameters')
List positionals

Unfortunately, the CliBuilder API currently does not provide a way to show separate positional parameters, with individual descriptions, in the usage help message of your application.

If your goal is to have separate positional parameters, perhaps with separate types, separate default values, etc., and let the usage help message show separate descriptions for each positional parameter, then you may want to consider using picocli directly (without CliBuilder) in your Groovy script or in your Groovy application.

The picocli user manual has many Groovy examples and has a dedicated section on using picocli in Groovy applications and Groovy scripts. This article (Groovy Scripts on Steroids) may also be useful.

Here is an example task-profile Groovy script with three positional parameters:

// task-profile.groovy
@Grab('info.picocli:picocli-groovy:4.6.3')
@GrabConfig(systemClassLoader=true)
@Command(name = "task-profile", version = "task-profile 1.0",
        mixinStandardHelpOptions = true, // add --help and --version options
        description = "Task Profile")
@picocli.groovy.PicocliScript2
import groovy.transform.Field
import static picocli.CommandLine.*

@Parameters(index = "0", description = "The first task")
@Field String task1;

@Parameters(index = "1", description = "The second task")
@Field String task2;

@Parameters(index = "2", description = "The third task")
@Field String task3;

// PicocliBaseScript2 prints usage help or version if requested by the user

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