From 9b1384eb5e70df3e6bc1fec5aed5c841adbd094b Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Thu, 11 Feb 2016 13:46:57 +0000
Subject: [PATCH] OPENDJSDK-42 Cli arguments fluent builder

---
 opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/SubCommandArgumentParser.java |   59 +++++++++++++++++++++++------------------------------------
 1 files changed, 23 insertions(+), 36 deletions(-)

diff --git a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/SubCommandArgumentParser.java b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/SubCommandArgumentParser.java
index 2c0ef33..f044761 100644
--- a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/SubCommandArgumentParser.java
+++ b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/SubCommandArgumentParser.java
@@ -207,58 +207,44 @@
      *             defined.
      */
     public void addGlobalArgument(Argument argument, ArgumentGroup group) throws ArgumentException {
-        String argumentName = argument.getName();
-        if (globalArgumentMap.containsKey(argumentName)) {
-            throw new ArgumentException(ERR_SUBCMDPARSER_DUPLICATE_GLOBAL_ARG_NAME.get(argumentName));
+        String longID = argument.getLongIdentifier();
+        if (globalArgumentMap.containsKey(longID)) {
+            throw new ArgumentException(ERR_SUBCMDPARSER_DUPLICATE_GLOBAL_ARG_NAME.get(longID));
         }
         for (SubCommand s : subCommands.values()) {
-            if (s.getArgumentForName(argumentName) != null) {
+            if (s.getArgumentForLongIdentifier(longID) != null) {
                 throw new ArgumentException(ERR_SUBCMDPARSER_GLOBAL_ARG_NAME_SUBCMD_CONFLICT.get(
-                        argumentName, s.getName()));
+                        longID, s.getName()));
             }
         }
 
         Character shortID = argument.getShortIdentifier();
         if (shortID != null) {
             if (globalShortIDMap.containsKey(shortID)) {
-                String name = globalShortIDMap.get(shortID).getName();
-
+                String conflictingLongID = globalShortIDMap.get(shortID).getLongIdentifier();
                 throw new ArgumentException(ERR_SUBCMDPARSER_DUPLICATE_GLOBAL_ARG_SHORT_ID.get(
-                        shortID, argumentName, name));
+                        shortID, longID, conflictingLongID));
             }
 
             for (SubCommand s : subCommands.values()) {
                 if (s.getArgument(shortID) != null) {
-                    String cmdName = s.getName();
-                    String name = s.getArgument(shortID).getName();
-
+                    String conflictingLongID = s.getArgument(shortID).getLongIdentifier();
                     throw new ArgumentException(ERR_SUBCMDPARSER_GLOBAL_ARG_SHORT_ID_CONFLICT.get(
-                            shortID, argumentName, name, cmdName));
+                            shortID, longID, conflictingLongID, s.getName()));
                 }
             }
         }
 
-        String longID = argument.getLongIdentifier();
-        if (longID != null) {
-            if (!longArgumentsCaseSensitive()) {
-                longID = toLowerCase(longID);
-            }
-
+        if (!longArgumentsCaseSensitive()) {
+            longID = toLowerCase(longID);
             if (globalLongIDMap.containsKey(longID)) {
-                String name = globalLongIDMap.get(longID).getName();
-
-                throw new ArgumentException(ERR_SUBCMDPARSER_DUPLICATE_GLOBAL_ARG_LONG_ID.get(
-                        argument.getLongIdentifier(), argumentName, name));
+                throw new ArgumentException(ERR_SUBCMDPARSER_DUPLICATE_GLOBAL_ARG_LONG_ID.get(longID));
             }
+        }
 
-            for (SubCommand s : subCommands.values()) {
-                if (s.getArgument(longID) != null) {
-                    String cmdName = s.getName();
-                    String name = s.getArgument(longID).getName();
-
-                    throw new ArgumentException(ERR_SUBCMDPARSER_GLOBAL_ARG_LONG_ID_CONFLICT.get(
-                            argument.getLongIdentifier(), argumentName, name, cmdName));
-                }
+        for (SubCommand s : subCommands.values()) {
+            if (s.getArgument(longID) != null) {
+                throw new ArgumentException(ERR_SUBCMDPARSER_GLOBAL_ARG_LONG_ID_CONFLICT.get(longID, s.getName()));
             }
         }
 
@@ -1143,11 +1129,6 @@
                 if (a.isHidden()) {
                     continue;
                 }
-                // Return a generic FQDN for localhost as the default hostname
-                // in reference documentation.
-                if (isHostNameArgument(a)) {
-                    a.setDefaultValue("localhost.localdomain");
-                }
 
                 Map<String, Object> option = new HashMap<>();
                 String optionSynopsis = getOptionSynopsis(a);
@@ -1162,7 +1143,8 @@
                     // Let this build its own arbitrarily formatted additional info.
                     info.put("usage", subCommandUsageHandler.getArgumentAdditionalInfo(subCommand, a, nameOption));
                 } else {
-                    String defaultValue = a.getDefaultValue();
+                    // Return a generic FQDN for localhost as the default hostname in reference documentation.
+                    final String defaultValue = isHostNameArgument(a) ? "localhost.localdomain" : a.getDefaultValue();
                     info.put("default", defaultValue != null ? REF_DEFAULT.get(defaultValue) : null);
 
                     // If there is a supplement to the description for this argument,
@@ -1239,4 +1221,9 @@
         map.put("subcommands", commands);
         applyTemplate(builder, "dscfgReference.ftl", map);
     }
+
+    @Override
+    public void replaceArgument(final Argument argument) {
+        replaceArgumentInCollections(globalLongIDMap, globalShortIDMap, globalArgumentList, argument);
+    }
 }

--
Gitblit v1.10.0