* [JGIT PATCH 17/28] Support automatic command line parsing for TextBuiltin subclasses
2008-07-18 1:44 ` [JGIT PATCH 16/28] Convert jgit's Main to use args4j for basic parsing services Shawn O. Pearce
@ 2008-07-18 1:44 ` Shawn O. Pearce
0 siblings, 0 replies; 3+ messages in thread
From: Shawn O. Pearce @ 2008-07-18 1:44 UTC (permalink / raw)
To: Robin Rosenberg, Marek Zawirski; +Cc: git
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [JGIT PATCH 17/28] Support automatic command line parsing for TextBuiltin subclasses
@ 2008-07-18 19:01 Florian Köberle
2008-07-18 20:38 ` Shawn O. Pearce
0 siblings, 1 reply; 3+ messages in thread
From: Florian Köberle @ 2008-07-18 19:01 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: Robin Rosenberg, Marek Zawirski, git
Hi Shawn
to use args4j was really a great idea. I had a patch, implementing
something similar to args4j in my repository, but using a library is
always better. *thumb up*
I had a short look at the files and noticed that the inner "if (help) {"
is unnecessary:
+ 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);
+ }
Also a cool function which you may want to use is:
public <U> Class<? extends U> asSubclass(Class<U> clazz)
With that method of Class<?> you can do the cast before you actually
create the object.
You could for example make use of them in SubcommandHandler:
l. 124 cmd = (TextBuiltin) cons.newInstance();
Best regards,
Florian Köberle
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [JGIT PATCH 17/28] Support automatic command line parsing for TextBuiltin subclasses
2008-07-18 19:01 [JGIT PATCH 17/28] Support automatic command line parsing for TextBuiltin subclasses Florian Köberle
@ 2008-07-18 20:38 ` Shawn O. Pearce
0 siblings, 0 replies; 3+ messages in thread
From: Shawn O. Pearce @ 2008-07-18 20:38 UTC (permalink / raw)
To: Florian KKKberle; +Cc: Robin Rosenberg, Marek Zawirski, git
Florian KKKberle <FloriansKarten@web.de> wrote:
> I had a short look at the files and noticed that the inner "if (help) {"
> is unnecessary:
>
> + if (help) {
> + System.err.print("jgit ");
> + System.err.print(commandName);
> + clp.printSingleLineUsage(System.err);
> + System.err.println();
> +
> + if (help) {
Gaaah. Good catch, thanks. I copied and pasted that block from the
global option parser to the per-command parser, and then refactored
it a little and missed removing this unnecessary inner if test.
I'll fix with a rebase.
--
Shawn.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-07-18 20:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-07-18 19:01 [JGIT PATCH 17/28] Support automatic command line parsing for TextBuiltin subclasses Florian Köberle
2008-07-18 20:38 ` Shawn O. Pearce
-- strict thread matches above, loose matches on Subject: below --
2008-07-18 1:43 [JGIT PATCH 00/28] Convert command line parsing to args4j Shawn O. Pearce
2008-07-18 1:43 ` [JGIT PATCH 01/28] Fix deadlock caused by push over SSH Shawn O. Pearce
2008-07-18 1:43 ` [JGIT PATCH 02/28] Use die utility method in glog Shawn O. Pearce
2008-07-18 1:43 ` [JGIT PATCH 03/28] Add args4j library for command line switch processing Shawn O. Pearce
2008-07-18 1:43 ` [JGIT PATCH 04/28] Move org.spearce.jgit.pgm to its own Java project Shawn O. Pearce
2008-07-18 1:43 ` [JGIT PATCH 05/28] Make TextBuiltin public so other packages can implement and use it Shawn O. Pearce
2008-07-18 1:43 ` [JGIT PATCH 06/28] Initialize TextBuiltins with the repository before execution Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 07/28] Define our own extended CmdLineParser for extra parsing support Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 08/28] Add parseTree method to RevWalk to obtain a RevTree from AnyObjectId Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 09/28] Add option handler for AbstractTreeIterator values Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 10/28] Add option handler for ObjectId values Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 11/28] Add option handler for PathTreeFilter values Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 12/28] Add option handler for RefSpec values Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 13/28] Add option handler for RevCommit values Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 14/28] Add option handler for RevTree values Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 15/28] Register most of our OptionHandler implementations for automatic use Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 16/28] Convert jgit's Main to use args4j for basic parsing services Shawn O. Pearce
2008-07-18 1:44 ` [JGIT PATCH 17/28] Support automatic command line parsing for TextBuiltin subclasses Shawn O. Pearce
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).