checkopt.h

Package: WA2L/edrc 1.5.57
Section: WA2L C Programmer's Manual (3)
Updated: 27 June 2024
Index Return to Main Contents

 

NAME

setopt, checkopt, CHECKOPT - check used command line options

 

SYNOPSIS

#include "checkopt.h"

char *setopt(char opt);

bool checkopt(char *head, int *table);

bool CHECKOPT(char *head, int_1, int_2, ..., int_n);

 

AVAILABILITY

WA2L/edrc

 

DESCRIPTION

checkopt.h provides functionality to efficiently check the correct use of command line switch combinations.

The definition of correct command line option combinations is done by a binary truth table that is more easy to maintain then combinations of logical and/or expressions.

 

setopt()

record all used command line options.

Example:

        #define USAGE "Usage: mycmd [ -l | -m ][ -z ][ -n | -f filename ]\n"

        int opt;
        while (( opt = getopt(argc, argv, "f:nlmz")) != -1) {
                switch (opt) {
                        case 'f':
                                        file = true;
                                        filename = strdup(optarg);
                                        break;
                        case 'n':
                                        file = false;
                                        break;
                        case 'l':
                                        low = true; 
                                        break;
                        case 'm':
                                        medium = true; 
                                        break;
                        case 'z':
                                        zero = true; 
                                        break;
                        case '?':
                                        printf(USAGE);
                                        return 4;
                                        break;
                }
                setopt(opt);
        }
        for (int i=optind; i<argc; i++){
                printf(USAGE);
                return 4;
        }

 

checkopt()

check all options recorded by setopt() against a truth table.

The header is a string with all options to be checked in the truth table.

In the truth table (=integer array) all allowed (good) option combinations are set by setting the related bit in the integer value list in the same order as the options given in the header string.

The last entry in the truth table needs to be set to -1.

Example:

        int table[] = {
                0b00000000,
                0b00000100,
                0b00001000,

                0b00000001,
                0b00000101,
                0b00001001,

                0b00000010,
                0b00000110,
                0b00001010,

                -1
        };

        if ( !checkopt("mlnf", table) ){
                printf(USAGE);
                return 4;
        };

 

CHECKOPT()

check all options recorded by setopt() against a truth table.

The header is a string with all options to be checked in the truth table.

In the truth table (=integer array) all allowed (good) option combinations are set by setting the related bit in the integer value list in the same order as the options given in the header string.

The CHECKOPT macro is suited to more conveniently define the truth table.

Example:

        if ( !CHECKOPT(
                     "mlnf",
                0b00000000,
                0b00000100,
                0b00001000,

                0b00000001,
                0b00000101,
                0b00001001,

                0b00000010,
                0b00000110,
                0b00001010
        )){
                printf(USAGE);
                return 4;
        };

 

RETURN VALUE

the return value of checkopt() and CHECKOPT() is true, if the option combination is correct (respectively found in the truth table) and false if the option combination is not correct (respectively not found in the truth table).

 

ENVIRONMENT

-

 

FILES

lib/$OSID/includes/checkopt.h

 

EXAMPLES

-

 

SEE ALSO

edrcintro(1), checkopt(3), https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html, osid(3), program.h(3), strings.h(3), utility.h(3), wa2lc(3)

 

NOTES

-

 

BUGS

The truth table can handle up to 31 options.

 

AUTHOR

checkopt.h was developed by Christian Walther. Send suggestions and bug reports to wa2l@users.sourceforge.net . 

 

COPYRIGHT

Copyright © 2024 Christian Walther

This is free software; see edrc/doc/COPYING for copying conditions. There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


 

Index

NAME
SYNOPSIS
AVAILABILITY
DESCRIPTION
setopt()
checkopt()
CHECKOPT()
RETURN VALUE
ENVIRONMENT
FILES
EXAMPLES
SEE ALSO
NOTES
BUGS
AUTHOR
COPYRIGHT

This document was created by man2html using the manual pages.
Time: 16:52:33 GMT, August 28, 2024