[gnome-games/sudoku-vala] sudoku: Parse command line arguments



commit f7243ce95d56552b513973bbfdb76da5adf2f8f7
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Apr 11 16:15:30 2011 +1000

    sudoku: Parse command line arguments

 gnome-sudoku/src/Makefile.am       |    1 +
 gnome-sudoku/src/gnome-sudoku.vala |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)
---
diff --git a/gnome-sudoku/src/Makefile.am b/gnome-sudoku/src/Makefile.am
index 73a6c73..5147bfe 100644
--- a/gnome-sudoku/src/Makefile.am
+++ b/gnome-sudoku/src/Makefile.am
@@ -18,6 +18,7 @@ gnome_sudoku_LDADD = \
 	$(GMODULE_LIBS)
 
 gnome_sudoku_VALAFLAGS = \
+    --pkg posix \
     --pkg gtk+-3.0 \
     --pkg gmodule-2.0
 
diff --git a/gnome-sudoku/src/gnome-sudoku.vala b/gnome-sudoku/src/gnome-sudoku.vala
index dfd83dd..b0b6daf 100644
--- a/gnome-sudoku/src/gnome-sudoku.vala
+++ b/gnome-sudoku/src/gnome-sudoku.vala
@@ -358,10 +358,42 @@ public class Application
 
 class GlChess
 {
+    static bool show_version;
+    public static const OptionEntry[] options =
+    {
+        { "version", 'v', 0, OptionArg.NONE, ref show_version,
+          /* Help string for command line --version flag */
+          N_("Show release version"), null},
+        { null }
+    };
+
     public static int main (string[] args)
     {
         Gtk.init (ref args);
 
+        var c = new OptionContext (/* Arguments and description for --help text */
+                                   _("[FILE] - Play Sudoku"));
+        c.add_main_entries (options, Config.GETTEXT_PACKAGE);
+        c.add_group (Gtk.get_option_group (true));
+        try
+        {
+            c.parse (ref args);
+        }
+        catch (Error e)
+        {
+            stderr.printf ("%s\n", e.message);
+            stderr.printf (/* Text printed out when an unknown command-line argument provided */
+                           _("Run '%s --help' to see a full list of available command line options."), args[0]);
+            stderr.printf ("\n");
+            return Posix.EXIT_FAILURE;
+        }
+        if (show_version)
+        {
+            /* Note, not translated so can be easily parsed */
+            stderr.printf ("gnome-sudoku %s\n", Config.VERSION);
+            return Posix.EXIT_SUCCESS;
+        }
+
         Application app = new Application ();
         app.start ();
 



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