commit c588131005232d911a1f03c40bbc9080ccfd6aa5
parent 0d6df1e48c94d1583afacbb5b011a7b99b5b7cb5
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Thu,  9 Aug 2012 11:02:33 -0700
Parse the command line
Diffstat:
| M | src/spek.cc |  |  | 52 | +++++++++++++++++++++++++++++++++++++++++++++++++++- | 
| M | src/spek.vala |  |  | 32 | -------------------------------- | 
2 files changed, 51 insertions(+), 33 deletions(-)
diff --git a/src/spek.cc b/src/spek.cc
@@ -16,21 +16,71 @@
  * along with Spek.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <wx/wx.h>
+#include <wx/cmdline.h>
+#include <wx/log.h>
 
 #include "spek-window.hh"
 
 class Spek: public wxApp
 {
+protected:
     virtual bool OnInit();
+    virtual void OnInitCmdLine(wxCmdLineParser& parser);
+    virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
 };
 
 IMPLEMENT_APP(Spek)
 
 bool Spek::OnInit()
 {
+    if (!wxApp::OnInit()) {
+        return false;
+    }
+
     SpekWindow *window = new SpekWindow(wxT("Hello World"), wxPoint(50,50), wxSize(450,340));
     window->Show(true);
     SetTopWindow(window);
     return true;
 }
+
+void Spek::OnInitCmdLine(wxCmdLineParser& parser)
+{
+    wxCmdLineEntryDesc desc[] = {{
+            wxCMD_LINE_SWITCH, wxT("h"),
+            wxT("help"), _("Show this help message"),
+            wxCMD_LINE_VAL_NONE,
+            wxCMD_LINE_OPTION_HELP
+        }, {
+            wxCMD_LINE_SWITCH,
+            wxT("V"),
+            wxT("version"),
+            _("Display the version and exit")
+        }, {
+            wxCMD_LINE_PARAM,
+            NULL,
+            NULL,
+            _("FILE"),
+            wxCMD_LINE_VAL_STRING,
+            wxCMD_LINE_PARAM_OPTIONAL
+        }, {
+            wxCMD_LINE_NONE
+        }
+    };
+
+    parser.SetDesc(desc);
+}
+
+bool Spek::OnCmdLineParsed(wxCmdLineParser& parser)
+{
+    if (!wxApp::OnCmdLineParsed(parser)) {
+        return false;
+    }
+
+    if (parser.Found(wxT("version"))) {
+        // TRANSLATORS: first %s is the package name, second %s is the package version.
+        wxPrintf(_("%s version %s\n"), wxT(PACKAGE_NAME), wxT(PACKAGE_VERSION));
+        return false;
+    }
+
+    return true;
+}
diff --git a/src/spek.vala b/src/spek.vala
@@ -21,15 +21,7 @@ namespace Spek {
     [CCode (array_length = false, array_null_terminated = true)]
     string[] files = null;
 
-    const OptionEntry[] options = {
-        { "version", 'V', 0, OptionArg.NONE, ref version, N_("Display the version and exit"), null },
-        { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files, null, null },
-        { null }
-    };
-
     int main (string[] args) {
-        Platform.fix_args (args);
-
         if (Preferences.instance.language.length > 0) {
             Environment.set_variable ("LANGUAGE", Preferences.instance.language, true);
         }
@@ -38,27 +30,6 @@ namespace Spek {
         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
         Intl.textdomain (Config.GETTEXT_PACKAGE);
 
-        try {
-            Gtk.init_with_args (ref args, _("[FILE]"), (OptionEntry[]) options, Config.GETTEXT_PACKAGE);
-        } catch (Error e) {
-            print (e.message);
-            print ("\n");
-            print (_("Run `%s --help` to see a full list of available command line options.\n"), args[0]);
-            return 1;
-        }
-
-        if (version) {
-            // TRANSLATORS: first %s is the package name, second %s is the package version.
-            print (_("%s version %s\n"), Config.PACKAGE_NAME, Config.PACKAGE_VERSION);
-            return 0;
-        }
-
-        if (files != null && files.length != 1) {
-            print (_("Specify a single file\n"));
-            return 1;
-        }
-
-        Platform.init ();
         Audio.init ();
         var file_name = files == null ? null : files[0];
         if (file_name != null && file_name.has_prefix ("file://")) {
@@ -67,9 +38,6 @@ namespace Spek {
             } catch (ConvertError e) {
             }
         }
-        var window = new Window (file_name);
-        Gtk.main ();
-        window.destroy ();
         return 0;
     }
 }
 \ No newline at end of file