Skip to main content

optparse.h

C API reference for the header optparse.h.

Public API

Enum: optparse_argtype

Canonical path: optparse.h#enum:optparse_argtype

Declared in: optparse.h

Signature

enum optparse_argtype { OPTPARSE_NONE, OPTPARSE_REQUIRED, OPTPARSE_OPTIONAL };

Function: optparse

Canonical path: optparse.h#optparse

Declared in: optparse.h

Signature

int optparse(struct optparse *options, const char *optstring);

Summary

Read the next option in the argv array.

Behavior

Iterates through the argument vector to identify short options, handles option permutation if enabled, and updates the parser state with the current option character and its argument.

Parameters

  • options: The parser state.
  • optstring: a getopt()-formatted option string.

Returns

  • Return value 1: the next option character, -1 for done, or '?' for error Just like getopt(), a character followed by no colons means no argument. One colon means the option has a required argument. Two colons means the option takes an optional argument.

Function: optparse_arg

Canonical path: optparse.h#optparse_arg

Declared in: optparse.h

Signature

char *optparse_arg(struct optparse *options);

Summary

Used for stepping over non-option arguments.

Behavior

Resets the sub-option index to 0, increments the option index if the current argument is not null, and returns that argument.

Parameters

  • options: The parser state containing the argument vector and current index.

Returns

  • Return value 1: the next non-option argument, or -1 for no more arguments Argument parsing can continue with optparse() after using this function. That would be used to parse the options for the subcommand returned by optparse_arg(). This function allows you to ignore the value of optind.

Function: optparse_init

Canonical path: optparse.h#optparse_init

Declared in: optparse.h

Signature

void optparse_init(struct optparse *options, char **argv);

Summary

Initializes the parser state.

Behavior

Sets the parser state to its initial values, including setting the argument vector, enabling permutation, and initializing indices and error messages.

Parameters

  • options: The parser state to be initialized.
  • argv: The argument vector to be parsed.

Function: optparse_long

Canonical path: optparse.h#optparse_long

Declared in: optparse.h

Signature

int
optparse_long(struct optparse *options,
const struct optparse_long *longopts,
int *longindex);

Summary

Handles GNU-style long options in addition to getopt() options. This works a lot like GNU's getopt_long(). The last option in longopts must be all zeros, marking the end of the array. The longindex argument may be NULL.

Behavior

Parses long options by skipping the double-dash prefix, matching the option name against the provided array, and handling associated arguments based on the specified argument type.

Parameters

  • options: The parser state.
  • longopts: An array of long option definitions, terminated by an entry of all zeros.
  • longindex: An optional pointer to an integer that will be set to the index of the matched long option.

Returns

  • Return value 1: Returns the short option character equivalent, -1 if no more options are found, or '?' if an error occurs.

Struct: optparse

Canonical path: optparse.h#struct:optparse

Declared in: optparse.h

Signature

struct optparse {
char **argv;
int permute;
int optind;
int optopt;
char *optarg;
char errmsg[64];
int subopt;
};

Struct: optparse_long

Canonical path: optparse.h#struct:optparse_long

Declared in: optparse.h

Signature

struct optparse_long {
const char *longname;
int shortname;
enum optparse_argtype argtype;
};