Skip to content

Commit

Permalink
Remove extra lines between commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hogejo committed Nov 29, 2024
1 parent 67e4133 commit 24d8421
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,13 @@ public void appendCommands(StringBuilder out, int indentCount, int descriptionIn
return;

out.append(indent + " Commands:\n");
boolean firstCommand = true;

// The magic value 3 is the number of spaces between the name of the option and its description
for (Map.Entry<JCommander.ProgramName, JCommander> commands : commander.getRawCommands().entrySet()) {
Object arg = commands.getValue().getObjects().get(0);
Parameters p = arg.getClass().getAnnotation(Parameters.class);

if (p == null || !p.hidden()) {
if (!firstCommand) {
out.append("\n");
} else {
firstCommand = false;
}
JCommander.ProgramName progName = commands.getKey();
String dispName = progName.getDisplayName();
String commandDescription = Optional.ofNullable(getCommandDescription(progName.getName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ class OneCommand {
@Parameter(names = {"-b", "--b", "--b-parameter"}, description = "b parameter")
public int b;
}
@Parameters(commandNames = "two", commandDescription = "two command")
class TwoCommand {
@Parameter(names = {"-c", "--c", "--c-parameter"}, description = "c parameter")
public int c;
}
JCommander jc = JCommander.newBuilder()
.addObject(new MainParameters())
.addCommand(new OneCommand())
.addCommand(new TwoCommand())
.build();
StringBuilder output = new StringBuilder();
jc.setConsole(new OutputForwardingConsole(output));
Expand All @@ -57,6 +63,12 @@ class OneCommand {
+ " Options:\n"
+ " -b, --b, --b-parameter\n"
+ " b parameter\n"
+ " Default: 0\n"
+ " two two command\n"
+ " Usage: two [options]\n"
+ " Options:\n"
+ " -c, --c, --c-parameter\n"
+ " c parameter\n"
+ " Default: 0\n";
Assert.assertEquals(output.toString(), expected);
}
Expand Down Expand Up @@ -378,7 +390,6 @@ class ArgCommandB {
+ " Commands:\n"
+ " a command a\n"
+ " Usage: a command a parameters\n"
+ "\n"
+ " b command b\n"
+ " Usage: b command b parameters\n";
Assert.assertEquals(sb.toString(), expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ class ArgCommandB {
+ " Commands:\n"
+ " a command a\n"
+ " Usage: a command a parameters\n"
+ "\n"
+ " b command b\n"
+ " Usage: b command b parameters\n";
Assert.assertEquals(sb.toString(), expected);
Expand Down

0 comments on commit 24d8421

Please sign in to comment.