1 //$Id$ -*- c++ -*-
2
3 /* gtkmm example Copyright (C) 2002 gtkmm development team
4 *
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.
8 *
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.
13 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <gtkmm.h>
20 #include <iomanip>
21 #include <iostream>
22
23
24 class ExampleOptionGroup : public Glib::OptionGroup
25 {
26 public:
27 ExampleOptionGroup();
28
29 bool on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group) override;
30 bool on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group) override;
31 void on_error(Glib::OptionContext& context, Glib::OptionGroup& group) override;
32
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.
35 int m_arg_foo;
36 std::string m_arg_filename;
37 Glib::ustring m_arg_goo;
38 bool m_arg_boolean;
39 Glib::OptionGroup::vecustrings m_arg_list;
40 };
41
ExampleOptionGroup()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)
45 {
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);
51
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);
57
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);
63
64 Glib::OptionEntry entry4;
65 entry4.set_long_name("activate_something");
66 entry4.set_description("Activate something");
67 add_entry(entry4, m_arg_boolean);
68
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);
74 }
75
on_pre_parse(Glib::OptionContext & context,Glib::OptionGroup & group)76 bool ExampleOptionGroup::on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
77 {
78 //This is called before the m_arg_* instances are given their values.
79 return Glib::OptionGroup::on_pre_parse(context, group);
80 }
81
on_post_parse(Glib::OptionContext & context,Glib::OptionGroup & group)82 bool ExampleOptionGroup::on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
83 {
84 //This is called after the m_arg_* instances are given their values.
85 return Glib::OptionGroup::on_post_parse(context, group);
86 }
87
on_error(Glib::OptionContext & context,Glib::OptionGroup & group)88 void ExampleOptionGroup::on_error(Glib::OptionContext& context, Glib::OptionGroup& group)
89 {
90 Glib::OptionGroup::on_error(context, group);
91 }
92
main(int argc,char * argv[])93 int main(int argc, char *argv[])
94 {
95 //This example should be executed like so:
96 //./example --foo=1 --bar=2 --goo=abc
97 //./example --help
98
99 Glib::OptionContext context;
100
101 ExampleOptionGroup group;
102 context.set_main_group(group);
103
104 try
105 {
106 Glib::RefPtr<Gtk::Application> app =
107 Gtk::Application::create(argc, argv);
108
109 //Here we can see the parsed values of our custom command-line arguments:
110
111 std::cout << "parsed values: " << std::endl <<
112 " foo = " << group.m_arg_foo << std::endl <<
113 " filename = " << group.m_arg_filename << std::endl <<
114 " activate_something = " << (group.m_arg_boolean ? "enabled" : "disabled") << std::endl <<
115 " goo = " << group.m_arg_goo << std::endl;
116
117 //This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b
118 std::cout << " list = ";
119 for(auto str : group.m_arg_list)
120 {
121 std::cout << str << ", ";
122 }
123 std::cout << std::endl;
124
125
126 //Any standard GTK+ command-line arguments will have an effect on this window:
127 //Try --name="bobble" to change the window's title to "bobble", for instance.
128 Gtk::Window testWindow;
129 return app->run(testWindow); //Shows the window and returns when it is closed.
130 }
131 catch(const Glib::Error& ex)
132 {
133 std::cerr << "Exception: " << ex.what() << std::endl;
134 }
135
136 return 0;
137 }
138