Author: bfoster
Date: Sat Dec  3 19:35:39 2011
New Revision: 1209988

URL: http://svn.apache.org/viewvc?rev=1209988&view=rev
Log:
- Added supported for CmdLineAction detailed help description
- Fixed CmdLineUtility to print action messages
- Changed CmdLineUtility's run(String[]) method to throw RuntimeException when 
in debug mode
- Changed Print Supported Actions StdCmdLinePrinter output format to more 
"readable"

-------------------
OODT-360
OODT-359
OODT-358
OODT-357

Modified:
    oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/CmdLineUtility.java
    
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/action/CmdLineAction.java
    
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/printer/StdCmdLinePrinter.java
    oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/util/CmdLineUtils.java
    oodt/trunk/cli/src/test/org/apache/oodt/cas/cli/util/TestCmdLineUtils.java

Modified: 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/CmdLineUtility.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/CmdLineUtility.java?rev=1209988&r1=1209987&r2=1209988&view=diff
==============================================================================
--- oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/CmdLineUtility.java 
(original)
+++ oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/CmdLineUtility.java 
Sat Dec  3 19:35:39 2011
@@ -146,6 +146,10 @@ public class CmdLineUtility {
             .getSupportedActions()));
    }
 
+   public void printActionMessages(List<String> messages) {
+      presenter.presentActionMessage(printer.printActionMessages(messages));
+   }
+
    public void printValidationErrors(List<CmdLineOptionValidator.Result> 
results) {
       
presenter.presentErrorMessage(printer.printOptionValidationErrors(results));
    }
@@ -175,7 +179,7 @@ public class CmdLineUtility {
             execute(cmdLineArgs);
          }
       } catch (Exception e) {
-         if (debugMode) { e.printStackTrace(); }
+         if (debugMode) { throw new RuntimeException(e); }
          printErrorMessage(e.getMessage());
       }
    }
@@ -288,7 +292,9 @@ public class CmdLineUtility {
 
       handle(cmdLineArgs);
 
-      cmdLineArgs.getSpecifiedAction().execute(new ActionMessagePrinter());
+      ActionMessagePrinter printer = new ActionMessagePrinter();
+      cmdLineArgs.getSpecifiedAction().execute(printer);
+      printActionMessages(printer.getPrintedMessages());
    }
 
    /**

Modified: 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/action/CmdLineAction.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/action/CmdLineAction.java?rev=1209988&r1=1209987&r2=1209988&view=diff
==============================================================================
--- 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/action/CmdLineAction.java 
(original)
+++ 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/action/CmdLineAction.java 
Sat Dec  3 19:35:39 2011
@@ -35,6 +35,8 @@ public abstract class CmdLineAction {
 
    private String name;
    private String description;
+   private String detailedDescription;
+   private List<Example> examples;
 
    public CmdLineAction() {
    }
@@ -60,6 +62,22 @@ public abstract class CmdLineAction {
       return description;
    }
 
+   public void setDetailedDescription(String detailedDescription) {
+      this.detailedDescription = detailedDescription;
+   }
+
+   public String getDetailedDescription() {
+      return detailedDescription;
+   }
+
+   public void setExamples(List<Example> examples) {
+      this.examples = examples;
+   }
+
+   public List<Example> getExamples() {
+      return examples;
+   }
+
    public abstract void execute(ActionMessagePrinter printer)
          throws CmdLineActionException;
 
@@ -87,4 +105,22 @@ public abstract class CmdLineAction {
          return messages;
       }
    }
+
+   public static class Example {
+      private String description;
+      private String example;
+
+      public Example(String description, String example) {
+         this.description = description;
+         this.example = example;
+      }
+
+      public String getDescription() {
+         return description;
+      }
+
+      public String getExample() {
+         return example;
+      }
+   }
 }

Modified: 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/printer/StdCmdLinePrinter.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/printer/StdCmdLinePrinter.java?rev=1209988&r1=1209987&r2=1209988&view=diff
==============================================================================
--- 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/printer/StdCmdLinePrinter.java
 (original)
+++ 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/printer/StdCmdLinePrinter.java
 Sat Dec  3 19:35:39 2011
@@ -24,6 +24,7 @@ import static org.apache.oodt.cas.cli.ut
 import static 
org.apache.oodt.cas.cli.util.CmdLineUtils.determineRequiredSubOptions;
 import static org.apache.oodt.cas.cli.util.CmdLineUtils.getFormattedString;
 import static org.apache.oodt.cas.cli.util.CmdLineUtils.isGroupOption;
+import static org.apache.oodt.cas.cli.util.CmdLineUtils.sortActions;
 import static 
org.apache.oodt.cas.cli.util.CmdLineUtils.sortOptionsByRequiredStatus;
 
 //JDK imports
@@ -35,6 +36,7 @@ import org.apache.commons.lang.StringUti
 
 //OODT imports
 import org.apache.oodt.cas.cli.action.CmdLineAction;
+import org.apache.oodt.cas.cli.action.CmdLineAction.Example;
 import org.apache.oodt.cas.cli.option.ActionCmdLineOption;
 import org.apache.oodt.cas.cli.option.AdvancedCmdLineOption;
 import org.apache.oodt.cas.cli.option.CmdLineOption;
@@ -57,7 +59,31 @@ public class StdCmdLinePrinter implement
          Set<CmdLineOption> options) {
       StringBuffer sb = new StringBuffer("");
       sb.append(getHeader(action)).append("\n");
+      sb.append(getDescription(action)).append("\n");
+      sb.append(getUsage(action, options)).append("\n");
+      sb.append(getExamples(action)).append("\n");
+      sb.append(getFooter(action)).append("\n");
+      return sb.toString();
+   }
+
+   protected String getHeader(CmdLineAction action) {
+      return "** Action Help for '" + action.getName() + "' **";
+   }
 
+   protected String getDescription(CmdLineAction action) {
+      StringBuffer sb = new StringBuffer("DESCRIPTION:\n");
+      if (action.getDetailedDescription() != null) {
+         sb.append(action.getDetailedDescription());
+      } else if (action.getDescription() != null){
+         sb.append(" ").append(action.getDescription());
+      } else {
+         sb.append(" - N/A");
+      }
+      return sb.append("\n").toString();
+   }
+
+   protected String getUsage(CmdLineAction action, Set<CmdLineOption> options) 
{
+      StringBuffer sb = new StringBuffer("USAGE:\n");
       sb.append(getRequiredSubHeader()).append("\n");
       Set<CmdLineOption> requiredOptions = determineRequired(action, options);
       List<CmdLineOption> sortedRequiredOptions = 
sortOptionsByRequiredStatus(requiredOptions);
@@ -71,17 +97,11 @@ public class StdCmdLinePrinter implement
       for (CmdLineOption option : sortedOptionalOptions) {
          sb.append(getOptionalOptionHelp(action, option)).append("\n");
       }
-
-      sb.append(getFooter(action)).append("\n");
       return sb.toString();
    }
 
-   protected String getHeader(CmdLineAction action) {
-      return "Action Help for '" + action.getName() + "'";
-   }
-
    protected String getRequiredSubHeader() {
-      return " - Required:";
+      return " Required:";
    }
 
    protected String getRequiredOptionHelp(CmdLineAction action,
@@ -89,12 +109,12 @@ public class StdCmdLinePrinter implement
       if (option instanceof GroupCmdLineOption) {
          return getGroupHelp(action, (GroupCmdLineOption) option, "    ");
       } else {
-         return getOptionHelp(action, option, "    ");
+         return getOptionHelp(action, option, "  ");
       }
    }
 
    protected String getOptionalSubHeader() {
-      return " - Optional:";
+      return " Optional:";
    }
 
    protected String getOptionalOptionHelp(CmdLineAction action,
@@ -102,8 +122,21 @@ public class StdCmdLinePrinter implement
       if (option instanceof GroupCmdLineOption) {
          return getGroupHelp(action, (GroupCmdLineOption) option, "    ");
       } else {
-         return getOptionHelp(action, option, "    ");
+         return getOptionHelp(action, option, "  ");
+      }
+   }
+
+   protected String getExamples(CmdLineAction action) {
+      StringBuffer sb = new StringBuffer("EXAMPLES:\n");
+      if (action.getExamples() != null && !action.getExamples().isEmpty()) {
+         for (Example example : action.getExamples()) {
+            sb.append(example.getDescription()).append("\n");
+            sb.append(" - ").append(example.getExample()).append("\n");
+         }
+      } else {
+         sb.append(" - N/A");
       }
+      return sb.toString();
    }
 
    protected String getFooter(CmdLineAction action) {
@@ -165,13 +198,15 @@ public class StdCmdLinePrinter implement
    @Override
    public String printActionsHelp(Set<CmdLineAction> actions) {
       StringBuffer sb = new StringBuffer("");
-      sb.append("Actions:").append("\n");
-      for (CmdLineAction action : actions) {
-         sb.append("  Action:").append("\n");
-         sb.append("    Name: ").append(action.getName()).append("\n");
-         sb.append("    Description: ").append(action.getDescription())
-               .append("\n").append("\n");
+      
sb.append("-----------------------------------------------------------------------------------------------------------------\n");
+      sb.append("|" + StringUtils.rightPad(" Action", 35) + "|"
+            + " Description\n");
+      
sb.append("-----------------------------------------------------------------------------------------------------------------\n");
+      for (CmdLineAction action : sortActions(actions)) {
+         sb.append("  ").append(StringUtils.rightPad(action.getName(), 35));
+         sb.append(" ").append(action.getDescription()).append("\n\n");
       }
+      
sb.append("-----------------------------------------------------------------------------------------------------------------\n");
       return sb.toString();
    }
 

Modified: 
oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/util/CmdLineUtils.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/util/CmdLineUtils.java?rev=1209988&r1=1209987&r2=1209988&view=diff
==============================================================================
--- oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/util/CmdLineUtils.java 
(original)
+++ oodt/trunk/cli/src/main/java/org/apache/oodt/cas/cli/util/CmdLineUtils.java 
Sat Dec  3 19:35:39 2011
@@ -302,6 +302,23 @@ public class CmdLineUtils {
    }
 
    /**
+    * Sorts {@link CmdLineAction}s by there name.
+    *
+    * @param actions The {@link CmdLineAction}s to be sorted
+    * @return Sorted {@link List} of {@link CmdLineAction}s
+    */
+   public static List<CmdLineAction> sortActions(Set<CmdLineAction> actions) {
+      List<CmdLineAction> actionsList = Lists.newArrayList(actions);
+      Collections.sort(actionsList, new Comparator<CmdLineAction>() {
+         @Override
+         public int compare(CmdLineAction a1, CmdLineAction a2) {
+            return a1.getName().compareTo(a2.getName());
+         }
+      });
+      return actionsList;
+   }
+
+   /**
     * Finds {@link CmdLineOption} whose short name or long name equals given
     * option name.
     * 

Modified: 
oodt/trunk/cli/src/test/org/apache/oodt/cas/cli/util/TestCmdLineUtils.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/cli/src/test/org/apache/oodt/cas/cli/util/TestCmdLineUtils.java?rev=1209988&r1=1209987&r2=1209988&view=diff
==============================================================================
--- oodt/trunk/cli/src/test/org/apache/oodt/cas/cli/util/TestCmdLineUtils.java 
(original)
+++ oodt/trunk/cli/src/test/org/apache/oodt/cas/cli/util/TestCmdLineUtils.java 
Sat Dec  3 19:35:39 2011
@@ -184,6 +184,19 @@ public class TestCmdLineUtils extends Te
             Sets.newHashSet(sortedOptions.get(2), sortedOptions.get(3)));
    }
 
+   public void testSortActions() {
+      Set<CmdLineAction> actions = Sets.newHashSet(
+            createAction("Tom"),
+            createAction("Bill"),
+            createAction("Young"),
+            createAction("Andy"));
+      List<CmdLineAction> sortedActions = CmdLineUtils.sortActions(actions);
+      assertEquals("Andy", sortedActions.get(0).getName());
+      assertEquals("Bill", sortedActions.get(1).getName());
+      assertEquals("Tom", sortedActions.get(2).getName());
+      assertEquals("Young", sortedActions.get(3).getName());
+   }
+
    public void testGetOptionByName() {
       CmdLineAction action = createAction("action");
       CmdLineOption userOption, urlOption, passOption, actionOption;


Reply via email to