From ee021bead4d9a72f6f2e6b52222ff4013cbc8b71 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 20 Jan 2015 09:26:46 +0000
Subject: [PATCH] OPENDJ-1734 (CR-5811) V/-version option does not work for some tools

---
 opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java |   95 +++++++++++++++++++++++++++++++----------------
 1 files changed, 62 insertions(+), 33 deletions(-)

diff --git a/opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java b/opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java
index 30759fb..a569cd9 100644
--- a/opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java
+++ b/opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java
@@ -33,7 +33,6 @@
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Comparator;
@@ -48,6 +47,7 @@
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.LocalizableMessageBuilder;
+import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.util.Utils;
 
 /**
@@ -61,6 +61,8 @@
  * on the command-line.
  */
 public class ArgumentParser {
+
+    private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
     /**
      * The name of the OpenDJ configuration direction in the user home
      * directory.
@@ -655,27 +657,17 @@
         return buffer.toString();
     }
 
-    private void writeUsageToOutputStream() {
-        try {
-            getUsage(usageOutputStream);
-        } catch (final Exception e) {
-            // Ignored.
-        }
-    }
-
     /**
-     * Writes usage information based on the defined arguments to the provided
-     * output stream.
+     * Writes message to the usage output stream.
      *
-     * @param outputStream
-     *            The output stream to which the usage information should be
-     *            written.
-     * @throws IOException
-     *             If a problem occurs while attempting to write the usage
-     *             information to the provided output stream.
+     * @param message the message to write
      */
-    void getUsage(final OutputStream outputStream) throws IOException {
-        outputStream.write(getBytes(getUsage()));
+    void writeToUsageOutputStream(CharSequence message) {
+        try {
+            usageOutputStream.write(getBytes(message.toString()));
+        } catch (final Exception e) {
+            logger.traceException(e);
+        }
     }
 
     /**
@@ -777,6 +769,15 @@
     }
 
     /**
+     * Returns the usage argument.
+     *
+     * @return the usageArgument
+     */
+    Argument getUsageArgument() {
+        return usageArgument;
+    }
+
+    /**
      * Retrieves a message containing usage information based on the defined
      * arguments.
      *
@@ -789,6 +790,22 @@
     }
 
     /**
+     * Returns the version handler.
+     *
+     * @return the version handler
+     */
+    VersionHandler getVersionHandler() {
+        return versionHandler;
+    }
+
+    /** Prints the version. */
+    void printVersion() {
+        versionPresent = true;
+        usageOrVersionDisplayed = true;
+        versionHandler.printVersion();
+    }
+
+    /**
      * Returns whether the usage argument was provided or not. This method
      * should be called after a call to parseArguments.
      *
@@ -811,6 +828,16 @@
     }
 
     /**
+     * Indicates whether subcommand names and long argument strings should be treated in a case-sensitive manner.
+     *
+     * @return <CODE>true</CODE> if subcommand names and long argument strings should be treated in a case-sensitive
+     *         manner, or <CODE>false</CODE> if they should not.
+     */
+    boolean longArgumentsCaseSensitive() {
+        return longArgumentsCaseSensitive;
+    }
+
+    /**
      * Parses the provided set of arguments and updates the information
      * associated with this parser accordingly.
      *
@@ -901,14 +928,12 @@
                     if (OPTION_LONG_HELP.equals(argName)) {
                         // "--help" will always be interpreted as requesting
                         // usage information.
-                        writeUsageToOutputStream();
+                        writeToUsageOutputStream(getUsage());
                         return;
                     } else if (versionHandler != null && OPTION_LONG_PRODUCT_VERSION.equals(argName)) {
                         // "--version" will always be interpreted as requesting
                         // version information.
-                        usageOrVersionDisplayed = true;
-                        versionPresent = true;
-                        versionHandler.printVersion();
+                        printVersion();
                         return;
                     } else {
                         // There is no such argument registered.
@@ -921,7 +946,7 @@
                     // If this is the usage argument, then immediately stop and
                     // print usage information.
                     if (usageArgument != null && usageArgument.getName().equals(a.getName())) {
-                        writeUsageToOutputStream();
+                        writeToUsageOutputStream(getUsage());
                         return;
                     }
                 }
@@ -979,16 +1004,11 @@
                 final Argument a = shortIDMap.get(argCharacter);
                 if (a == null) {
                     if (argCharacter == '?') {
-                        writeUsageToOutputStream();
+                        writeToUsageOutputStream(getUsage());
                         return;
                     } else if (versionHandler != null && argCharacter == OPTION_SHORT_PRODUCT_VERSION
                             && !shortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION)) {
-                        // "-V" will always be interpreted as requesting
-                        // version information except if it's already defined
-                        // (e.g in ldap tools).
-                        usageOrVersionDisplayed = true;
-                        versionPresent = true;
-                        versionHandler.printVersion();
+                        printVersion();
                         return;
                     } else {
                         // There is no such argument registered.
@@ -1000,7 +1020,7 @@
                     // If this is the usage argument, then immediately stop and
                     // print usage information.
                     if (usageArgument != null && usageArgument.getName().equals(a.getName())) {
-                        writeUsageToOutputStream();
+                        writeToUsageOutputStream(getUsage());
                         return;
                     }
                 }
@@ -1055,7 +1075,7 @@
                             // If this is the usage argument,
                             // then immediately stop and print usage information.
                             if (usageArgument != null && usageArgument.getName().equals(b.getName())) {
-                                writeUsageToOutputStream();
+                                writeToUsageOutputStream(getUsage());
                                 return;
                             }
                         }
@@ -1260,6 +1280,15 @@
     }
 
     /**
+     * Sets whether the usage or version displayed.
+     *
+     * @param usageOrVersionDisplayed the usageOrVersionDisplayed to set
+     */
+    public void setUsageOrVersionDisplayed(boolean usageOrVersionDisplayed) {
+        this.usageOrVersionDisplayed = usageOrVersionDisplayed;
+    }
+
+    /**
      * Sets the version handler which will be used to display the product version.
      *
      * @param handler

--
Gitblit v1.10.0