[gtkmm-documentation] command_line_handling: handle_local_options(): Correct the return values.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtkmm-documentation] command_line_handling: handle_local_options(): Correct the return values.
- Date: Wed, 9 Sep 2015 15:20:57 +0000 (UTC)
commit 8e44ba1a0de61d718ff903a1849fb2e9bbd05796
Author: Murray Cumming <murrayc murrayc com>
Date: Wed Sep 9 17:18:10 2015 +0200
command_line_handling: handle_local_options(): Correct the return values.
Since glib bug #750796 was fixed, we really need to return a negative
value to keep the program running. Returning EXIT_SUCCESS or EXIT_FAILURE
stops the program.
Add a --version option as an example of when we might want to stop
with EXIT_SUCCESS, and add validation for --goo="ungoo" as an example of
when we might want to stop with EXIT_FAILURE. Otherwise, we return -1
to keep going.
Bug #754763
.../command_line_handling/exampleapplication.cc | 39 +++++++++++++++++++-
1 files changed, 37 insertions(+), 2 deletions(-)
---
diff --git a/examples/book/application/command_line_handling/exampleapplication.cc
b/examples/book/application/command_line_handling/exampleapplication.cc
index f8e4245..4e90cc5 100644
--- a/examples/book/application/command_line_handling/exampleapplication.cc
+++ b/examples/book/application/command_line_handling/exampleapplication.cc
@@ -41,6 +41,10 @@ ExampleApplication::ExampleApplication()
//An int.
add_main_option_entry(Gio::Application::OPTION_TYPE_INT, "bar", 'b', "The bar to use.", "number");
+ //A bool.
+ add_main_option_entry(Gio::Application::OPTION_TYPE_BOOL, "version", 'v', "Show the application version.");
+
+
//A std::vector<std::string>.
add_main_option_entry(Gio::Application::OPTION_TYPE_FILENAME_VECTOR, G_OPTION_REMAINING);
@@ -161,16 +165,22 @@ int ExampleApplication::on_command_line(const Glib::RefPtr<Gio::ApplicationComma
get_arg_value(options, "hoo", hoo_value);
int bar_value = 0;
get_arg_value(options, "bar", bar_value);
+ bool version_value = false;
+ get_arg_value(options, "version", version_value);
//The remaining filenames:
std::vector<std::string> vec_remaining;
get_arg_value(options, G_OPTION_REMAINING, vec_remaining);
+ //Note that "foo" and "goo" will not be false/empty here because we
+ //handled them in on_handle_local_options() and therefore removed them from
+ //the options VariantDict.
std::cout << "on_command_line(), parsed values: " << std::endl <<
" foo = " << (foo_value ? "true" : "false") << std::endl <<
" goo = " << goo_value << std::endl <<
" hoo = " << hoo_value << std::endl <<
" bar = " << bar_value << std::endl <<
+ " version = " << (version_value ? "true" : "false") << std::endl <<
" remaining =";
for (std::size_t i = 0; i < vec_remaining.size(); ++i)
std::cout << ' ' << vec_remaining[i];
@@ -206,19 +216,44 @@ int ExampleApplication::on_handle_local_options(const Glib::RefPtr<Glib::Variant
get_arg_value(options, "hoo", hoo_value);
int bar_value = 0;
get_arg_value(options, "bar", bar_value);
+ bool version_value = false;
+ get_arg_value(options, "version", version_value);
std::cout << "on_handle_local_options(), parsed values: " << std::endl <<
" foo = " << (foo_value ? "true" : "false") << std::endl <<
" goo = " << goo_value << std::endl <<
" hoo = " << hoo_value << std::endl <<
- " bar = " << bar_value << std::endl;
+ " bar = " << bar_value << std::endl <<
+ " version = " << (version_value ? "true" : "false") << std::endl;
//Remove some options to show that we have handled them in the local instance,
//so they won't be passed to the primary (remote) instance:
options->remove("foo");
options->remove("goo");
- return EXIT_SUCCESS;
+ //If --version was requested,
+ //just output the version number and exit with a success code:
+ if(version_value)
+ {
+ std::cout << "Version: 1.2.3" << std::endl;
+
+ //Any non-negative return value here means stop the program.
+ //The local instance will eventually exit with this status code.
+ return EXIT_SUCCESS;
+ }
+
+ //If the command line parameters were invalid,
+ //complain and exist with a failure code:
+ if(goo_value == "ungoo")
+ {
+ std::cerr << "goo cannot be ungoo." << std::endl;
+
+ //Any non-negative return value here means stop the program.
+ //The local instance will eventually exit with this status code.
+ return EXIT_FAILURE;
+ }
+
+ return -1;
}
bool ExampleApplication::on_option_arg_string(const Glib::ustring& option_name,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]