1 /* Copyright (C) 2004 The glibmm Development Team
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 class ExampleOptionGroup : public Glib::OptionGroup
28 virtual bool on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
29 virtual bool on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
30 virtual void on_error(Glib::OptionContext& context, Glib::OptionGroup& group);
32 //These int instances should live as long as the OptionGroup to which they are added,
33 //and as long as the OptionContext to which those OptionGroups are added.
35 std::string m_arg_filename;
36 Glib::ustring m_arg_goo;
38 Glib::OptionGroup::vecustrings m_arg_list;
41 ExampleOptionGroup::ExampleOptionGroup()
42 : Glib::OptionGroup("example_group", "description of example group", "help description of example group"),
43 m_arg_foo(0), m_arg_boolean(false)
45 Glib::OptionEntry entry1;
46 entry1.set_long_name("foo");
47 entry1.set_short_name('f');
48 entry1.set_description("The Foo");
49 add_entry(entry1, m_arg_foo);
51 Glib::OptionEntry entry2;
52 entry2.set_long_name("file");
53 entry2.set_short_name('F');
54 entry2.set_description("The Filename");
55 add_entry_filename(entry2, m_arg_filename);
57 Glib::OptionEntry entry3;
58 entry3.set_long_name("goo");
59 entry3.set_short_name('g');
60 entry3.set_description("The Goo");
61 m_arg_goo = "default-goo-value"; //We can choose a default to be used if the user doesn't specify this option.
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 // You do not need to override this method. This is just here to show you how,
80 // in case you want to do any extra processing.
81 return Glib::OptionGroup::on_pre_parse(context, group);
84 bool ExampleOptionGroup::on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
86 //This is called after the m_arg_* instances are given their values.
87 // You do not need to override this method. This is just here to show you how,
88 // in case you want to do any extra processing.
89 return Glib::OptionGroup::on_post_parse(context, group);
92 void ExampleOptionGroup::on_error(Glib::OptionContext& context, Glib::OptionGroup& group)
94 Glib::OptionGroup::on_error(context, group);
99 int main(int argc, char** argv)
101 //This example should be executed like so:
102 //./example --foo=1 --bar=2 --goo=abc
107 Glib::OptionContext context;
109 ExampleOptionGroup group;
110 context.set_main_group(group);
112 #ifdef GLIBMM_EXCEPTIONS_ENABLED
115 context.parse(argc, argv);
117 catch(const Glib::Error& ex)
119 std::cout << "Exception: " << ex.what() << std::endl;
122 std::auto_ptr<Glib::Error> ex;
123 context.parse(argc, argv, ex);
126 std::cout << "Exception: " << ex->what() << std::endl;
128 #endif //GLIBMM_EXCEPTIONS_ENABLED
130 std::cout << "parsed values: " << std::endl <<
131 " foo = " << group.m_arg_foo << std::endl <<
132 " filename = " << group.m_arg_filename << std::endl <<
133 " activate_something = " << (group.m_arg_boolean ? "enabled" : "disabled") << std::endl <<
134 " goo = " << group.m_arg_goo << std::endl;
136 //This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b
137 std::cout << " list = ";
138 for(Glib::OptionGroup::vecustrings::const_iterator iter = group.m_arg_list.begin(); iter != group.m_arg_list.end(); ++iter)
140 std::cout << *iter << ", ";
142 std::cout << std::endl;