title: GTKmm 3: Parse command line with Gtk::Application
body: <p>I'm trying to use GTK's argv-handling, but there seem to be some issues with the main loop.</p>
  
  <p>My goal is to parse the command line after GTK removed its options (like <code>--display</code>), but before opening a window, because I want my app to be usable with a CLI-only interface, too, with both variants making use of Glib etc. This is why I'm trying to open the window in the <code>command_line</code> signal handler.</p>
  
  <p>This works as expected, exits when the window is closed.</p>
  
  <pre><code>#include &lt;gtkmm.h&gt;
  int main(int argc, char **argv) {
      auto app = Gtk::Application::create(argc, argv, "my.app");
      Gtk::ApplicationWindow win;
      return app-&gt;run(win);
  }
  </code></pre>
  
  <p>But simply adding the <code>HANDLES_COMMAND_LINE</code> flag destroys that: The window is never shown.</p>
  
  <pre><code>#include &lt;gtkmm.h&gt;
  int on_cmd(const Glib::RefPtr&lt;Gio::ApplicationCommandLine&gt; &amp;) {
      return 0;
  }
  int main(int argc, char **argv) {
      auto app = Gtk::Application::create(argc, argv, "my.app",
                   Gio::APPLICATION_HANDLES_COMMAND_LINE);
      app-&gt;signal_command_line().connect(sigc::ptr_fun(on_cmd), false);
      Gtk::ApplicationWindow win;
      return app-&gt;run(win);
  }
  </code></pre>
  
  <p>So I figured that the <code>command_line</code> handler isn't actually suppposed to return? But the documentation says <code>run</code> starts the main loop. I haven't found a method that simply waits for the main loop to finish, so I crank it manually. The window is being shown again, but of course the loop continues once it's closed, which is the least problem with that code:</p>
  
  <pre><code>#include &lt;gtkmm.h&gt;
  int on_cmd(const Glib::RefPtr&lt;Gio::ApplicationCommandLine&gt; &amp;,
             Glib::RefPtr&lt;Gtk::Application&gt; &amp;app) {
      Gtk::ApplicationWindow win(app);
      // app-&gt;run(win); --- lands here again -&gt; stack overflow.
      win.show();
      // This looks very wrong but seems to work?!
      while(true)
          Glib::MainContext::get_default()-&gt;iteration(true);
      // never reach this
      return 0;
  }
  int main(int argc, char **argv) {
      auto app = Gtk::Application::create(argc, argv, "my.app",
                   Gio::APPLICATION_HANDLES_COMMAND_LINE);
      app-&gt;signal_command_line().connect(
        sigc::bind(sigc::ptr_fun(on_cmd), app), false);
      return app-&gt;run();
  }
  </code></pre>
  
  <p>(gtkmm-3.0 version 3.5.13)</p>