Skip to content

Commit

Permalink
Add extra blank lines before options, commands and between commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hogejo committed Dec 1, 2024
1 parent 24d8421 commit a644c0b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/beust/jcommander/DefaultUsageFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public void appendMainLine(StringBuilder out, boolean hasOptions, boolean hasCom
public void appendAllParametersDetails(StringBuilder out, int indentCount, String indent,
List<ParameterDescription> sortedParameters) {
if (sortedParameters.size() > 0) {
out.append("\n");
out.append(indent).append(" Options:\n");
}

Expand Down Expand Up @@ -269,14 +270,21 @@ public void appendCommands(StringBuilder out, int indentCount, int descriptionIn
if (hasOnlyHiddenCommands)
return;

out.append("\n");
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
12 changes: 10 additions & 2 deletions src/test/java/com/beust/jcommander/DefaultUsageFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,24 @@ class TwoCommand {
jc.setConsole(new OutputForwardingConsole(output));
jc.usage();
String expected = "Usage: <main class> [options] [command] [command options]\n"
+ "\n"
+ " Options:\n"
+ " -a, --a, --a-parameter\n"
+ " a parameter\n"
+ " Default: 0\n"
+ "\n"
+ " Commands:\n"
+ " one one command\n"
+ " Usage: one [options]\n"
+ "\n"
+ " Options:\n"
+ " -b, --b, --b-parameter\n"
+ " b parameter\n"
+ " Default: 0\n"
+ "\n"
+ " two two command\n"
+ " Usage: two [options]\n"
+ "\n"
+ " Options:\n"
+ " -c, --c, --c-parameter\n"
+ " c parameter\n"
Expand Down Expand Up @@ -109,6 +114,7 @@ class ArgsTemplate {

// verify
String expected = "Usage: <main class> [options]\n"
+ "\n"
+ " Options:\n"
+ " --a, -a\n"
+ " Default: 0\n"
Expand Down Expand Up @@ -358,7 +364,7 @@ class ArgCommandB {

StringBuilder sb = new StringBuilder();
c.getUsageFormatter().usage(sb);
Assert.assertTrue(sb.toString().contains("[command options]\n Commands:"));
Assert.assertTrue(sb.toString().contains("[command options]\n\n Commands:"));
}

@Test
Expand Down Expand Up @@ -387,9 +393,11 @@ class ArgCommandB {
StringBuilder sb = new StringBuilder();
c.getUsageFormatter().usage(sb);
String expected = "Usage: <main class> [command] [command options]\n"
+ "\n"
+ " 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 Expand Up @@ -424,7 +432,7 @@ class ArgCommandB {

StringBuilder sb = new StringBuilder();
c.getUsageFormatter().usage(sb);
Assert.assertTrue(sb.toString().contains("command a parameters\n Commands:"));
Assert.assertTrue(sb.toString().contains("command a parameters\n\n Commands:"));
Assert.assertTrue(sb.toString().contains("command b\n Usage:"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class ArgCommandB {

StringBuilder sb = new StringBuilder();
jc.getUsageFormatter().usage(sb);
Assert.assertTrue(sb.toString().contains("[command options]\n Commands:"));
Assert.assertTrue(sb.toString().contains("[command options]\n\n Commands:"));
}

@Test
Expand Down Expand Up @@ -332,9 +332,11 @@ class ArgCommandB {
StringBuilder sb = new StringBuilder();
jc.getUsageFormatter().usage(sb);
String expected = "Usage: <main class> [command] [command options]\n"
+ "\n"
+ " 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 Expand Up @@ -370,7 +372,7 @@ class ArgCommandB {

StringBuilder sb = new StringBuilder();
jc.getUsageFormatter().usage(sb);
Assert.assertTrue(sb.toString().contains("command a parameters\n Commands:"));
Assert.assertTrue(sb.toString().contains("command a parameters\n\n Commands:"));
Assert.assertTrue(sb.toString().contains("command b\n Usage:"));
}

Expand Down

0 comments on commit a644c0b

Please sign in to comment.