LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  November 2014

HPS-SVN November 2014

Subject:

r1465 - /java/trunk/conditions/src/main/java/org/hps/conditions/cli/AbstractCommand.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Mon, 10 Nov 2014 08:15:37 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (108 lines)

Author: [log in to unmask]
Date: Mon Nov 10 00:15:35 2014
New Revision: 1465

Log:
Add generic parsing to abstract class's execute method.

Modified:
    java/trunk/conditions/src/main/java/org/hps/conditions/cli/AbstractCommand.java

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/AbstractCommand.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/AbstractCommand.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/AbstractCommand.java	Mon Nov 10 00:15:35 2014
@@ -1,12 +1,15 @@
 package org.hps.conditions.cli;
 
+import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.Parser;
 import org.apache.commons.cli.PosixParser;
 
 /**
- * This is the API that sub-commands must implement in the conditions command line interface.
+ * This is the API that sub-commands must implement in the conditions command
+ * line interface.
  * 
  * @author Jeremy McCormick <[log in to unmask]>
  */
@@ -16,20 +19,22 @@
     String description;
     Options options = new Options();
     Parser parser = new PosixParser();
+    CommandLine commandLine;
     boolean verbose = false;
 
     /**
-     * Class constructor.     
+     * Class constructor.
      * @param name The string that invokes this command.
      * @param description The description of this command.
      */
     AbstractCommand(String name, String description) {
         this.name = name;
         this.description = description;
+        options.addOption("h", false, "Print help for this command");
     }
-    
+
     /**
-     * Get the name of this command.     
+     * Get the name of this command.
      * @return A String of the name of this command.
      */
     String getName() {
@@ -37,7 +42,7 @@
     }
 
     /**
-     * Get the description of this command.    
+     * Get the description of this command.
      * @return A String of the description of this command.
      */
     String getDescription() {
@@ -45,7 +50,7 @@
     }
 
     /**
-     * Options for this command.     
+     * Options for this command.
      * @return Options object for this command.
      */
     Options getOptions() {
@@ -60,7 +65,7 @@
         HelpFormatter help = new HelpFormatter();
         help.printHelp(getName(), getOptions());
     }
-    
+
     /**
      * Set whether verbose output is enabled.
      * @param verbose True to enable verbose output.
@@ -70,9 +75,19 @@
     }
 
     /**
-     * Execute the command with the arguments.  
-     * This is the only method that sub-classes must implement. 
+     * Execute the command with the arguments. This is the only method that
+     * sub-classes must implement.
      * @param arguments The sub-command's arguments.
      */
-    abstract void execute(String[] arguments);
+    void execute(String[] arguments) {
+        try {
+            commandLine = parser.parse(options, arguments);
+        } catch (ParseException e) {
+            throw new RuntimeException(e);
+        }
+        if (commandLine.hasOption("h")) {
+            this.printUsage();
+            System.exit(0);
+        }
+    }
 }

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use