diff options
author | Kjell Ahlstedt <kjell.ahlstedt@bredband.net> | 2011-02-14 16:24:27 +0100 |
---|---|---|
committer | Murray Cumming <murrayc@murrayc.com> | 2011-02-15 12:41:59 +0100 |
commit | 94a1f3d568decbde9c3fab84c34db5741ffcad62 (patch) | |
tree | 7b25ba17b002e06ea7d8642a4ebe61cfe3038be9 /examples/options | |
parent | efa5be64159016d2b9cc4ac8d1cc5a6176703ce7 (diff) | |
download | glibmm-94a1f3d568decbde9c3fab84c34db5741ffcad62.tar.gz |
OptionGroup: Add add_entry() that takes a slot with callback function.
* glib/src/optiongroup.[hg|ccg]: Add add_entry() and add_entry_filename()
that take a slot. Add protected option_arg_callback().
An exception thrown by on_pre_parse() or on_post_parse() is propagated
to the error argument of g_callback_pre_parse() or post_parse_callback().
* glib/src/optionentry.hg: Add description of set_flags().
* examples/options/main.cc: Add more OptionEntries and callback functions
for parsing command option values.
Bug 589197 (Hubert Figuiere)
Diffstat (limited to 'examples/options')
-rw-r--r-- | examples/options/main.cc | 94 |
1 files changed, 91 insertions, 3 deletions
diff --git a/examples/options/main.cc b/examples/options/main.cc index 0ba5372b..ee524dac 100644 --- a/examples/options/main.cc +++ b/examples/options/main.cc @@ -28,7 +28,12 @@ public: virtual bool on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group); virtual bool on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group); virtual void on_error(Glib::OptionContext& context, Glib::OptionGroup& group); - + + bool on_option_arg_string(const Glib::ustring& option_name, + const Glib::ustring& value, bool has_value); + bool on_option_arg_filename(const Glib::ustring& option_name, + const std::string& value, bool has_value); + //These members should live as long as the OptionGroup to which they are added, //and as long as the OptionContext to which that OptionGroup is added. int m_arg_foo; @@ -37,6 +42,8 @@ public: bool m_arg_boolean; Glib::OptionGroup::vecustrings m_arg_list; Glib::OptionGroup::vecustrings m_remaining_list; + Glib::ustring m_arg_x_string; + std::string m_arg_x_filename; }; ExampleOptionGroup::ExampleOptionGroup() @@ -73,6 +80,22 @@ ExampleOptionGroup::ExampleOptionGroup() entry5.set_description("A List"); add_entry(entry5, m_arg_list); + Glib::OptionEntry entry6; + entry6.set_long_name("x-string"); + entry6.set_short_name('x'); + entry6.set_description("A string with custom parsing"); + entry6.set_flags(Glib::OptionEntry::FLAG_OPTIONAL_ARG); + m_arg_x_string = "not specified"; + add_entry(entry6, sigc::mem_fun(*this, &ExampleOptionGroup::on_option_arg_string)); + + Glib::OptionEntry entry7; + entry7.set_long_name("x-filename"); + entry7.set_short_name('X'); + entry7.set_description("A filename with custom parsing"); + entry7.set_flags(Glib::OptionEntry::FLAG_OPTIONAL_ARG); + m_arg_x_filename = "not specified"; + add_entry_filename(entry7, sigc::mem_fun(*this, &ExampleOptionGroup::on_option_arg_filename)); + Glib::OptionEntry entry_remaining; entry_remaining.set_long_name(G_OPTION_REMAINING); entry_remaining.set_arg_description(G_OPTION_REMAINING); @@ -103,6 +126,66 @@ void ExampleOptionGroup::on_error(Glib::OptionContext& /* context */, Glib::Opti std::cout << "on_error called" << std::endl; } +bool ExampleOptionGroup::on_option_arg_string(const Glib::ustring& option_name, + const Glib::ustring& value, bool has_value) +{ + if(option_name != "-x" && option_name != "--x-string") + { + m_arg_x_string = "on_option_arg_string called with unexpected option_name: " + option_name; + throw Glib::OptionError(Glib::OptionError::UNKNOWN_OPTION, m_arg_x_string); + } + + if(!has_value) + { + m_arg_x_string = "no value"; + return true; + } + + if(value.empty()) + { + m_arg_x_string = "empty string"; + return true; + } + + m_arg_x_string = value; + if(value == "error") + { + throw Glib::OptionError(Glib::OptionError::BAD_VALUE, + "on_option_arg_string called with value = " + m_arg_x_string); + } + return value != "false"; +} + +bool ExampleOptionGroup::on_option_arg_filename(const Glib::ustring& option_name, + const std::string& value, bool has_value) +{ + if(option_name != "-X" && option_name != "--x-filename") + { + m_arg_x_filename = "on_option_arg_filename called with unexpected option_name: " + option_name; + throw Glib::OptionError(Glib::OptionError::UNKNOWN_OPTION, m_arg_x_filename); + } + + if(!has_value) + { + m_arg_x_filename = "no value"; + return true; + } + + if(value.empty()) + { + m_arg_x_filename = "empty string"; + return true; + } + + m_arg_x_filename = value; + if(value == "error") + { + throw Glib::OptionError(Glib::OptionError::BAD_VALUE, + "on_option_arg_filename called with value = " + m_arg_x_filename); + } + return value != "false"; +} + int main(int argc, char** argv) { @@ -111,7 +194,10 @@ int main(int argc, char** argv) //./example --help Glib::init(); - + + //Set up the current locale. + setlocale(LC_ALL, ""); + Glib::OptionContext context; ExampleOptionGroup group; @@ -130,7 +216,9 @@ int main(int argc, char** argv) " foo = " << group.m_arg_foo << std::endl << " filename = " << group.m_arg_filename << std::endl << " activate_something = " << (group.m_arg_boolean ? "enabled" : "disabled") << std::endl << - " goo = " << group.m_arg_goo << std::endl; + " goo = " << group.m_arg_goo << std::endl << + " x-string = " << group.m_arg_x_string << std::endl << + " x-filename = " << group.m_arg_x_filename << std::endl; //This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b std::cout << " list = "; |