0

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

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);
4

1 回答 1

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 命令语法

编辑显示命令本身的描述。在这种情况下,可以省略@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
于 2017-04-13T12:19:16.963 回答