Blame glib/goption.h

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