|
NAMEgetarg , arg_printusage —
collect command line options
SYNOPSIS#include <getarg.h>
int
void
DESCRIPTIONgetarg () collects any command line options given to a
program in an easily used way. arg_printusage ()
pretty-prints the available options, with a short help text.
args is the option specification to use, and it's an array of struct getargs elements. num_args is the size of args (in elements). argc and argv are the argument count and argument vector to extract option from. optind is a pointer to an integer where the index to the last processed argument is stored, it must be initialised to the first index (minus one) to process (normally 0) before the first call. arg_printusage take the same args and num_args as getarg; progname is the name of the program (to be used in the help text), and extra_string is a string to print after the actual options to indicate more arguments. The usefulness of this function is realised only be people who has used programs that has help strings that doesn't match what the code does. The getargs struct has the following elements. struct getargs{ const char *long_name; char short_name; enum { arg_integer, arg_string, arg_flag, arg_negative_flag, arg_strings, arg_double, arg_collect } type; void *value; const char *help; const char *arg_help; }; long_name is the long name of the option, it
can be
Option parsing is similar to what
getopt
uses. Short options without arguments can be compressed
( Long option names are prefixed with -- (double dash), and the
value with a = (equal),
EXAMPLE#include <stdio.h> #include <string.h> #include <getarg.h> char *source = "Ouagadougou"; char *destination; int weight; int include_catalog = 1; int help_flag; struct getargs args[] = { { "source", 's', arg_string, &source, "source of shippment", "city" }, { "destination", 'd', arg_string, &destination, "destination of shippment", "city" }, { "weight", 'w', arg_integer, &weight, "weight of shippment", "tons" }, { "catalog", 'c', arg_negative_flag, &include_catalog, "include product catalog" }, { "help", 'h', arg_flag, &help_flag } }; int num_args = sizeof(args) / sizeof(args[0]); /* number of elements in args */ const char *progname = "ship++"; int main(int argc, char **argv) { int optind = 0; if (getarg(args, num_args, argc, argv, &optind)) { arg_printusage(args, num_args, progname, "stuff..."); exit (1); } if (help_flag) { arg_printusage(args, num_args, progname, "stuff..."); exit (0); } if (destination == NULL) { fprintf(stderr, "%s: must specify destination\n", progname); exit(1); } if (strcmp(source, destination) == 0) { fprintf(stderr, "%s: destination must be different from source\n"); exit(1); } /* include more stuff here ... */ exit(2); } The output help output from this program looks like this: $ ship++ --help Usage: ship++ [--source=city] [-s city] [--destination=city] [-d city] [--weight=tons] [-w tons] [--no-catalog] [-c] [--help] [-h] stuff... -s city, --source=city source of shippment -d city, --destination=city destination of shippment -w tons, --weight=tons weight of shippment -c, --no-catalog include product catalog BUGSIt should be more flexible, so it would be possible to use other more complicated option syntaxes, such as what ps(1), and tar(1), uses, or the AFS model where you can skip the flag names as long as the options come in the correct order.Options with multiple arguments should be handled better. Should be integrated with SL. It's very confusing that the struct you pass in is called getargS. SEE ALSOgetopt(3)
Visit the GSP FreeBSD Man Page Interface. |