对于每个命令,我都有一个实现某个接口的具体类。 例如:
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);
答案 0 :(得分:2)
以下代码段可能是您要找的内容的起点。
@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
编辑显示命令本身的说明。在这种情况下,可以省略类@Parameters(commandDescription = "foo-command short description")
上的注释FooCommand
。
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