FRAG * frag_init(frag_option * fops, int argc, char ** argv, int flags);
int frag_parse(FRAG * frag);
const char * frag_err(FRAG * frag);
void frag_usage(FRAG * frag);
void frag_free(FRAG * frag);
typedef struct {
signed int id; /* the id-number */
char chr; /* short option */
char *str; /* long option */
int type; /* type */
char *arg; /* argument name */
char *desc; /* description */
} frag_option;
typedef struct {
int index; /* where are we going */
int chr; /* more specificly */
int id; /* the id-number of the option (or error) */
int type; /* what type the option was (enable or disable) */
/* + some other fields you don't need to know or care about */
} FRAG;
This man-page is meant to be a quick referance, the complete manual is in info-format.
chr
This is the short option. At the moment any character is acceptable. Using common sense is recommended though, as for example '-' won't work right.
str
This is the long option. No checking is done for the strings at the moment. Traditionally only alphanumeric characters (usually in lower case) and the dash (to substitute space) are used in long options.
type
This is the type of the option. Zero means that it is a normal option. FRAG_ARG means it takes an argument. FRAG_OPT_ARG means it takes an optional argument. FRAG_NEG means it is negatable. If you OR FRAG_HIDDEN with any of the above, the option won't show in the help message. If FRAG_ALIAS is OR'd with them, the option is printed as if it was just another alternative of the previous option (description is discarded).
arg
This is used in the help message to describe what kind of argument the option takes. If the option takes an argument and this is NULL, "VALUE" is used instead. Brackets are automatically used for optional arguments.
desc
This is used in the help message to describe what the option does. Long lines are automatically word-wrapped. Any tabs or newlines are printed as well, so you can do some rudimentary formatting if you think it's necessary.
FRAG_END_ARRAY
To end the array, use FRAG_END_ARRAY. FRAG_END_ARRAY covers all fields. If you do not use FRAG_END_ARRAY, frag-opt will segfault.
FRAG_DISABLE_DOUBLEDASH
Disables the behaviour where all arguments after a "--" are interpreted to be arguments for the program.
FRAG_DISABLE_CLUSTERS
Disables clusters of short options like "-xzv".
FRAG_DISABLE_EQUALS_LONG
Disables the "--option=ARGUMENT" way of giving arguments to options.
FRAG_ENABLE_SPACED_LONG
Enables the "--option ARGUMENT" way of giving arguments to options.
FRAG_DISABLE_SPACED_SHORT
Disables the "-o ARGUMENT" way of giving arguments to options.
FRAG_ENABLE_NO_SPACE_SHORT
Enables the "-oARGUMENT" way of giving arguments to options.
FRAG_DISABLE_LONG_OPTIONS
Disables long options altogether.
FRAG_DISABLE_SHORT_OPTIONS
Disables short options altogether.
FRAG_DISABLE_NEGATION_OPTIONS
Disables negation options altogether (both long and short).
FRAG_ENABLE_ONEDASH_LONG
Long options can be used with "-option". Consider disabling clusters if using this, since the "-utc" cluster could be interpreted as the long option "-utc". If the options are designed so that no such overlapping is possible, then there is no danger.
FRAG_QUIET
Does not print error-messages. An alternative way of getting a description is using frag_err(), whic returns a simpler, constant string.
#include <stdio.h>
#include <stdlib.h>
#include <frag-opt.h>
int main(int argc, char ** argv)
{
int recursion = 0;
int depth = 0;
char *end;
FRAG *frag;
frag_option fops[] = {
{ 0, FRAG_PROGRAM, "[FILES]", "an example program" },
{ 1, 'h', "help", 0 NULL, "print this message" },
{ 1, 'u', "usage", FRAG_ALIAS, NULL, NULL },
{ 2, 'v', "version", 0, NULL, "print version info" },
{ 3, 'r', "recursive", FRAG_NEG, NULL, "search recursively" },
{ 4, 'd', "depth", FRAG_ARG, "DEPTH", "depth of recursion" },
{ FRAG_END_ARRAY }
};
frag = frag_init(fops, argc, argv, FRAG_ENABLE_SPACED_LONG
| FRAG_ENABLE_NO_SPACE_SHORT);
if (frag == NULL) {
printf("out of memory\n");
exit(EXIT_FAILURE);
}
while (frag_parse(frag)) {
switch (frag->id) {
case 0:
printf("arg: %s\n", frag->arg);
break;
case 1:
frag_usage(frag);
frag_free(frag);
exit(EXIT_SUCCESS);
case 2:
printf("example version 1.0.0\n");
frag_free(frag);
exit(EXIT_SUCCESS);
case 3:
recursion = frag->type;
break;
case 4:
depth = atoi(frag->arg);
if (depth < 0) {
printf("depth must be a positive integer\n");
frag_free(frag);
exit(EXIT_FAILURE);
} else if (depth == 0)
recursion = 0;
else
recursion = 1;
break;
default: /* an error occured */
frag_free(frag);
frag_usage(frag);
exit(EXIT_FAILURE);
}
}
frag_free(frag);
printf("recursion is %d, depth is %d\n", recursion, depth);
return 0;
}
Of course a real program would check that the argument actually is a number and use a function like strtol(3) to convert it to a number.