gpt4 book ai didi

java - 如何为命令设置描述,但不为选项设置描述

转载 作者:行者123 更新时间:2023-12-02 02:54:00 26 4
gpt4 key购买 nike

对于每个命令,我都有一个实现特定接口(interface)的具体类。例如:

public class FooCommand implements Command{

@Parameter(names = {"-?","--help"}, description = "display this help",help = true)
private boolean helpRequested = false;
...
}

这是我收到的使用消息:

Usage: foo-command [options]
Options:
-?, --help
display this help

如何向命令添加描述(但不向选项添加描述)。例如我想得到这样的使用消息:

Usage: foo-command [options] - This command is used as base foo
Options:
-?, --help
display this help

编辑我有 foo-command、boo-command、lala-command。然而,所有这些命令都是独立的,并不在一个主命令中(换句话说,这不像 git clone ...)。这就是我获得使用的方式

 JCommander jCommander=new JCommander(command, args);
jCommander.setProgramName(commandName);//for example foo-command
StringBuilder builder=new StringBuilder();
jCommander.usage(builder);

最佳答案

以下代码片段可能是您正在寻找的内容的起点。

@Parameters(commandDescription = "foo-command short description")
public class FooCommand implements Command {

@Parameter(names = {"-?", "--help"}, description = "display this help",
help = true)
private boolean helpRequested = false;

@Parameter(description = "This command is used as base foo")
public List<String> commandOptions;

// your command code goes below
}


public class CommandMain {

public static void main(String[] args) {
JCommander jc = new JCommander();
jc.setProgramName(CommandMain.class.getSimpleName());
FooCommand foo = new FooCommand();
jc.addCommand("foo-command", foo);
// display the help
jc.usage();
}
}

输出

Usage: CommandMain [options] [command] [command options]
Commands:
foo-command foo-command short description
Usage: foo-command [options] This command is used as base foo
Options:
-?, --help
display this help
Default: false

另请查看:JCommander command syntax

编辑 显示命令本身的描述。在这种情况下,可以省略类 FooCommand 上的注释 @Parameters(commandDescription = "foo-command short description")

Command command = new FooCommand();
JCommander jc = new JCommander(command, args);
jc.setProgramName("foo-command");
StringBuilder builder = new StringBuilder();
jc.usage(builder);
System.out.println(builder);

输出

Usage: foo-command [options] This command is used as base foo
Options:
-?, --help
display this help
Default: false

关于java - 如何为命令设置描述,但不为选项设置描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43389715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com