glibmm 2.82.0
options/main.cc

An example with Glib::OptionContext, Glib::OptionGroup and Glib::OptionEntry.

/* Copyright (C) 2004 The glibmm Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glibmm.h>
#include <iomanip>
#include <iostream>
class ExampleOptionGroup : public Glib::OptionGroup
{
public:
ExampleOptionGroup();
private:
bool on_pre_parse(Glib::OptionContext& context) override;
bool on_post_parse(Glib::OptionContext& context) override;
void on_error(Glib::OptionContext& context, const Glib::Error& error) override;
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);
public:
// 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;
std::string m_arg_filename;
Glib::ustring m_arg_goo;
bool m_arg_boolean;
Glib::ustring m_arg_x_string;
Glib::ustring m_arg_x_filename;
};
ExampleOptionGroup::ExampleOptionGroup()
: Glib::OptionGroup(
"example_group", "description of example group", "help description of example group"),
m_arg_foo(0),
m_arg_boolean(false)
{
entry1.set_long_name("foo");
entry1.set_short_name('f');
entry1.set_description("The Foo");
add_entry(entry1, m_arg_foo);
entry2.set_long_name("file");
entry2.set_short_name('F');
entry2.set_description("The Filename");
add_entry_filename(entry2, m_arg_filename);
entry3.set_long_name("goo");
entry3.set_short_name('g');
entry3.set_description("The Goo");
// We can choose a default to be used if the user doesn't specify
// this option.
m_arg_goo = "default-goo-value";
add_entry(entry3, m_arg_goo);
entry4.set_long_name("activate_something");
entry4.set_description("Activate something");
add_entry(entry4, m_arg_boolean);
entry5.set_long_name("list");
entry5.set_short_name('l');
entry5.set_description("A List");
add_entry(entry5, m_arg_list);
entry6.set_long_name("x-string");
entry6.set_short_name('x');
entry6.set_description("A string with custom parsing");
m_arg_x_string = "not specified";
add_entry(entry6, sigc::mem_fun(*this, &ExampleOptionGroup::on_option_arg_string));
entry7.set_long_name("x-filename");
entry7.set_short_name('X');
entry7.set_description("A filename with custom parsing");
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);
add_entry(entry_remaining, m_remaining_list);
}
bool
ExampleOptionGroup::on_pre_parse(Glib::OptionContext& /* context */)
{
// This is called before the m_arg_* instances are given their values.
// You do not need to override this method. This is just here to show you how,
// in case you want to do any extra processing.
std::cout << "on_pre_parse called" << std::endl;
return true;
}
bool
ExampleOptionGroup::on_post_parse(
Glib::OptionContext& /* context */)
{
// This is called after the m_arg_* instances are given their values.
// You do not need to override this method. This is just here to show you how,
// in case you want to do any extra processing.
std::cout << "on_post_parse called" << std::endl;
return true;
}
void
ExampleOptionGroup::on_error(Glib::OptionContext& /* context */, const Glib::Error& /* error */)
{
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;
}
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")
{
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;
}
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")
{
"on_option_arg_filename called with value = " + m_arg_x_filename);
}
return value != "false";
}
int
main(int argc, char** argv)
{
// This example should be executed like so:
//./example --foo=1 --activate_something --goo=abc
//./example --help
ExampleOptionGroup group;
context.set_main_group(group);
try
{
context.parse(argc, argv);
}
catch (const Glib::Error& ex)
{
std::cout << "Exception: " << ex.what() << std::endl;
}
std::cout << "parsed values: " << std::endl
<< " foo = " << group.m_arg_foo << std::endl
<< " filename = " << group.m_arg_filename << std::endl
<< " activate_something = " << (group.m_arg_boolean ? "enabled" : "disabled")
<< " 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 = ";
for (const auto& i : group.m_arg_list)
{
std::cout << i << ", ";
}
// This one shows the remaining arguments on the command line, which had no name= form:
std::cout << " remaining = ";
for (const auto& i : group.m_remaining_list)
{
std::cout << i << ", ";
}
return 0;
}
basic_string< char > string
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cout
Definition error.h:29
const char * what() const noexcept override
An OptionContext defines and parses commandline options, using OptionGroups and option entries .
Definition optioncontext.h:127
bool parse(int &argc, char **&argv)
Parses the command line arguments, recognizing options which have been added to context.
void set_main_group(OptionGroup &group)
Sets an OptionGroup as the main group of the context.
An OptionEntry defines a single option.
Definition optionentry.h:46
void set_long_name(const Glib::ustring &value)
void set_description(const Glib::ustring &value)
void set_flags(const Flags &value)
Set one or more OptionEntry::Flags.
void set_short_name(const gchar &value)
Exception class for options.
Definition optioncontext.h:41
@ UNKNOWN_OPTION
An option was not known to the parser.
Definition optioncontext.h:58
@ BAD_VALUE
A value couldn't be parsed.
Definition optioncontext.h:59
An OptionGroup defines the options in a single group.
Definition optiongroup.h:58
virtual void on_error(OptionContext &context, const Error &error)
virtual bool on_post_parse(OptionContext &context)
virtual bool on_pre_parse(OptionContext &context)
Glib::ustring has much the same interface as std::string, but contains Unicode characters encoded as ...
Definition ustring.h:336
bool empty() const
Returns true if the string is empty.
@ OPTIONAL_ARG
For options of the G_OPTION_ARG_CALLBACK kind, this flag indicates that the argument supply is option...
Definition containerhandle_shared.h:36
void init()
Initialize glibmm.