Skip to content

Commit a644c0b

Browse files
committed
Add extra blank lines before options, commands and between commands
1 parent 24d8421 commit a644c0b

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/main/java/com/beust/jcommander/DefaultUsageFormatter.java

+8
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public void appendMainLine(StringBuilder out, boolean hasOptions, boolean hasCom
169169
public void appendAllParametersDetails(StringBuilder out, int indentCount, String indent,
170170
List<ParameterDescription> sortedParameters) {
171171
if (sortedParameters.size() > 0) {
172+
out.append("\n");
172173
out.append(indent).append(" Options:\n");
173174
}
174175

@@ -269,14 +270,21 @@ public void appendCommands(StringBuilder out, int indentCount, int descriptionIn
269270
if (hasOnlyHiddenCommands)
270271
return;
271272

273+
out.append("\n");
272274
out.append(indent + " Commands:\n");
273275

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

279282
if (p == null || !p.hidden()) {
283+
if (!firstCommand) {
284+
out.append("\n");
285+
} else {
286+
firstCommand = false;
287+
}
280288
JCommander.ProgramName progName = commands.getKey();
281289
String dispName = progName.getDisplayName();
282290
String commandDescription = Optional.ofNullable(getCommandDescription(progName.getName()))

src/test/java/com/beust/jcommander/DefaultUsageFormatterTest.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,24 @@ class TwoCommand {
5353
jc.setConsole(new OutputForwardingConsole(output));
5454
jc.usage();
5555
String expected = "Usage: <main class> [options] [command] [command options]\n"
56+
+ "\n"
5657
+ " Options:\n"
5758
+ " -a, --a, --a-parameter\n"
5859
+ " a parameter\n"
5960
+ " Default: 0\n"
61+
+ "\n"
6062
+ " Commands:\n"
6163
+ " one one command\n"
6264
+ " Usage: one [options]\n"
65+
+ "\n"
6366
+ " Options:\n"
6467
+ " -b, --b, --b-parameter\n"
6568
+ " b parameter\n"
6669
+ " Default: 0\n"
70+
+ "\n"
6771
+ " two two command\n"
6872
+ " Usage: two [options]\n"
73+
+ "\n"
6974
+ " Options:\n"
7075
+ " -c, --c, --c-parameter\n"
7176
+ " c parameter\n"
@@ -109,6 +114,7 @@ class ArgsTemplate {
109114

110115
// verify
111116
String expected = "Usage: <main class> [options]\n"
117+
+ "\n"
112118
+ " Options:\n"
113119
+ " --a, -a\n"
114120
+ " Default: 0\n"
@@ -358,7 +364,7 @@ class ArgCommandB {
358364

359365
StringBuilder sb = new StringBuilder();
360366
c.getUsageFormatter().usage(sb);
361-
Assert.assertTrue(sb.toString().contains("[command options]\n Commands:"));
367+
Assert.assertTrue(sb.toString().contains("[command options]\n\n Commands:"));
362368
}
363369

364370
@Test
@@ -387,9 +393,11 @@ class ArgCommandB {
387393
StringBuilder sb = new StringBuilder();
388394
c.getUsageFormatter().usage(sb);
389395
String expected = "Usage: <main class> [command] [command options]\n"
396+
+ "\n"
390397
+ " Commands:\n"
391398
+ " a command a\n"
392399
+ " Usage: a command a parameters\n"
400+
+ "\n"
393401
+ " b command b\n"
394402
+ " Usage: b command b parameters\n";
395403
Assert.assertEquals(sb.toString(), expected);
@@ -424,7 +432,7 @@ class ArgCommandB {
424432

425433
StringBuilder sb = new StringBuilder();
426434
c.getUsageFormatter().usage(sb);
427-
Assert.assertTrue(sb.toString().contains("command a parameters\n Commands:"));
435+
Assert.assertTrue(sb.toString().contains("command a parameters\n\n Commands:"));
428436
Assert.assertTrue(sb.toString().contains("command b\n Usage:"));
429437
}
430438

src/test/java/com/beust/jcommander/UnixStyleUsageFormatterTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class ArgCommandB {
302302

303303
StringBuilder sb = new StringBuilder();
304304
jc.getUsageFormatter().usage(sb);
305-
Assert.assertTrue(sb.toString().contains("[command options]\n Commands:"));
305+
Assert.assertTrue(sb.toString().contains("[command options]\n\n Commands:"));
306306
}
307307

308308
@Test
@@ -332,9 +332,11 @@ class ArgCommandB {
332332
StringBuilder sb = new StringBuilder();
333333
jc.getUsageFormatter().usage(sb);
334334
String expected = "Usage: <main class> [command] [command options]\n"
335+
+ "\n"
335336
+ " Commands:\n"
336337
+ " a command a\n"
337338
+ " Usage: a command a parameters\n"
339+
+ "\n"
338340
+ " b command b\n"
339341
+ " Usage: b command b parameters\n";
340342
Assert.assertEquals(sb.toString(), expected);
@@ -370,7 +372,7 @@ class ArgCommandB {
370372

371373
StringBuilder sb = new StringBuilder();
372374
jc.getUsageFormatter().usage(sb);
373-
Assert.assertTrue(sb.toString().contains("command a parameters\n Commands:"));
375+
Assert.assertTrue(sb.toString().contains("command a parameters\n\n Commands:"));
374376
Assert.assertTrue(sb.toString().contains("command b\n Usage:"));
375377
}
376378

0 commit comments

Comments
 (0)