Blame glib/glib/goption.h

Packit db3073
/* goption.h - Option parser
Packit db3073
 *
Packit db3073
 *  Copyright (C) 2004  Anders Carlsson <andersca@gnome.org>
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Library General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit db3073
 * Library General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Library General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
Packit db3073
#error "Only <glib.h> can be included directly."
Packit db3073
#endif
Packit db3073
Packit db3073
#ifndef __G_OPTION_H__
Packit db3073
#define __G_OPTION_H__
Packit db3073
Packit db3073
#include <glib/gerror.h>
Packit db3073
#include <glib/gquark.h>
Packit db3073
Packit db3073
G_BEGIN_DECLS
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionContext:
Packit db3073
 * 
Packit db3073
 * A <structname>GOptionContext</structname> struct defines which options
Packit db3073
 * are accepted by the commandline option parser. The struct has only private 
Packit db3073
 * fields and should not be directly accessed.
Packit db3073
 */
Packit db3073
typedef struct _GOptionContext GOptionContext;
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionGroup:
Packit db3073
 *
Packit db3073
 * A <structname>GOptionGroup</structname> struct defines the options in a single
Packit db3073
 * group. The struct has only private fields and should not be directly accessed.
Packit db3073
 *
Packit db3073
 * All options in a group share the same translation function. Libraries which
Packit db3073
 * need to parse commandline options are expected to provide a function for
Packit db3073
 * getting a <structname>GOptionGroup</structname> holding their options, which
Packit db3073
 * the application can then add to its #GOptionContext.
Packit db3073
 */
Packit db3073
typedef struct _GOptionGroup   GOptionGroup;
Packit db3073
typedef struct _GOptionEntry   GOptionEntry;
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionFlags:
Packit db3073
 * @G_OPTION_FLAG_HIDDEN: The option doesn't appear in <option>--help</option>
Packit db3073
 *  output.
Packit db3073
 * @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
Packit db3073
 *  <option>--help</option> output, even if it is defined in a group.
Packit db3073
 * @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this flag
Packit db3073
 *  indicates that the sense of the option is reversed.
Packit db3073
 * @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind,
Packit db3073
 *  this flag indicates that the callback does not take any argument
Packit db3073
 *  (like a %G_OPTION_ARG_NONE option). Since 2.8
Packit db3073
 * @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK
Packit db3073
 *  kind, this flag indicates that the argument should be passed to the
Packit db3073
 *  callback in the GLib filename encoding rather than UTF-8. Since 2.8
Packit db3073
 * @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK 
Packit db3073
 *  kind, this flag indicates that the argument supply is optional. If no argument
Packit db3073
 *  is given then data of %GOptionParseFunc will be set to NULL. Since 2.8
Packit db3073
 * @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict resolution
Packit db3073
 *  which prefixes long option names with <literal>groupname-</literal> if 
Packit db3073
 *  there is a conflict. This option should only be used in situations where
Packit db3073
 *  aliasing is necessary to model some legacy commandline interface. It is
Packit db3073
 *  not safe to use this option, unless all option groups are under your 
Packit db3073
 *  direct control. Since 2.8.
Packit db3073
 * 
Packit db3073
 * Flags which modify individual options.
Packit db3073
 */
Packit db3073
typedef enum
Packit db3073
{
Packit db3073
  G_OPTION_FLAG_HIDDEN		= 1 << 0,
Packit db3073
  G_OPTION_FLAG_IN_MAIN		= 1 << 1,
Packit db3073
  G_OPTION_FLAG_REVERSE		= 1 << 2,
Packit db3073
  G_OPTION_FLAG_NO_ARG		= 1 << 3,
Packit db3073
  G_OPTION_FLAG_FILENAME	= 1 << 4,
Packit db3073
  G_OPTION_FLAG_OPTIONAL_ARG    = 1 << 5,
Packit db3073
  G_OPTION_FLAG_NOALIAS	        = 1 << 6
Packit db3073
} GOptionFlags;
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionArg:
Packit db3073
 * @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags.
Packit db3073
 * @G_OPTION_ARG_STRING: The option takes a string argument.
Packit db3073
 * @G_OPTION_ARG_INT: The option takes an integer argument.
Packit db3073
 * @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the
Packit db3073
 *  extra argument.
Packit db3073
 * @G_OPTION_ARG_FILENAME: The option takes a filename as argument.
Packit db3073
 * @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple
Packit db3073
 *  uses of the option are collected into an array of strings.
Packit db3073
 * @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument, 
Packit db3073
 *  multiple uses of the option are collected into an array of strings.
Packit db3073
 * @G_OPTION_ARG_DOUBLE: The option takes a double argument. The argument
Packit db3073
 *  can be formatted either for the user's locale or for the "C" locale. Since 2.12
Packit db3073
 * @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like %G_OPTION_ARG_INT
Packit db3073
 *  but for larger numbers. The number can be in decimal base, or in hexadecimal
Packit db3073
 *  (when prefixed with <literal>0x</literal>, for example, <literal>0xffffffff</literal>).
Packit db3073
 *  Since 2.12
Packit db3073
 * 
Packit db3073
 * The #GOptionArg enum values determine which type of extra argument the
Packit db3073
 * options expect to find. If an option expects an extra argument, it
Packit db3073
 * can be specified in several ways; with a short option:
Packit db3073
 * <option>-x arg</option>, with a long option: <option>--name arg</option>
Packit db3073
 * or combined in a single argument: <option>--name=arg</option>.
Packit db3073
 */
Packit db3073
typedef enum
Packit db3073
{
Packit db3073
  G_OPTION_ARG_NONE,
Packit db3073
  G_OPTION_ARG_STRING,
Packit db3073
  G_OPTION_ARG_INT,
Packit db3073
  G_OPTION_ARG_CALLBACK,
Packit db3073
  G_OPTION_ARG_FILENAME,
Packit db3073
  G_OPTION_ARG_STRING_ARRAY,
Packit db3073
  G_OPTION_ARG_FILENAME_ARRAY,
Packit db3073
  G_OPTION_ARG_DOUBLE,
Packit db3073
  G_OPTION_ARG_INT64
Packit db3073
} GOptionArg;
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionArgFunc:
Packit db3073
 * @option_name: The name of the option being parsed. This will be either a 
Packit db3073
 *  single dash followed by a single letter (for a short name) or two dashes
Packit db3073
 *  followed by a long option name.
Packit db3073
 * @value: The value to be parsed.
Packit db3073
 * @data: User data added to the #GOptionGroup containing the option when it
Packit db3073
 *  was created with g_option_group_new()
Packit db3073
 * @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED
Packit db3073
 *  is intended to be used for errors in #GOptionArgFunc callbacks.
Packit db3073
 * 
Packit db3073
 * The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK
Packit db3073
 * options.
Packit db3073
 * 
Packit db3073
 * Returns: %TRUE if the option was successfully parsed, %FALSE if an error 
Packit db3073
 *  occurred, in which case @error should be set with g_set_error()
Packit db3073
 */
Packit db3073
typedef gboolean (*GOptionArgFunc) (const gchar    *option_name,
Packit db3073
				    const gchar    *value,
Packit db3073
				    gpointer        data,
Packit db3073
				    GError        **error);
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionParseFunc:
Packit db3073
 * @context: The active #GOptionContext
Packit db3073
 * @group: The group to which the function belongs
Packit db3073
 * @data: User data added to the #GOptionGroup containing the option when it
Packit db3073
 *  was created with g_option_group_new()
Packit db3073
 * @error: A return location for error details
Packit db3073
 * 
Packit db3073
 * The type of function that can be called before and after parsing. 
Packit db3073
 * 
Packit db3073
 * Returns: %TRUE if the function completed successfully, %FALSE if an error 
Packit db3073
 *  occurred, in which case @error should be set with g_set_error()
Packit db3073
 */
Packit db3073
typedef gboolean (*GOptionParseFunc) (GOptionContext *context,
Packit db3073
				      GOptionGroup   *group,
Packit db3073
				      gpointer	      data,
Packit db3073
				      GError        **error);
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionErrorFunc:
Packit db3073
 * @context: The active #GOptionContext
Packit db3073
 * @group: The group to which the function belongs
Packit db3073
 * @data: User data added to the #GOptionGroup containing the option when it
Packit db3073
 *  was created with g_option_group_new()
Packit db3073
 * @error: The #GError containing details about the parse error
Packit db3073
 * 
Packit db3073
 * The type of function to be used as callback when a parse error occurs.
Packit db3073
 */
Packit db3073
typedef void (*GOptionErrorFunc) (GOptionContext *context,
Packit db3073
				  GOptionGroup   *group,
Packit db3073
				  gpointer        data,
Packit db3073
				  GError        **error);
Packit db3073
Packit db3073
/**
Packit db3073
 * G_OPTION_ERROR:
Packit db3073
 * 
Packit db3073
 * Error domain for option parsing. Errors in this domain will
Packit db3073
 * be from the #GOptionError enumeration. See #GError for information on 
Packit db3073
 * error domains.
Packit db3073
 */
Packit db3073
#define G_OPTION_ERROR (g_option_error_quark ())
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionError:
Packit db3073
 * @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser.
Packit db3073
 *  This error will only be reported, if the parser hasn't been instructed
Packit db3073
 *  to ignore unknown options, see g_option_context_set_ignore_unknown_options().
Packit db3073
 * @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed.
Packit db3073
 * @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed.
Packit db3073
 * 
Packit db3073
 * Error codes returned by option parsing.
Packit db3073
 */
Packit db3073
typedef enum
Packit db3073
{
Packit db3073
  G_OPTION_ERROR_UNKNOWN_OPTION,
Packit db3073
  G_OPTION_ERROR_BAD_VALUE,
Packit db3073
  G_OPTION_ERROR_FAILED
Packit db3073
} GOptionError;
Packit db3073
Packit db3073
GQuark g_option_error_quark (void);
Packit db3073
Packit db3073
/**
Packit db3073
 * GOptionEntry:
Packit db3073
 * @long_name: The long name of an option can be used to specify it
Packit db3073
 *  in a commandline as --<replaceable>long_name</replaceable>. Every
Packit db3073
 *  option must have a long name. To resolve conflicts if multiple
Packit db3073
 *  option groups contain the same long name, it is also possible to
Packit db3073
 *  specify the option as 
Packit db3073
 *  --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>.
Packit db3073
 * @short_name: If an option has a short name, it can be specified
Packit db3073
 *  -<replaceable>short_name</replaceable> in a commandline. @short_name must be 
Packit db3073
 *  a printable ASCII character different from '-', or zero if the option has no
Packit db3073
 *  short name.
Packit db3073
 * @flags: Flags from #GOptionFlags.
Packit db3073
 * @arg: The type of the option, as a #GOptionArg.
Packit db3073
 * @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must 
Packit db3073
 *  point to a #GOptionArgFunc callback function, which will be called to handle 
Packit db3073
 *  the extra argument. Otherwise, @arg_data is a pointer to a location to store 
Packit db3073
 *  the value, the required type of the location depends on the @arg type:
Packit db3073
 *  <variablelist>
Packit db3073
 *  <varlistentry>
Packit db3073
 *  <term>%G_OPTION_ARG_NONE</term>
Packit db3073
 *  <listitem><para>%gboolean</para></listitem>
Packit db3073
 *  </varlistentry>
Packit db3073
 *  <varlistentry>
Packit db3073
 *  <term>%G_OPTION_ARG_STRING</term>
Packit db3073
 *  <listitem><para>%gchar*</para></listitem>
Packit db3073
 *  </varlistentry>
Packit db3073
 *  <varlistentry>
Packit db3073
 *  <term>%G_OPTION_ARG_INT</term>
Packit db3073
 *  <listitem><para>%gint</para></listitem>
Packit db3073
 *  </varlistentry>
Packit db3073
 *  <varlistentry>
Packit db3073
 *  <term>%G_OPTION_ARG_FILENAME</term>
Packit db3073
 *  <listitem><para>%gchar*</para></listitem>
Packit db3073
 *  </varlistentry>
Packit db3073
 *  <varlistentry>
Packit db3073
 *  <term>%G_OPTION_ARG_STRING_ARRAY</term>
Packit db3073
 *  <listitem><para>%gchar**</para></listitem>
Packit db3073
 *  </varlistentry>
Packit db3073
 *  <varlistentry>
Packit db3073
 *  <term>%G_OPTION_ARG_FILENAME_ARRAY</term>
Packit db3073
 *  <listitem><para>%gchar**</para></listitem>
Packit db3073
 *  </varlistentry>
Packit db3073
 *  <varlistentry>
Packit db3073
 *  <term>%G_OPTION_ARG_DOUBLE</term>
Packit db3073
 *  <listitem><para>%gdouble</para></listitem>
Packit db3073
 *  </varlistentry>
Packit db3073
 *  </variablelist>
Packit db3073
 *  If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME the location
Packit db3073
 *  will contain a newly allocated string if the option was given. That string
Packit db3073
 *  needs to be freed by the callee using g_free(). Likewise if @arg type is
Packit db3073
 *  %G_OPTION_ARG_STRING_ARRAY or %G_OPTION_ARG_FILENAME_ARRAY, the data should
Packit db3073
 *  be freed using g_strfreev().
Packit db3073
 * @description: the description for the option in <option>--help</option>
Packit db3073
 *  output. The @description is translated using the @translate_func of the
Packit db3073
 *  group, see g_option_group_set_translation_domain().
Packit db3073
 * @arg_description: The placeholder to use for the extra argument parsed
Packit db3073
 *  by the option in <option>--help</option>
Packit db3073
 *  output. The @arg_description is translated using the @translate_func of the
Packit db3073
 *  group, see g_option_group_set_translation_domain().
Packit db3073
 * 
Packit db3073
 * A <structname>GOptionEntry</structname> defines a single option.
Packit db3073
 * To have an effect, they must be added to a #GOptionGroup with
Packit db3073
 * g_option_context_add_main_entries() or g_option_group_add_entries().
Packit db3073
 */
Packit db3073
struct _GOptionEntry
Packit db3073
{
Packit db3073
  const gchar *long_name;
Packit db3073
  gchar        short_name;
Packit db3073
  gint         flags;
Packit db3073
Packit db3073
  GOptionArg   arg;
Packit db3073
  gpointer     arg_data;
Packit db3073
  
Packit db3073
  const gchar *description;
Packit db3073
  const gchar *arg_description;
Packit db3073
};
Packit db3073
Packit db3073
/**
Packit db3073
 * G_OPTION_REMAINING:
Packit db3073
 * 
Packit db3073
 * If a long option in the main group has this name, it is not treated as a 
Packit db3073
 * regular option. Instead it collects all non-option arguments which would
Packit db3073
 * otherwise be left in <literal>argv</literal>. The option must be of type
Packit db3073
 * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
Packit db3073
 * or %G_OPTION_ARG_FILENAME_ARRAY.
Packit db3073
 * 
Packit db3073
 * 
Packit db3073
 * Using #G_OPTION_REMAINING instead of simply scanning <literal>argv</literal>
Packit db3073
 * for leftover arguments has the advantage that GOption takes care of 
Packit db3073
 * necessary encoding conversions for strings or filenames.
Packit db3073
 * 
Packit db3073
 * Since: 2.6
Packit db3073
 */
Packit db3073
#define G_OPTION_REMAINING ""
Packit db3073
Packit db3073
GOptionContext *g_option_context_new              (const gchar         *parameter_string);
Packit db3073
void            g_option_context_set_summary      (GOptionContext      *context,
Packit db3073
                                                   const gchar         *summary);
Packit db3073
const gchar *   g_option_context_get_summary      (GOptionContext     *context);
Packit db3073
void            g_option_context_set_description  (GOptionContext      *context,
Packit db3073
                                                   const gchar         *description);
Packit db3073
const gchar *   g_option_context_get_description  (GOptionContext     *context);
Packit db3073
void            g_option_context_free             (GOptionContext      *context);
Packit db3073
void		g_option_context_set_help_enabled (GOptionContext      *context,
Packit db3073
						   gboolean		help_enabled);
Packit db3073
gboolean	g_option_context_get_help_enabled (GOptionContext      *context);
Packit db3073
void		g_option_context_set_ignore_unknown_options (GOptionContext *context,
Packit db3073
							     gboolean	     ignore_unknown);
Packit db3073
gboolean        g_option_context_get_ignore_unknown_options (GOptionContext *context);
Packit db3073
Packit db3073
void            g_option_context_add_main_entries (GOptionContext      *context,
Packit db3073
						   const GOptionEntry  *entries,
Packit db3073
						   const gchar         *translation_domain);
Packit db3073
gboolean        g_option_context_parse            (GOptionContext      *context,
Packit db3073
						   gint                *argc,
Packit db3073
						   gchar             ***argv,
Packit db3073
						   GError             **error);
Packit db3073
void            g_option_context_set_translate_func (GOptionContext     *context,
Packit db3073
						     GTranslateFunc      func,
Packit db3073
						     gpointer            data,
Packit db3073
						     GDestroyNotify      destroy_notify);
Packit db3073
void            g_option_context_set_translation_domain (GOptionContext  *context,
Packit db3073
							 const gchar     *domain);
Packit db3073
Packit db3073
void            g_option_context_add_group      (GOptionContext *context,
Packit db3073
						 GOptionGroup   *group);
Packit db3073
void          g_option_context_set_main_group (GOptionContext *context,
Packit db3073
					       GOptionGroup   *group);
Packit db3073
GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
Packit db3073
gchar        *g_option_context_get_help       (GOptionContext *context,
Packit db3073
                                               gboolean        main_help,
Packit db3073
                                               GOptionGroup   *group);
Packit db3073
Packit db3073
GOptionGroup *g_option_group_new                    (const gchar        *name,
Packit db3073
						     const gchar        *description,
Packit db3073
						     const gchar        *help_description,
Packit db3073
						     gpointer            user_data,
Packit db3073
						     GDestroyNotify      destroy);
Packit db3073
void	      g_option_group_set_parse_hooks	    (GOptionGroup       *group,
Packit db3073
						     GOptionParseFunc    pre_parse_func,
Packit db3073
						     GOptionParseFunc	 post_parse_func);
Packit db3073
void	      g_option_group_set_error_hook	    (GOptionGroup       *group,
Packit db3073
						     GOptionErrorFunc	 error_func);
Packit db3073
void          g_option_group_free                   (GOptionGroup       *group);
Packit db3073
void          g_option_group_add_entries            (GOptionGroup       *group,
Packit db3073
						     const GOptionEntry *entries);
Packit db3073
void          g_option_group_set_translate_func     (GOptionGroup       *group,
Packit db3073
						     GTranslateFunc      func,
Packit db3073
						     gpointer            data,
Packit db3073
						     GDestroyNotify      destroy_notify);
Packit db3073
void          g_option_group_set_translation_domain (GOptionGroup       *group,
Packit db3073
						     const gchar        *domain);
Packit db3073
Packit db3073
G_END_DECLS
Packit db3073
Packit db3073
#endif /* __G_OPTION_H__ */