1 //$Id: main.cc 616 2006-05-11 11:40:25Z murrayc $ -*- c++ -*-
3 /* gtkmm example Copyright (C) 2002 gtkmm development team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 class ExampleOptionGroup : public Glib::OptionGroup
29 virtual bool on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
30 virtual bool on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
31 virtual void on_error(Glib::OptionContext& context, Glib::OptionGroup& group);
33 //These int instances should live as long as the OptionGroup to which they are added,
34 //and as long as the OptionContext to which those OptionGroups are added.
36 std::string m_arg_filename;
37 Glib::ustring m_arg_goo;
39 Glib::OptionGroup::vecustrings m_arg_list;
42 ExampleOptionGroup::ExampleOptionGroup()
43 : Glib::OptionGroup("example_group", "description of example group", "help description of example group"),
44 m_arg_foo(0), m_arg_boolean(false)
46 Glib::OptionEntry entry1;
47 entry1.set_long_name("foo");
48 entry1.set_short_name('f');
49 entry1.set_description("The Foo");
50 add_entry(entry1, m_arg_foo);
52 Glib::OptionEntry entry2;
53 entry2.set_long_name("file");
54 entry2.set_short_name('F');
55 entry2.set_description("The Filename");
56 add_entry_filename(entry2, m_arg_filename);
58 Glib::OptionEntry entry3;
59 entry3.set_long_name("goo");
60 entry3.set_short_name('g');
61 entry3.set_description("The Goo");
62 add_entry(entry3, m_arg_goo);
64 Glib::OptionEntry entry4;
65 entry4.set_long_name("activate_something");
66 entry4.set_description("Activate something");
67 add_entry(entry4, m_arg_boolean);
69 Glib::OptionEntry entry5;
70 entry5.set_long_name("list");
71 entry5.set_short_name('l');
72 entry5.set_description("The List");
73 add_entry(entry5, m_arg_list);
76 bool ExampleOptionGroup::on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
78 //This is called before the m_arg_* instances are given their values.
79 return Glib::OptionGroup::on_pre_parse(context, group);
82 bool ExampleOptionGroup::on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
84 //This is called after the m_arg_* instances are given their values.
85 return Glib::OptionGroup::on_post_parse(context, group);
88 void ExampleOptionGroup::on_error(Glib::OptionContext& context, Glib::OptionGroup& group)
90 Glib::OptionGroup::on_error(context, group);
93 int main(int argc, char *argv[])
95 //This example should be executed like so:
96 //./example --foo=1 --bar=2 --goo=abc
99 Glib::OptionContext context;
101 ExampleOptionGroup group;
102 context.set_main_group(group);
104 #ifdef GLIBMM_EXCEPTIONS_ENABLED
107 #endif //GLIBMM_EXCEPTIONS_ENABLED
108 Gtk::Main main_instance(argc, argv, context);
110 //Here we can see the parsed values of our custom command-line arguments:
112 std::cout << "parsed values: " << std::endl <<
113 " foo = " << group.m_arg_foo << std::endl <<
114 " filename = " << group.m_arg_filename << std::endl <<
115 " activate_something = " << (group.m_arg_boolean ? "enabled" : "disabled") << std::endl <<
116 " goo = " << group.m_arg_goo << std::endl;
118 //This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b
119 std::cout << " list = ";
120 for(Glib::OptionGroup::vecustrings::const_iterator iter = group.m_arg_list.begin(); iter != group.m_arg_list.end(); ++iter)
122 std::cout << *iter << ", ";
124 std::cout << std::endl;
127 //Any standard GTK+ command-line arguments will have an effect on this window:
128 //Try --name="bobble" to change the window's title to "bobble", for instance.
129 Gtk::Window testWindow;
130 main_instance.run(testWindow); //Shows the window and returns when it is closed.
131 #ifdef GLIBMM_EXCEPTIONS_ENABLED
133 catch(const Glib::Error& ex)
135 std::cout << "Exception: " << ex.what() << std::endl;
137 #endif //GLIBMM_EXCEPTIONS_ENABLED