[seed] interpreter: Add --version argument; chain option parsing to libseed.



commit 9ba90d411acf73bf23325239687501bfa014cdde
Author: Tim Horton <hortont424 gmail com>
Date:   Wed Oct 28 01:26:21 2009 -0400

    interpreter: Add --version argument; chain option parsing to libseed.

 libseed/seed-engine.c |    4 +-
 libseed/seed-engine.h |    2 +
 libseed/seed.h        |    3 +-
 src/Makefile.am       |    3 +-
 src/args.c            |   81 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.c            |   13 ++++++++
 6 files changed, 102 insertions(+), 4 deletions(-)
---
diff --git a/libseed/seed-engine.c b/libseed/seed-engine.c
index 2b4bb0a..b7d4a05 100644
--- a/libseed/seed-engine.c
+++ b/libseed/seed-engine.c
@@ -1268,7 +1268,7 @@ static GOptionEntry seed_args[] = {
    "Disable Seed debugging", "FLAGS"},
 #endif /* SEED_ENABLE_DEBUG */
   {"seed-version", 0, 0, G_OPTION_ARG_NONE, &seed_arg_print_version,
-   "Print current version", 0},
+   "Print libseed version", 0},
   {NULL,},
 };
 
@@ -1300,7 +1300,7 @@ seed_parse_args (int *argc, char ***argv)
   /* Initiate any command line options from the backend */
 
   seed_group = seed_get_option_group ();
-  g_option_context_set_main_group (option_context, seed_group);
+  g_option_context_add_group (option_context, seed_group);
 
   if (!g_option_context_parse (option_context, argc, argv, &error))
     {
diff --git a/libseed/seed-engine.h b/libseed/seed-engine.h
index 9ddbebe..665578f 100644
--- a/libseed/seed-engine.h
+++ b/libseed/seed-engine.h
@@ -80,4 +80,6 @@ void seed_script_destroy (SeedScript * s);
 JSValueRef seed_simple_evaluate (JSContextRef ctx, const gchar * script,
 				 JSValueRef * exception);
 
+GOptionGroup * seed_get_option_group (void);
+
 #endif
diff --git a/libseed/seed.h b/libseed/seed.h
index f897758..11c3473 100644
--- a/libseed/seed.h
+++ b/libseed/seed.h
@@ -99,6 +99,8 @@ gchar *seed_exception_to_string (SeedContext ctx, SeedException exception);
 
 SeedValue seed_evaluate (SeedContext ctx, SeedScript * s, SeedObject this);
 
+GOptionGroup * seed_get_option_group (void);
+
 /*
  * seed-api.c
  */
@@ -431,5 +433,4 @@ seed_closure_invoke_with_context (SeedContext ctx, GClosure *closure, SeedValue
 void
 seed_closure_warn_exception (GClosure *c, SeedContext ctx, SeedException exception);
 
-
 #endif
diff --git a/src/Makefile.am b/src/Makefile.am
index d91ef20..04ee4c8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,7 +3,8 @@
 bin_PROGRAMS = seed
 
 seed_SOURCES = \
-	main.c
+	main.c \
+	args.c
 
 seed_CFLAGS = \
 	-DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" \
diff --git a/src/args.c b/src/args.c
new file mode 100644
index 0000000..9b9a488
--- /dev/null
+++ b/src/args.c
@@ -0,0 +1,81 @@
+/* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */
+
+/*
+ * This file is part of Seed, the GObject Introspection<->Javascript bindings.
+ *
+ * Seed is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ * Seed is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) Robert Carr 2009 <carrr rpi edu>
+ */
+
+#include <stdlib.h>
+#include <glib.h>
+#include <glib-object.h>
+#include "../libseed/seed.h"
+#include "../libseed/seed-debug.h"
+#include <girepository.h>
+#include "config.h"
+
+extern gboolean seed_interpreter_arg_print_version;
+
+static GOptionEntry seed_args[] = {
+  {"version", 0, 0, G_OPTION_ARG_NONE, &seed_interpreter_arg_print_version,
+   "Print interpreter version", 0},
+  {NULL,},
+};
+
+static GOptionGroup *
+seed_interpreter_get_option_group (void)
+{
+  GOptionGroup *group;
+
+  group = g_option_group_new ("seed-interpreter", "Interpreter Options",
+			      "Show Interpreter Options", NULL, NULL);
+  g_option_group_add_entries (group, seed_args);
+
+  return group;
+}
+
+gboolean
+seed_interpreter_parse_args (int *argc, char ***argv)
+{
+  GOptionContext *option_context;
+  GOptionGroup *interpreter_group;
+
+  GError *error = NULL;
+  gboolean ret = TRUE;
+
+  option_context = g_option_context_new (NULL);
+  g_option_context_set_ignore_unknown_options (option_context, TRUE);
+  g_option_context_set_help_enabled (option_context, TRUE);
+
+  /* Initiate any command line options from the backend */
+
+  interpreter_group = seed_interpreter_get_option_group ();
+  g_option_context_set_main_group (option_context, interpreter_group);
+  g_option_context_add_group (option_context, seed_get_option_group());
+
+  if (!g_option_context_parse (option_context, argc, argv, &error))
+    {
+      if (error)
+	{
+	  g_warning ("%s", error->message);
+	  g_error_free (error);
+	}
+
+      ret = FALSE;
+    }
+
+  g_option_context_free (option_context);
+
+  return ret;
+}
diff --git a/src/main.c b/src/main.c
index c8b42b5..f2a8e6a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,10 +23,14 @@
 #include "../libseed/seed.h"
 #include "../libseed/seed-debug.h"
 #include <girepository.h>
+#include "config.h"
 
 #define DEFAULT_PATH "."
 
 SeedEngine *eng;
+gboolean seed_interpreter_arg_print_version;
+
+gboolean seed_interpreter_parse_args (int *argc, char ***argv);
 
 static void
 seed_repl ()
@@ -85,6 +89,15 @@ main (gint argc, gchar ** argv)
 {
   g_set_prgname ("seed");
   g_thread_init (0);
+  
+  seed_interpreter_parse_args(&argc, &argv);
+  
+  if (seed_interpreter_arg_print_version)
+    {
+      g_print("%s\n", "Seed " VERSION);
+      exit(EXIT_SUCCESS);
+    }
+
   eng = seed_init (&argc, &argv);
 
   seed_engine_set_search_path (eng, DEFAULT_PATH);



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