为什么 picocli 无法从命令行识别我的选项?
Why doesn't picocli recognize my option from the command line?
我正在尝试使用 PICOCLI 在 Java 中构建 CLI,但我卡在了一个非常基本的点上。我根本无法让我的应用程序为消费者提供一个选择,它是有价值的。这是我的 class:
package com.example.demo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import picocli.CommandLine;
@SpringBootApplication
@CommandLine.Command(name = "Greet", header = "%n@|green Hello world demo|@")
class DemoApplication implements Runnable {
@CommandLine.Option(names = {"-u", "--user"}, required = true, description = "The user name.")
String userName;
public void run() {
System.out.println("Hello, " + userName);
}
public static void main(String... args) {
CommandLine.run(new DemoApplication(), System.err, args);
}
}
然后我做了 mvn package
、cd target
和 java -jar demo-1.0.jar Greet -u pico
,但我只遇到了这个:
Unmatched argument at index 0: 'Greet'
Hello world demo
Usage: Greet -u=<userName>
-u, --user=<userName> The user name.
我 运行 没有耐心尝试打印一条简单的消息!我不知道还有什么办法可以解决这个问题。请帮忙!
如果用java
调用命令,不需要指定命令名Greet
,只需指定命令行选项:
java -jar demo-1.0.jar -u pico
你可以这样想:java -jar demo-1.0.jar
是 Greet
命令。
您可能希望使用 Application Assembler Maven Plugin 创建一个启动器脚本,并将该启动器脚本命名为 Greet
。
这样,您的程序的用户就可以使用以下命令在命令行上调用它:
Greet -u pico
AppAssembler 的 Maven 配置应如下所示:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<programs>
<program>
<mainClass>com.example.demo.DemoApplication</mainClass>
<id>Greet</id>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
</project>
更新:我在 how to run picocli-based applications 上的用户手册中添加了一个部分。
更新 2:我建议升级到最新版本的 picocli; 4.0 版引入了新的 execute API,更好地支持退出代码和错误处理。
在您的示例中,它看起来像这样:
public static void main(String... args) {
int exitCode = new CommandLine(new DemoApplication()).execute(args);
System.exit(exitCode);
}
我正在尝试使用 PICOCLI 在 Java 中构建 CLI,但我卡在了一个非常基本的点上。我根本无法让我的应用程序为消费者提供一个选择,它是有价值的。这是我的 class:
package com.example.demo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import picocli.CommandLine;
@SpringBootApplication
@CommandLine.Command(name = "Greet", header = "%n@|green Hello world demo|@")
class DemoApplication implements Runnable {
@CommandLine.Option(names = {"-u", "--user"}, required = true, description = "The user name.")
String userName;
public void run() {
System.out.println("Hello, " + userName);
}
public static void main(String... args) {
CommandLine.run(new DemoApplication(), System.err, args);
}
}
然后我做了 mvn package
、cd target
和 java -jar demo-1.0.jar Greet -u pico
,但我只遇到了这个:
Unmatched argument at index 0: 'Greet'
Hello world demo
Usage: Greet -u=<userName>
-u, --user=<userName> The user name.
我 运行 没有耐心尝试打印一条简单的消息!我不知道还有什么办法可以解决这个问题。请帮忙!
如果用java
调用命令,不需要指定命令名Greet
,只需指定命令行选项:
java -jar demo-1.0.jar -u pico
你可以这样想:java -jar demo-1.0.jar
是 Greet
命令。
您可能希望使用 Application Assembler Maven Plugin 创建一个启动器脚本,并将该启动器脚本命名为 Greet
。
这样,您的程序的用户就可以使用以下命令在命令行上调用它:
Greet -u pico
AppAssembler 的 Maven 配置应如下所示:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<programs>
<program>
<mainClass>com.example.demo.DemoApplication</mainClass>
<id>Greet</id>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
</project>
更新:我在 how to run picocli-based applications 上的用户手册中添加了一个部分。
更新 2:我建议升级到最新版本的 picocli; 4.0 版引入了新的 execute API,更好地支持退出代码和错误处理。
在您的示例中,它看起来像这样:
public static void main(String... args) {
int exitCode = new CommandLine(new DemoApplication()).execute(args);
System.exit(exitCode);
}