TITLE: One way to process a command line PROBLEM: terry@uivlsisd.csl.uiuc.edu (Terry Lee), 9 May 94 Is getopt() standardized? If so, what should be the type of its second argument? I've encountered one compiler (IBM xlC) that prototypes the second argument as const char**. This is a problem because then I can't pass in argv because conversion from char** to const char** isn't safe. Is there no portable way of using getopt()? Thanks for any help. RESPONSE: kanze@us-es.sel.de (James Kanze) I don't believe that getopt is standard. I don't remember having seen it in the ISO C standard. So if you use it, counting on the system to provide it, your code is not portable. But do you really want `getopt'? I've recently been experimenting with another approach, which looks very promessing. Basically, I have an Option class, with a derived class for each of the different option types (BooleanOption, NumericOption, etc.). There is also a class ProgArgs, which maintains a static list of the options; the constructor of Option registers itself with ProgArgs. The constructor for ProgArgs takes argc and argv as parameters, and iterates over the args, removing any options from the list and calling the `set' function (a virtual function of Option) for the corresponding option. In addition, the derived option classes have conversion operators for the correct type, e.g.: BooleanOption::operator bool(), NumericOption::operator int(), etc. It is easier to show the use than to explain: static BooleanOption bOpt( 'b' ) ; static NumericOption nOpt( 'n' ) ; int main( int argc , char* argv[] ) { ProgArgs a( argc , argv ) ; if ( bOpt ) { for ( int i = 0 ; i < nOpt ; i ++ ) // ... There's obviously more to it than that, but you get the general idea. But it makes adding an option *really* easy. (Maybe too easy for good interface design:-). [...] (The option classes and ProgArgs, above, work together to parse the command line. The programmer doesn't write a program, nor even call a function. He just declares a few variables to statically describe the possible options; the variables themselves do the rest.) [...]