cbeust / jcommander

Command line parsing framework for Java
Apache License 2.0
1.96k stars 334 forks source link

Fix usage() for missing description on commands #481

Closed jkosh44 closed 1 year ago

jkosh44 commented 4 years ago

Before this commit, if you supply a command to a JCommander object and you dont supply a description for the command, then when calling usage() you will see a line of the following format: "\<command-name> null"

After this commit the string literal "null" will be omitted from the usage() call if you do not supply a description for the command.

Fixes #480

mkarg commented 1 year ago

Thank you for your contribution. Can you please check if this bug still exists in JCommander 1.83?

jkosh44 commented 1 year ago

Thank you for your contribution. Can you please check if this bug still exists in JCommander 1.83?

Yes it still exists in v1.82. You can repro it with the attached project, which is mostly identical to the repro in #480.

jcommander-repro.zip

EDIT: Sorry, I see you've asked about 1.83 not 1.82. I don't see that version on maven and I no longer have this project set up locally. Unfortunately, I don't plan on checking if this still exists on a local version. Hopefully someone else can take over.

jkosh44 commented 1 year ago

I couldn't help myself. This issue still exists on the master branch of this repo. You can repro it with the following diff:

diff --git a/src/test/java/test/DescriptionMainTest.java b/src/test/java/test/DescriptionMainTest.java
new file mode 100644
index 0000000..a2b2616
--- /dev/null
+++ b/src/test/java/test/DescriptionMainTest.java
@@ -0,0 +1,21 @@
+package test;
+
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+import org.testng.annotations.Test;
+
+
+public class DescriptionMainTest {
+  @Parameter(names = "-t")
+  private String test;
+
+  @Test
+  public void testMain() {
+    JCommander jc = JCommander.newBuilder().addCommand("testCommand", new DescriptionMainTest()).build();
+    jc.usage();
+  }
+  
+  public static void main(String[] args) {
+    new DescriptionMainTest().testMain();
+  }
+}

The output is

Usage: <main class> [command] [command options]
  Commands:
    testCommand      null
      Usage: testCommand [options]
        Options:
          -t