Add canvas-note.cc that probably shouldn't exist anyway :)
[ardour.git] / libs / glibmm2 / examples / options / main.cc
1 /* Copyright (C) 2004 The glibmm Development Team
2  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <glibmm.h>
19 #include <iomanip>
20 #include <iostream>
21
22
23 class ExampleOptionGroup : public Glib::OptionGroup
24
25 public:
26   ExampleOptionGroup();
27
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);
31   
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.
34   int m_arg_foo;
35   std::string m_arg_filename;
36   Glib::ustring m_arg_goo;
37   bool m_arg_boolean;
38   Glib::OptionGroup::vecustrings m_arg_list;
39 };
40
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)
44 {
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);
50       
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);
56  
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);
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
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   // 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);
82 }
83
84 bool ExampleOptionGroup::on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
85 {
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);
90 }
91
92 void ExampleOptionGroup::on_error(Glib::OptionContext& context, Glib::OptionGroup& group)
93 {
94   Glib::OptionGroup::on_error(context, group);
95 }
96   
97
98
99 int main(int argc, char** argv)
100 {
101   //This example should be executed like so:
102   //./example --foo=1 --bar=2 --goo=abc
103   //./example --help
104   
105   Glib::init();
106    
107   Glib::OptionContext context;
108   
109   ExampleOptionGroup group;
110   context.set_main_group(group);
111   
112   #ifdef GLIBMM_EXCEPTIONS_ENABLED
113   try
114   {
115     context.parse(argc, argv);
116   }
117   catch(const Glib::Error& ex)
118   {
119     std::cout << "Exception: " << ex.what() << std::endl;
120   }
121   #else
122   std::auto_ptr<Glib::Error> ex;
123   context.parse(argc, argv, ex);
124   if(ex.get())
125   {
126     std::cout << "Exception: " << ex->what() << std::endl;
127   }
128   #endif //GLIBMM_EXCEPTIONS_ENABLED
129
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;
135     
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)
139   {
140     std::cout << *iter << ", ";
141   }
142   std::cout << std::endl;
143  
144   return 0;
145 }
146