[JGIT PATCH 17/28] Support automatic command line parsing for TextBuiltin subclasses

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



By default the execute method will parse the supplied arguments
and then invoke run().  Newer style builtins will override the
run method, rather than the execute method, and take advantage of
the standard parsing support.

Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx>
---
 .../src/org/spearce/jgit/pgm/TextBuiltin.java      |   69 +++++++++++++++++++-
 1 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
index 0f39f11..c4a55f4 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
@@ -43,9 +43,13 @@ import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.Option;
 import org.spearce.jgit.lib.Constants;
 import org.spearce.jgit.lib.ObjectId;
 import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.pgm.opt.CmdLineParser;
+import org.spearce.jgit.revwalk.RevWalk;
 
 /**
  * Abstract command which can be invoked from the command line.
@@ -67,12 +71,18 @@ public abstract class TextBuiltin {
 
 	private String commandName;
 
+	@Option(name = "--help", usage = "display this help text", aliases = { "-h" })
+	private boolean help;
+
 	/** Stream to output to, typically this is standard output. */
 	protected PrintWriter out;
 
 	/** Git repository the command was invoked within. */
 	protected Repository db;
 
+	/** RevWalk used during command line parsing, if it was required. */
+	protected RevWalk argWalk;
+
 	/**
 	 * Set the name this command can be invoked as on the command line.
 	 * 
@@ -94,7 +104,7 @@ public abstract class TextBuiltin {
 	}
 
 	/**
-	 * Perform the action(s) of this command.
+	 * Parse arguments and run this command.
 	 * 
 	 * @param args
 	 *            command line arguments passed after the command name.
@@ -103,7 +113,62 @@ public abstract class TextBuiltin {
 	 *             framework will catch the exception and print a message on
 	 *             standard error.
 	 */
-	public abstract void execute(String[] args) throws Exception;
+	public void execute(String[] args) throws Exception {
+		parseArguments(args);
+		run();
+	}
+
+	/**
+	 * Parses the command line arguments prior to running.
+	 * <p>
+	 * This method should only be invoked by {@link #execute(String[])}, prior
+	 * to calling {@link #run()}. The default implementation parses all
+	 * arguments into this object's instance fields.
+	 * 
+	 * @param args
+	 *            the arguments supplied on the command line, if any.
+	 */
+	protected void parseArguments(final String[] args) {
+		final CmdLineParser clp = new CmdLineParser(this);
+		try {
+			clp.parseArgument(args);
+		} catch (CmdLineException err) {
+			if (!help) {
+				System.err.println("fatal: " + err.getMessage());
+				System.exit(1);
+			}
+		}
+
+		if (help) {
+			System.err.print("jgit ");
+			System.err.print(commandName);
+			clp.printSingleLineUsage(System.err);
+			System.err.println();
+
+			if (help) {
+				System.err.println();
+				clp.printUsage(System.err);
+				System.err.println();
+			}
+			System.exit(1);
+		}
+
+		argWalk = clp.getRevWalkGently();
+	}
+
+	/**
+	 * Perform the actions of this command.
+	 * <p>
+	 * This method should only be invoked by {@link #execute(String[])}.
+	 * 
+	 * @throws Exception
+	 *             an error occurred while processing the command. The main
+	 *             framework will catch the exception and print a message on
+	 *             standard error.
+	 */
+	protected void run() throws Exception {
+		throw die("Override either execute (legacy) or run (new style).");
+	}
 
 	/**
 	 * @return the repository this command accesses.
-- 
1.5.6.3.569.ga9185

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux