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

How to set description for command, but not for option

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

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 命令、boo 命令、lala 命令。但是,所有这些命令都是独立的,不在一个主命令中(换句话说,这不像 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

编辑 显示命令本身的描述。在这种情况下,可以省略 class 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