Listing 3

#include "mgetopt.h"
int main( int argc, char** argv)
{
    const char* p;
    int ind, delay;

    /* Define short and longshort option strings with help text */
    char* short_opts ="\
{Usage: example3 [option]...\n}                             \
    a           { -a                    process all\n}      \
    b           { -b                    use binary mode\n}  \
    d@          { -d SEC                delay in seconds\n} \
";
    char* longshort_opts ="\
    verbose     { -v, --verbose         set verbose mode\n} \
    file:       { -f, --file=NAME       use file\n}     \
    help        { -h, --help -h         print help\n}       \
";
   
    /* Parse command line. Upon failure print "BAD" and "HELP" options. */
    if( ! mgetopt_parse( short_opts, 0, longshort_opts, argc, argv)) {
        fprintf( stderr, "%s: mgetopt_parse: failed, bad_opt(s) : %s\n",
             mgetopt( "NAME"), mgetopt( "BAD"));
        fprintf( stderr, "%s\n", mgetopt( "HELP"));
        return 1;
    }
    ind =atoi( mgetopt( "IND"));
    
    /* Print help in case no arguments are entered or option 'help'
     * is entered */
    if( ind ==1 || mgetopt( "help")) {
        fprintf( stderr, "%s\n", mgetopt( "HELP"));
        return 0;
    }
    
    /* Check if the short options are entered */
    if( p =mgetopt( "a")) printf( "a=%s\n", p);
    if( p =mgetopt( "b")) printf( "b=%s\n", p);
    if( p =mgetopt( "d")) printf( "d=%d\n", atoi( p));
    
    /* Check if the longshort options are entered */
    if( p =mgetopt( "verbose")) printf( "verbose=%s\n", p);
    if( p =mgetopt( "file")) printf( "file=%s\n", p);
     
    /* Print remaining non-option arguments */
    for( ; ind < argc; ind++) {
        printf( "argv[%d]=%s\n", ind, argv[ind]);
    }
    return 0;
}