---
name: "parse_opts"
section: "3"
platform: "linux"
package: "ltp-dev"
version: "20091231+dfsg-1"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/parse_opts"
---

# parse\_opts(3)

parse standard and user options for LTP test programs

## NAME

parse\_opts - parse standard and user options for LTP test programs

## SYNOPSIS

```
#include ”test.h”
#include ”usctest.h”

char *parse_opts(int argc, char *argv[],
                 option_t option_array[],
                 void (*user_help_func)());
```

## DESCRIPTION

The **parse\_opts()** routine parses options from the command line, looking for user specified options or standard options (see below). Its arguments *argc* and *argv* are the argument count and array as passed to the main() function on program invocation. User options may be specified in the *option\_array* argument. A help function may be specified in the *user\_help\_func* argument.

*option\_array* is a pointer to the first element of an array of **option\_t**.  If no additional options are needed, pass NULL. **option\_t** is declared in **usctest.h** as

```

typedef struct {
char	*option;
int	*flag;
char	**arg;
} option_t;
```

The meanings of the different fields are:

- *option*

  is a valid option string to be given to getopt().

- *flag*

  is a pointer to an integer location to set true if option is given in *argv*.  This can be NULL if the option doesn't require an argument.

- *arg*

  is a pointer to a character pointer variable that will be set with the option argument if the option is present in argv.  This pointer MUST be provided if the option can take an argument.  Failure to provide a location will cause **parse\_opts()** to return an error.

*user\_help\_func* is a pointer to a function that will be called when the -h option is found. This function should print help messages for the options in *option\_array* to standard out. The standard help messages are formatted such that the option designator starts in column 3 and the description starts in column 11.

## STANDARD OPTIONS

Below is a list of the standard options defined in **parse\_opts()**:

- **-c** *n*

  Run *n* copies of the test in parallel.  This is done by forking *n* times and running the test as usual.  If -i or -I are specified, each process will run for that amount of time.

- **-e**

  Turn on logging all errno's received.  This option is to facilitate security audit testing for MLS.

- **-f**

  Suppresses functional testing messages.

- **-h**

  Print help message.  The standard options will be printed first, then a call to *user\_help\_func()* will be made.

- **-i** *n*

  Run for *n* iterations. A value of 0 makes the test loop infinitely. (default 1)

- **-I** *x*

  The test will loop until *x* seconds have passed. (default 0.0)

- **-p**

  Pause for SIGUSR1 before testing. The test will pause were you place TEST\_PAUSE.  *Warning*: The test will also fork at this point if -c is used.

- **-P** *x*

  This option will do a delay of *x* seconds after each iteration. (default 0.0)

- **-t**

  Produce timing statistics. \*NOT IMPLEMENTED\*

The STD\_\* flags are used by system call test macros defined in usctest.h (see **[usctest(3)](/usctest/3.md)**), or may be used in the user's code.

## RETURN VALUE

**parse\_opts()** returns a NULL pointer upon successful completion.  If an error occurs a pointer to an error message is returned.

## EXAMPLE

The following example defines two options, one with an argument, one without.

```
int fflag, Tflag;	/* binary flags: opt or not */
char *Topt;		/* option arguments */
option_t options[] = {
	{ "F", &fflag, NULL },		/* No argument */
	{ "T:",	&Tflag, &Topt },	/* argument required */
	{ NULL, NULL, NULL }		/* NULL required to end array */
};
void help()
{
	printf("  -F       An option with no argument\n");
	printf("  -T opt   An option that requires an argument\n");
}
int main(int argc, char *argv[])
{
	char *msg;
	if ( (msg=parse_opts(argc, argv, options, &help)) != NULL )
		error_exit(msg);
	return 0;
}
```

The following example shows how to use **parse\_opts()** without defining new options.

```
int main(int argc, char *argv[])
{
	char *msg;
	if ((msg=parse_opts(argc, argv, (option_t *)NULL, NULL)) != NULL)
		error_exit(msg);
	return 0;
}
```

## SEE ALSO

[usctest(3)](/usctest/3.md), [getopt(3)](/getopt/3.md).
