summaryrefslogtreecommitdiff
path: root/glib/src/optioncontext.hg
blob: bc31f15dcc13bf610d3ef324cedcb2c05e19832d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* 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, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

_DEFS(glibmm,glib)

#include <glibmm/optionentry.h>
#include <glibmm/optiongroup.h>
#include <glibmm/error.h>
#include <sigc++/signal.h>

#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C" { typedef struct _GOptionContext GOptionContext; }
#endif


namespace Glib
{

/** Exception class for options.
 */
_WRAP_GERROR(OptionError, GOptionError, G_OPTION_ERROR, NO_GTYPE)

/** An OptionContext defines which options are accepted by the commandline option parser.
 */
class OptionContext
{
  _CLASS_GENERIC(OptionContext, GOptionContext)
  _IGNORE(g_option_context_free)
public:

  /** Creates a new option context.
   * @param parameter_string A string which is displayed in the first line of --help output, after programname [OPTION...]
   */
  explicit OptionContext(const Glib::ustring& parameter_string = Glib::ustring());

  //Note that, unlike Glib::Wrap(), this would create a second C++ instance for the same C instance,
  //so it should be used carefully. For instance you could not access data in a derived class via this second instance.
  explicit OptionContext(GOptionContext* castitem, bool take_ownership = false);
  virtual ~OptionContext();

  _WRAP_METHOD(void set_help_enabled(bool help_enabled = true), g_option_context_set_help_enabled)
  _WRAP_METHOD(bool get_help_enabled() const, g_option_context_get_help_enabled)
  _WRAP_METHOD(void set_ignore_unknown_options(bool ignore_unknown = true), g_option_context_set_ignore_unknown_options)
  _WRAP_METHOD(bool get_ignore_unknown_options() const, g_option_context_get_ignore_unknown_options)

#m4 _CONVERSION(`char**&',`gchar***',`&($3)')
  _WRAP_METHOD(bool parse(int& argc, char**& argv), g_option_context_parse, errthrow)

  //g_option_context_add_main_entries(), just creates a group internally, adds them to it, and does a set_main_group()
  //- a group without callbacks seems to do some simple default parsing.
  _IGNORE(g_option_context_add_main_entries)

  /** Adds an OptionGroup to the context, so that parsing with context will recognize the options in the group.
   * Note that the group will not be copied, so it should exist for as long as the context exists.
   *
   * @param group The group to add.
   */
  void add_group(OptionGroup& group);
  _IGNORE(g_option_context_add_group)

  /** Sets an OptionGroup as the main group of the context. This has the same effect as calling add_group(), the only
   * difference is that the options in the main group are treated differently when generating --help output.
   * Note that the group will not be copied, so it should exist for as long as the context exists.
   *
   * @param group The group to add.
   */
  void set_main_group(OptionGroup& group);
  _IGNORE(g_option_context_set_main_group)

  //We don't need this (hopefully), and the memory management would be really awkward.
  //OptionGroup& get_main_group();
  //const OptionGroup& get_main_group() const;
  _IGNORE(g_option_context_get_main_group)

  #m4 _CONVERSION(`const OptionGroup&',`GOptionGroup*',`const_cast<GOptionGroup*>(($3).gobj())')
  _WRAP_METHOD(Glib::ustring get_help(bool main_help, const OptionGroup& group) const, g_option_context_get_help)

  /** Returns a formatted, translated help text for the given context.
   * To obtain the text produced by --help, call get_help (true).
   * To obtain the text produced by --help-all, call get_help(false).
   * To obtain the help text for an option group, call get_help(false, group).
   *
   * @param main_help If true, only include the main group.
   * @result string containing the help text.
   */
  Glib::ustring get_help(bool main_help = true) const;

  GOptionContext*       gobj()       { return gobject_; }
  const GOptionContext* gobj() const { return gobject_; }

  _WRAP_METHOD(void set_summary(const Glib::ustring& summary), g_option_context_set_summary)
  _WRAP_METHOD(Glib::ustring get_summary() const, g_option_context_get_summary)
  _WRAP_METHOD(void set_description(const Glib::ustring& description), g_option_context_set_description)
  _WRAP_METHOD(Glib::ustring get_description() const, g_option_context_get_description)

  _WRAP_METHOD(void set_translation_domain(const Glib::ustring& domain), g_option_context_set_translation_domain)

  /**
   * This function is used to translate user-visible strings, for --help output.
   * The function takes an untranslated string and returns a translated string
   */
  typedef sigc::slot<Glib::ustring, const Glib::ustring&> SlotTranslate;

  /**
   * Sets the function which is used to translate user-visible
   * strings, for --help output.  Different groups can use different functions.
   *
   * If you are using gettext(), you only need to set the translation domain,
   * see set_translation_domain().
   *
   * @newin{2,14}
   */
  void set_translate_func (const SlotTranslate& slot);
  _IGNORE(g_option_context_set_translate_func)

protected:

  GOptionContext* gobject_;
  bool has_ownership_;
};


} // namespace Glib