0

For every command I have a concrete class which implement certain interface. For example:

public class FooCommand implements Command{

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

And this is the usage message I get:

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

How can I add description to command (but not to option). For example I want to get such usage message:

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

EDIT I have foo-command, boo-command, lala-command. However, all these commands are separate and are not within one main command (by other words this is not like git clone ...). This is the way I get usage

 JCommander jCommander=new JCommander(command, args);
 jCommander.setProgramName(commandName);//for example foo-command
 StringBuilder builder=new StringBuilder();
 jCommander.usage(builder);
Pavel_K
  • 10,748
  • 13
  • 73
  • 186

1 Answers1

2

Following snippet might be a starting point for what you are looking for.

@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();
    }
}

output

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

Also have a look at: JCommander command syntax

edit Show the description for a command itself. The annotation @Parameters(commandDescription = "foo-command short description") on the class FooCommand can be omitted in that case.

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

output

Usage: foo-command [options] This command is used as base foo
  Options:
    -?, --help
       display this help
       Default: false
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Thank you for your answer. But I don't need CommandMain. I have different commands without "Main" – Pavel_K Apr 13 '17 at 12:35
  • @Pavel Command main is there to provide a [MCVE](http://stackoverflow.com/help/mcve). Maybe you should put in that case a better example of your code into your question. – SubOptimal Apr 13 '17 at 12:49
  • @Pavel I added an amended code snippet. The classes are the same as before. – SubOptimal Apr 13 '17 at 13:20
  • Thank you very much. It seems that `@Parameters(commandDescription = "")` can be omitted. So to get what I want is necessary to use `@Parameter(description = "This command is used as base foo")` - parameter without `names` for any field of class with any type. How can it be explained? – Pavel_K Apr 13 '17 at 14:12
  • @Pavel I updated my answer to show where the `commandDescription` of the class is shown in the help. – SubOptimal Apr 21 '17 at 12:35
  • Thank you very much. I've tested your solution - it works, but it is not `a clean` solution. So I opened an issue. See https://github.com/cbeust/jcommander/issues/352 – Pavel_K Apr 21 '17 at 12:38