git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 18, 2008, 01:44 UTC
Message-ID
<1216345461-59382-18-git-send-email-spearce@spearce.org>
In-Reply-To
<1216345461-59382-17-git-send-email-spearce@spearce.org>

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@spearce.org>
---
 .../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
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 18 of 29 in “Convert command line parsing to args4j”
  1. 00/28 Convert command line parsing to args4jShawn O. Pearce, Jul 18, 2008
  2. 01/28 Fix deadlock caused by push over SSHShawn O. Pearce, Jul 18, 2008
  3. 02/28 Use die utility method in glogShawn O. Pearce, Jul 18, 2008
  4. 03/28 Add args4j library for command line switch processingShawn O. Pearce, Jul 18, 2008
  5. 04/28 Move org.spearce.jgit.pgm to its own Java projectShawn O. Pearce, Jul 18, 2008
  6. 05/28 Make TextBuiltin public so other packages can implement and use itShawn O. Pearce, Jul 18, 2008
  7. 06/28 Initialize TextBuiltins with the repository before executionShawn O. Pearce, Jul 18, 2008
  8. 07/28 Define our own extended CmdLineParser for extra parsing supportShawn O. Pearce, Jul 18, 2008
  9. 08/28 Add parseTree method to RevWalk to obtain a RevTree from AnyObjectIdShawn O. Pearce, Jul 18, 2008
  10. 09/28 Add option handler for AbstractTreeIterator valuesShawn O. Pearce, Jul 18, 2008
  11. 10/28 Add option handler for ObjectId valuesShawn O. Pearce, Jul 18, 2008
  12. 11/28 Add option handler for PathTreeFilter valuesShawn O. Pearce, Jul 18, 2008
  13. 12/28 Add option handler for RefSpec valuesShawn O. Pearce, Jul 18, 2008
  14. 13/28 Add option handler for RevCommit valuesShawn O. Pearce, Jul 18, 2008
  15. 14/28 Add option handler for RevTree valuesShawn O. Pearce, Jul 18, 2008
  16. 15/28 Register most of our OptionHandler implementations for automatic useShawn O. Pearce, Jul 18, 2008
  17. 16/28 Convert jgit's Main to use args4j for basic parsing servicesShawn O. Pearce, Jul 18, 2008
  18. 17/28 Support automatic command line parsing for TextBuiltin subclassesShawn O. Pearce, Jul 18, 2008
  19. 18/28 Convert diff-tree program to args4jShawn O. Pearce, Jul 18, 2008
  20. 19/28 Convert fetch program to args4jShawn O. Pearce, Jul 18, 2008
  21. 20/28 Convert index-pack program to args4jShawn O. Pearce, Jul 18, 2008
  22. 21/28 Convert ls-remote program to args4jShawn O. Pearce, Jul 18, 2008
  23. 22/28 Convert ls-tree program to args4jShawn O. Pearce, Jul 18, 2008
  24. 23/28 Convert merge-base program to args4jShawn O. Pearce, Jul 18, 2008
  25. 24/28 Convert push program to args4jShawn O. Pearce, Jul 18, 2008
  26. 25/28 Convert show-ref program to args4jShawn O. Pearce, Jul 18, 2008
  27. 26/28 Convert tag program to args4jShawn O. Pearce, Jul 18, 2008
  28. 27/28 Convert rev-list, log, glog programs to args4jShawn O. Pearce, Jul 18, 2008
  29. 28/28 Remove support for legacy style TextBuiltinsShawn O. Pearce, Jul 18, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.