Monday, October 30, 2006

getopts example

http://www.siit.tu.ac.th/mdailey/class/2003_s2/its225/assignments/testopts.c:

/*
* testopts.c: example usage for getopt command line parsing
* library.
*
* Matt Dailey, Feb 2004
*
* Compile with "gcc testopt.c" to get executable a.out.
*
* This example program takes arguments of the following form:
*
* a.out [-ac] [-b ] arg1 arg2 ...
*
* That is, a.out has three optional options -a, -b, and -c.
* -a and -c are standalone options, and -b is an option that
* takes a string argument.
*
* Valid invocations include:
*
* a.out
* a.out -a
* a.out -c
* a.out -b arg1
* a.out -ac
* a.out -b arg1 -ca
* a.out f1 f2 f3
* a.out -a f1 f2 f3
* a.out -ca f1 f2 f3
* a.out -ca f1 f2 -b arg1 f3
*
* and so on. What's nice is that the options and arguments
* can occur in any order.
*
* Also refer to "man 3 getopt"
*
*/

#include
#include
#include
#include

#define TRUE 1
#define FALSE 0

char *g_pCharProgramName = NULL;

int main( int argc, char *argv[] ) {

extern char *optarg;
extern int optind;
int this_opt;

int bOptA = FALSE;
int bOptB = FALSE;
int bOptC = FALSE;
char *pCharOptB = NULL;

g_pCharProgramName = argv[0];

/* Loop until all arguments are processed. On each pass, we
* call getopt() for the next option */

while ( 1 ) {

/* Get the next option. It is placed in this_opt. We pass
* the user-entered argc/argv and our option string. The
* option string "ab:c" says our program has options -a, -b,
* and -c. The -b option requires an argument (this is
* specified by the colon in 'b:'. */

this_opt = getopt( argc, argv, "ab:c" );

/* If there are no options left, getopt() returns -1. */

if ( this_opt == -1 ) break;

/* Now we have either a valid option or an invalid option.
* Check all possible cases. */

switch ( this_opt ) {
case 'a':

/* The user selected the -a option. Set a bool for later use. */

bOptA = TRUE;
break;

case 'b':

/* This is the -b option. Since we put b: in the getopt
* options string, option b has an argument. Let's save it */

bOptB = TRUE;
pCharOptB = strdup( optarg );
break;

case 'c':

/* -c is another simple option like -a. Just set a flag to
* not that the user selected it. */

bOptC = TRUE;
break;

default:

/* Error case.
*
* getopt() prints an invalid option message for you.
* Usually we now print a usage message. */

printf( "Usage: %s [-ac] [-b ] [arg1] [arg2] ...\n",
g_pCharProgramName );
exit( -1 );
break;
}

}

/* Print out the options selected and not selected */

if ( bOptA ) {
printf( "Option A selected\n" );
} else {
printf( "Option A not selected\n" );
}
if ( bOptB ) {
printf( "Option B selected with argument %s\n", pCharOptB );
} else {
printf( "Option B not selected\n" );
}
if ( bOptC ) {
printf( "Option C selected\n" );
} else {
printf( "Option C not selected\n" );
}

/* Print out the non-option arguments. optind now indexes
* the first non-option argument in argv[]. */

while( optind < argc ) {
printf( "Got non-option argument %s\n", argv[optind] );
optind++;
}

/* Clean up */

if ( pCharOptB != NULL ) free( pCharOptB );
}

No comments: