---
name: "all_opt"
section: "3"
platform: "linux"
package: "lam2-dev"
version: "6.3.2-3"
release: "Debian 2.2 (Potato)"
platforms: ["linux"]
url: "https://new.carta.tech/all_opt"
---

# all\_opt(3)

general purpose command-line options parsing (LAM)

## NAME

all\_opt - general purpose command-line options parsing (LAM)

## SYNOPSIS

```
#include <all_opt.h>
OPT	*ao_init (void);
char	*ao_chosen (OPT *od, const char *opt);
char	*ao_param (OPT *od, const char *opt,
			int inst, int idx);
int	ao_intparam (OPT *od, const char *opt,
			int inst, int idx, int *inum);
int	ao_ninsts (OPT *od, const char *opt);
int	ao_nparams (OPT *od, const char *opt, int inst);
int	ao_ntaken (OPT *od);
int	ao_parse (OPT *od, int *argc, char **argv);
int	ao_setopt (OPT *od, const char *opt,
		const char *mutex, int nparams, int flags);
int	ao_setopt1 (OPT *od, const char *opt,
		const char *mutex, int nparams, int flags);
int	ao_tail (OPT *od, int *tailc, char ***tailv);
int	ao_taken (OPT *od, const char *opt);
void	ao_free (OPT *od);
```

## DESCRIPTION

The *all\_opt* package provides general purpose command-line option parsing. It parses multi-letter option strings with varying numbers of parameters and invocations. Options can be made mandatory as well as grouped into mutually exclusive sets. *all\_opt* can handle multiple command-line parsing invocations by maintaining a separate option descriptor for each one.

An option descriptor is created and initialized by the *ao\_init()* function. It returns a pointer to a new descriptor, typedef *OPT* (defined in \<all\_opt.h\>). This descriptor pointer is used as the first argument to all the other functions. When no longer needed, a descriptor can be destroyed by calling *ao\_free()*.

Once a descriptor is created, the user declares each valid command-line option string by calling *ao\_setopt()*. The function's arguments are: *ad*, the option descriptor; *opt*, the option string being declared; *mutex*, an option string in the set of mutually exclusive options (or NULL if not used); *nparams*, the number of parameters expected to follow this option (or AOVARNUM if variable); and *flags*, a bit-mapped field of flags controlling other characteristics of the option. The *flags* value is constructed by ORing flags from the following list:

- AOINT

  All arguments following the option are integers.

- AOMUST

  The option is mandatory and must be taken. If the option is part of a mutually exclusive set, all options in the set must also have this flag set.

*ao\_setopt1()* is a convenience function that interprets the *opt* argument as a string of single letter options and calls *ao\_setopt()* for each letter with the other arguments given.

The option string "#" is reserved to represent the special case of the "-#" option (i.e. a dash followed by an integer). The integer can be either decimal, octal, or hexadecimal. This option implies that the *AOINT* flag is set and that the *nparams* argument is 1.

After all valid options are declared, the command-line arguments are parsed by calling *ao\_parse()*. The *argc* and *argv* parameters are those passed to main(). Note that *argc* is passed by reference. Option strings in the command-line (prefixed with the dash character '-') and any parameters following them, are parsed and deleted from the *argc*, *argv* structure, leaving in it any additional strings that are neither options nor parameters. Options can be invoked multiple times, *ao\_parse()* maintains a count of these instances as well as potential parameters  for each option. A "--" argument notifies *ao\_parse()* to avoid processing the arguments that follow it. These unprocessed arguments at the tail end of the command-line can be retrieved by calling *ao\_tail()*. *ao\_parse()* checks the command-line for invalid options and parameters, and verifies that mutual exclusion is satisfied and mandatory options  are taken. Once the command-line options are parsed, the user can make queries to check which options were actually taken and what parameters were supplied.

### Option Queries

The function *ao\_ntaken()* returns the number of options taken, i.e. that appeared on the command-line. *ao\_taken()* checks if the given option *opt* was taken, returning 1 (true) or 0 (false). The *ao\_chosen()* function simplifies the task of locating the single option taken in a mutually exclusive set. The set is identified by setting *opt* to any of member options in it. *ao\_chosen()* returns the option taken or NULL if none was chosen.

*ao\_ninsts()* returns the number of times (instances) an option was invoked on the command-line. This number can be helpful if an option accepts parameters. It allows the user to request the different parameters of each instance. Instances are numbered sequentially starting with 0. If the option accepts a variable number of parameters, *ao\_nparams()* returns the number of parameters provided for an option's given instance number *inst*. Parameters are identified by sequential index values starting with 0. If the option takes integer parameters,  *ao\_intparam()* can be used to retrieve a specific parameter given its instance number *inst* and its index value *idx*. The parameter is returned by reference in the *inum* variable. If the option parameters are strings, *ao\_param()* is used to return a specific parameter given its instance and index values.

The tail end of the command-line, formed by all arguments to the right of the "--" special option, can be retrieved by calling IR ao\_tail() . The tail is returned in the standard *argc*, *argv* format through the *tailc* and *tailv* parameters passed by reference.

## EXAMPLE

The following example code demonstrates how *all\_opt* can be used to parse command-lines. The hypothetical tool accepts two mutually exclusive options: "foo", "bar". The "foo" option requires 2 string parameters and can only be invoked once. For simplicity, error checking is not done.

```
#include <all_opt.h>
main(argc, argv)
int		argc;
char		*argv[];
{
	OPT	*ad;
	char	*opt;
	ad = ao_init();
	ao_setopt(ad, "foo", NULL, 2, 0);
	ao_setopt(ad, "bar", "foo", 0, 0);
	ao_parse(ad, &argc, argv);
	opt = ao_chosen(ad, "foo");
	if (strcmp(opt, "foo") == 0) {
		if (ao_ninsts(ad, "foo") > 1) { /* error */ }
		printf("foo chosen: %s %s\n",
			ao_param(ad, "foo", 0, 0),
			ao_param(ad, "foo", 0, 1));
	} else {
		printf("bar taken %d times\n",
			ao_ninsts(ad, "bar"));
	}
	ao_free(ad);
}
```

## RETURN VALUES

In case of an error, *ao\_init()* returns a NULL descriptor, *ao\_chosen()* and *ao\_param()* return NULL strings, and *ao\_intparam()*, *ao\_parse()*, *ao\_setopt()*, *ao\_setopt1()*, and *ao\_tail()* return -1. In addition, the global variable *errno* is set to indicate the error.

## ERRORS

- EUSAGE

  The command-line violates the option rules.

- EBADASCIINUMB

  A string representing an integer has an invalid format.
