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

[JGIT PATCH 16/28] Convert jgit's Main to use args4j for basic parsing services

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

We now use args4j for our global options parsing and for the subcommand selection. The remaining arguments that are left over after the subcommand are sent to the subcommand unmodified. An additional CmdLineParser could be created by the subcommand to handle its own argument parsing over the remaining options.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/pgm/Main.java             |  111 ++++++---------
 .../src/org/spearce/jgit/pgm/TextBuiltin.java      |   19 +++
 .../spearce/jgit/pgm/opt/SubcommandHandler.java    |  149 ++++++++++++++++++++
 3 files changed, 214 insertions(+), 65 deletions(-)
 create mode 100644 org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Main.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Main.java
index 68748bf..c069989 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Main.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Main.java
@@ -39,8 +39,13 @@
 package org.spearce.jgit.pgm;
 
 import java.io.File;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
 
+import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.ExampleMode;
+import org.kohsuke.args4j.Option;
 import org.spearce.jgit.awtui.AwtAuthenticator;
 import org.spearce.jgit.errors.TransportException;
 import org.spearce.jgit.lib.Repository;
@@ -50,7 +55,20 @@ import org.spearce.jgit.util.HttpSupport;
 
 /** Command line entry point. */
 public class Main {
-	private static boolean showStackTrace;
+	@Option(name = "--help", usage = "display this help text", aliases = { "-h" })
+	private boolean help;
+
+	@Option(name = "--show-stack-trace", usage = "display the Java stack trace on exceptions")
+	private boolean showStackTrace;
+
+	@Option(name = "--git-dir", metaVar = "GIT_DIR", usage = "set the git repository to operate on")
+	private File gitdir;
+
+	@Argument(index = 0, metaVar = "command", required = true, handler = SubcommandHandler.class)
+	private TextBuiltin subcommand;
+
+	@Argument(index = 1, metaVar = "ARG")
+	private List<String> arguments = new ArrayList<String>();
 
 	/**
 	 * Execute the command line.
@@ -59,23 +77,24 @@ public class Main {
 	 *            arguments.
 	 */
 	public static void main(final String[] argv) {
+		final Main me = new Main();
 		try {
 			AwtAuthenticator.install();
 			HttpSupport.configureHttpProxy();
-			execute(argv);
+			me.execute(argv);
 		} catch (Die err) {
 			System.err.println("fatal: " + err.getMessage());
-			if (showStackTrace)
+			if (me.showStackTrace)
 				err.printStackTrace();
 			System.exit(128);
 		} catch (Exception err) {
-			if (!showStackTrace && err.getCause() != null
+			if (!me.showStackTrace && err.getCause() != null
 					&& err instanceof TransportException)
 				System.err.println("fatal: " + err.getCause().getMessage());
 
 			if (err.getClass().getName().startsWith("org.spearce.jgit.errors.")) {
 				System.err.println("fatal: " + err.getMessage());
-				if (showStackTrace)
+				if (me.showStackTrace)
 					err.printStackTrace();
 				System.exit(128);
 			}
@@ -84,24 +103,28 @@ public class Main {
 		}
 	}
 
-	private static void execute(final String[] argv) throws Exception {
-		int argi = 0;
+	private void execute(final String[] argv) throws Exception {
+		final CmdLineParser clp = new CmdLineParser(this);
+		try {
+			clp.parseArgument(argv);
+		} catch (CmdLineException err) {
+			if (argv.length > 0 && !help) {
+				System.err.println("fatal: " + err.getMessage());
+				System.exit(1);
+			}
+		}
 
-		File gitdir = null;
-		for (; argi < argv.length; argi++) {
-			final String arg = argv[argi];
-			if (arg.startsWith("--git-dir="))
-				gitdir = new File(arg.substring("--git-dir=".length()));
-			else if (arg.equals("--show-stack-trace"))
-				showStackTrace = true;
-			else if (arg.startsWith("--"))
-				usage();
-			else
-				break;
+		if (argv.length == 0 || help) {
+			final String ex = clp.printExample(ExampleMode.ALL);
+			System.err.println("jgit" + ex + " command [ARG ...]");
+			if (help) {
+				System.err.println();
+				clp.printUsage(System.err);
+				System.err.println();
+			}
+			System.exit(1);
 		}
 
-		if (argi == argv.length)
-			usage();
 		if (gitdir == null)
 			gitdir = findGitDir();
 		if (gitdir == null || !gitdir.isDirectory()) {
@@ -109,10 +132,10 @@ public class Main {
 			System.exit(1);
 		}
 
-		final TextBuiltin cmd = createCommand(argv[argi++]);
+		final TextBuiltin cmd = subcommand;
 		cmd.init(new Repository(gitdir));
 		try {
-			cmd.execute(subarray(argv, argi));
+			cmd.execute(arguments.toArray(new String[arguments.size()]));
 		} finally {
 			if (cmd.out != null)
 				cmd.out.flush();
@@ -129,46 +152,4 @@ public class Main {
 		}
 		return null;
 	}
-
-	private static String[] subarray(final String[] argv, final int i) {
-		return Arrays.asList(argv).subList(i, argv.length).toArray(
-				new String[0]);
-	}
-
-	private static TextBuiltin createCommand(final String name) {
-		final StringBuilder s = new StringBuilder();
-		s.append(mypackage());
-		s.append('.');
-		boolean upnext = true;
-		for (int i = 0; i < name.length(); i++) {
-			final char c = name.charAt(i);
-			if (c == '-') {
-				upnext = true;
-				continue;
-			}
-			if (upnext)
-				s.append(Character.toUpperCase(c));
-			else
-				s.append(c);
-			upnext = false;
-		}
-		try {
-			return (TextBuiltin) Class.forName(s.toString()).newInstance();
-		} catch (Exception e) {
-			System.err.println("error: " + name + " is not a jgit command.");
-			System.exit(1);
-			return null;
-		}
-	}
-
-	private static String mypackage() {
-		final String p = Main.class.getName();
-		final int dot = p.lastIndexOf('.');
-		return p.substring(0, dot);
-	}
-
-	private static void usage() {
-		System.err.println("jgit [--git-dir=path] cmd ...");
-		System.exit(1);
-	}
 }
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 05ad211..0f39f11 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
@@ -65,12 +65,24 @@ public abstract class TextBuiltin {
 
 	protected static final String REFS_TAGS = Constants.TAGS_PREFIX + "/";
 
+	private String commandName;
+
 	/** Stream to output to, typically this is standard output. */
 	protected PrintWriter out;
 
 	/** Git repository the command was invoked within. */
 	protected Repository db;
 
+	/**
+	 * Set the name this command can be invoked as on the command line.
+	 * 
+	 * @param name
+	 *            the name of the command.
+	 */
+	public void setCommandName(final String name) {
+		commandName = name;
+	}
+
 	void init(final Repository repo) {
 		try {
 			out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
@@ -93,6 +105,13 @@ public abstract class TextBuiltin {
 	 */
 	public abstract void execute(String[] args) throws Exception;
 
+	/**
+	 * @return the repository this command accesses.
+	 */
+	public Repository getRepository() {
+		return db;
+	}
+
 	protected ObjectId resolve(final String s) throws IOException {
 		final ObjectId r = db.resolve(s);
 		if (r == null)
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java
new file mode 100644
index 0000000..2318a03
--- /dev/null
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.pgm.opt;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.text.MessageFormat;
+
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.OptionDef;
+import org.kohsuke.args4j.spi.OptionHandler;
+import org.kohsuke.args4j.spi.Parameters;
+import org.kohsuke.args4j.spi.Setter;
+import org.spearce.jgit.pgm.Main;
+import org.spearce.jgit.pgm.TextBuiltin;
+
+/**
+ * Custom Argument handler for jgit command selection.
+ * <p>
+ * Translates a single argument string to a {@link TextBuiltin} instance which
+ * we can execute at runtime with the remaining arguments of the parser.
+ */
+public class SubcommandHandler extends OptionHandler<TextBuiltin> {
+	private static String mypackage() {
+		final String p = Main.class.getName();
+		final int dot = p.lastIndexOf('.');
+		return p.substring(0, dot);
+	}
+
+	/**
+	 * Create a new handler for the command name.
+	 * <p>
+	 * This constructor is used only by args4j.
+	 * 
+	 * @param parser
+	 * @param option
+	 * @param setter
+	 */
+	public SubcommandHandler(final CmdLineParser parser,
+			final OptionDef option, final Setter<? super TextBuiltin> setter) {
+		super(parser, option, setter);
+	}
+
+	@Override
+	public int parseArguments(final Parameters params) throws CmdLineException {
+		final String name = params.getParameter(0);
+		final StringBuilder s = new StringBuilder();
+		s.append(mypackage());
+		s.append('.');
+		boolean upnext = true;
+		for (int i = 0; i < name.length(); i++) {
+			final char c = name.charAt(i);
+			if (c == '-') {
+				upnext = true;
+				continue;
+			}
+			if (upnext)
+				s.append(Character.toUpperCase(c));
+			else
+				s.append(c);
+			upnext = false;
+		}
+
+		final Class<?> clazz;
+		try {
+			clazz = Class.forName(s.toString());
+		} catch (ClassNotFoundException e) {
+			throw new CmdLineException(MessageFormat.format(
+					"{0} is not a jgit command", name));
+		}
+
+		if (!TextBuiltin.class.isAssignableFrom(clazz))
+			throw new CmdLineException(MessageFormat.format(
+					"{0} is not a jgit command", name));
+
+		final Constructor<?> cons;
+		try {
+			cons = clazz.getDeclaredConstructor();
+		} catch (SecurityException e) {
+			throw new CmdLineException("Cannot create " + name, e);
+		} catch (NoSuchMethodException e) {
+			throw new CmdLineException("Cannot create " + name, e);
+		}
+		cons.setAccessible(true);
+
+		final TextBuiltin cmd;
+		try {
+			cmd = (TextBuiltin) cons.newInstance();
+		} catch (InstantiationException e) {
+			throw new CmdLineException("Cannot create " + name, e);
+		} catch (IllegalAccessException e) {
+			throw new CmdLineException("Cannot create " + name, e);
+		} catch (InvocationTargetException e) {
+			throw new CmdLineException("Cannot create " + name, e);
+		}
+
+		cmd.setCommandName(name);
+		setter.addValue(cmd);
+
+		// Force option parsing to stop. Everything after us should
+		// be arguments known only to this command and must not be
+		// recognized by the current parser.
+		//
+		owner.stopOptionParsing();
+
+		return 1;
+	}
+
+	@Override
+	public String getDefaultMetaVariable() {
+		return "command";
+	}
+}
-- 
1.5.6.3.569.ga9185
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 17 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.