git/parse-options.h

#ifndef PARSE_OPTIONS_H
#define PARSE_OPTIONS_H

#include "gettext.h"

/**
 * Refer to Documentation/technical/api-parse-options.txt for the API doc.
 */

enum parse_opt_type {};

enum parse_opt_flags {};

enum parse_opt_option_flags {};

enum parse_opt_result {};

struct option;
parse_opt_cb;

struct parse_opt_ctx_t;
parse_opt_ll_cb;

parse_opt_subcommand_fn;

/*
 * `type`::
 *   holds the type of the option, you must have an OPTION_END last in your
 *   array.
 *
 * `short_name`::
 *   the character to use as a short option name, '\0' if none.
 *
 * `long_name`::
 *   the long option (without the leading dashes) or subcommand name,
 *   NULL if none.
 *
 * `value`::
 *   stores pointers to the values to be filled.
 *
 * `argh`::
 *   token to explain the kind of argument this option wants. Does not
 *   begin in capital letter, and does not end with a full stop.
 *   Should be wrapped by N_() for translation.
 *   Is automatically enclosed in brackets when printed, unless it
 *   contains any of the following characters: ()<>[]|
 *   E.g. "name" is shown as "<name>" to indicate that a name value
 *   needs to be supplied, not the literal string "name", but
 *   "<start>,<end>" and "(this|that)" are printed verbatim.
 *
 * `help`::
 *   the short help associated to what the option does.
 *   Must never be NULL (except for OPTION_END and OPTION_SUBCOMMAND).
 *   OPTION_GROUP uses this pointer to store the group header.
 *   Should be wrapped by N_() for translation.
 *
 * `flags`::
 *   mask of parse_opt_option_flags.
 *   PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
 *   PARSE_OPT_NOARG: says that this option does not take an argument
 *   PARSE_OPT_NONEG: says that this option cannot be negated
 *   PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
 *                     shown only in the full usage.
 *   PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
 *				value if no argument is given when the option
 *				is last on the command line. If the option is
 *				not last it will require an argument.
 *				Should not be used with PARSE_OPT_OPTARG.
 *   PARSE_OPT_NODASH: this option doesn't start with a dash; can only be a
 *		       short option and can't accept arguments.
 *   PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
 *				(i.e. '<argh>') in the help message.
 *				Useful for options with multiple parameters.
 *   PARSE_OPT_NOCOMPLETE: by default all visible options are completable
 *			   by git-completion.bash. This option suppresses that.
 *   PARSE_OPT_COMP_ARG: this option forces to git-completion.bash to
 *			 complete an option as --name= not --name even if
 *			 the option takes optional argument.
 *
 * `callback`::
 *   pointer to the callback to use for OPTION_CALLBACK
 *
 * `defval`::
 *   default value to fill (*->value) with for PARSE_OPT_OPTARG.
 *   OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met.
 *   CALLBACKS can use it like they want.
 *
 * `ll_callback`::
 *   pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK
 *
 * `subcommand_fn`::
 *   pointer to a function to use for OPTION_SUBCOMMAND.
 *   It will be put in value when the subcommand is given on the command line.
 */
struct option {};

#define OPT_BIT_F(s, l, v, h, b, f)
#define OPT_COUNTUP_F(s, l, v, h, f)
#define OPT_SET_INT_F(s, l, v, h, i, f)
#define OPT_BOOL_F(s, l, v, h, f)
#define OPT_CALLBACK_F(s, l, v, a, h, f, cb)
#define OPT_STRING_F(s, l, v, a, h, f)
#define OPT_INTEGER_F(s, l, v, h, f)

#define OPT_END()
#define OPT_GROUP(h)
#define OPT_BIT(s, l, v, h, b)
#define OPT_BITOP(s, l, v, h, set, clear)
#define OPT_NEGBIT(s, l, v, h, b)
#define OPT_COUNTUP(s, l, v, h)
#define OPT_SET_INT(s, l, v, h, i)
#define OPT_BOOL(s, l, v, h)
#define OPT_HIDDEN_BOOL(s, l, v, h)
#define OPT_CMDMODE_F(s, l, v, h, i, f)
#define OPT_CMDMODE(s, l, v, h, i)

#define OPT_INTEGER(s, l, v, h)
#define OPT_MAGNITUDE(s, l, v, h)
#define OPT_STRING(s, l, v, a, h)
#define OPT_STRING_LIST(s, l, v, a, h)
#define OPT_STRVEC(s, l, v, a, h)
#define OPT_UYN(s, l, v, h)
#define OPT_EXPIRY_DATE(s, l, v, h)
#define OPT_CALLBACK(s, l, v, a, h, cb)
#define OPT_NUMBER_CALLBACK(v, h, cb)
#define OPT_FILENAME(s, l, v, h)
#define OPT_COLOR_FLAG(s, l, v, h)

#define OPT_NOOP_NOARG(s, l)

#define OPT_ALIAS(s, l, source_long_name)

#define OPT_SUBCOMMAND_F(l, v, fn, f)
#define OPT_SUBCOMMAND(l, v, fn)

/*
 * parse_options() will filter out the processed options and leave the
 * non-option arguments in argv[]. argv0 is assumed program name and
 * skipped.
 *
 * usagestr strings should be marked for translation with N_().
 *
 * Returns the number of arguments left in argv[].
 *
 * In one-shot mode, argv0 is not a program name, argv[] is left
 * untouched and parse_options() returns the number of options
 * processed.
 */
int parse_options(int argc, const char **argv, const char *prefix,
		  const struct option *options,
		  const char * const usagestr[],
		  enum parse_opt_flags flags);

NORETURN void usage_with_options(const char * const *usagestr,
				 const struct option *options);

NORETURN void usage_msg_opt(const char *msg,
			    const char * const *usagestr,
			    const struct option *options);

/**
 * usage_msg_optf() is like usage_msg_opt() except that the first
 * argument is a format string, and optional format arguments follow
 * after the 3rd option.
 */
__attribute__((format (printf,1,4)))
void NORETURN usage_msg_optf(const char *fmt,
			     const char * const *usagestr,
			     const struct option *options, ...);

void die_for_incompatible_opt4(int opt1, const char *opt1_name,
			       int opt2, const char *opt2_name,
			       int opt3, const char *opt3_name,
			       int opt4, const char *opt4_name);


static inline void die_for_incompatible_opt3(int opt1, const char *opt1_name,
					     int opt2, const char *opt2_name,
					     int opt3, const char *opt3_name)
{}

/*
 * Use these assertions for callbacks that expect to be called with NONEG and
 * NOARG respectively, and do not otherwise handle the "unset" and "arg"
 * parameters.
 */
#define BUG_ON_OPT_NEG(unset)
#define BUG_ON_OPT_ARG(arg)

/*
 * Similar to the assertions above, but checks that "arg" is always non-NULL.
 * This assertion also implies BUG_ON_OPT_NEG(), letting you declare both
 * assertions in a single line.
 */
#define BUG_ON_OPT_NEG_NOARG(unset, arg)

/*----- incremental advanced APIs -----*/

struct parse_opt_cmdmode_list;

/*
 * It's okay for the caller to consume argv/argc in the usual way.
 * Other fields of that structure are private to parse-options and should not
 * be modified in any way.
 */
struct parse_opt_ctx_t {};

void parse_options_start(struct parse_opt_ctx_t *ctx,
			 int argc, const char **argv, const char *prefix,
			 const struct option *options,
			 enum parse_opt_flags flags);

enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
					 const struct option *options,
					 const char * const usagestr[]);

int parse_options_end(struct parse_opt_ctx_t *ctx);

struct option *parse_options_dup(const struct option *a);
struct option *parse_options_concat(const struct option *a, const struct option *b);

/*----- some often used options -----*/
int parse_opt_abbrev_cb(const struct option *, const char *, int);
int parse_opt_expiry_date_cb(const struct option *, const char *, int);
int parse_opt_color_flag_cb(const struct option *, const char *, int);
int parse_opt_verbosity_cb(const struct option *, const char *, int);
/* value is struct oid_array* */
int parse_opt_object_name(const struct option *, const char *, int);
/* value is struct object_id* */
int parse_opt_object_id(const struct option *, const char *, int);
int parse_opt_commits(const struct option *, const char *, int);
int parse_opt_commit(const struct option *, const char *, int);
int parse_opt_tertiary(const struct option *, const char *, int);
int parse_opt_string_list(const struct option *, const char *, int);
int parse_opt_strvec(const struct option *, const char *, int);
int parse_opt_noop_cb(const struct option *, const char *, int);
int parse_opt_passthru(const struct option *, const char *, int);
int parse_opt_passthru_argv(const struct option *, const char *, int);
/* value is enum branch_track* */
int parse_opt_tracking_mode(const struct option *, const char *, int);

#define OPT__VERBOSE(var, h)
#define OPT__QUIET(var, h)
#define OPT__VERBOSITY(var)
#define OPT__DRY_RUN(var, h)
#define OPT__FORCE(var, h, f)
#define OPT__ABBREV(var)
#define OPT__SUPER_PREFIX(var)

#define OPT__COLOR(var, h)
#define OPT_COLUMN(s, l, v, h)
#define OPT_PASSTHRU(s, l, v, a, h, f)
#define OPT_PASSTHRU_ARGV(s, l, v, a, h, f)
#define _OPT_CONTAINS_OR_WITH(l, v, h, f)
#define OPT_CONTAINS(v, h)
#define OPT_NO_CONTAINS(v, h)
#define OPT_WITH(v, h)
#define OPT_WITHOUT(v, h)
#define OPT_CLEANUP(v)
#define OPT_PATHSPEC_FROM_FILE(v)
#define OPT_PATHSPEC_FILE_NUL(v)
#define OPT_AUTOSTASH(v)

#define OPT_IPVERSION(v)

#endif