Visual Studio 6Visual C++ 7.0Windows 2000Visual C++ 6.0Windows XPIntermediateDevVisual StudioWindowsC++
Simple command line processing





4.00/5 (8 votes)
Jan 15, 2002

70634

811
A simple method of parsing command line arguments; similar to getopt
Introduction
I wanted a simple method to parse command line parameters that would compile on both Windows and Unix. I was familiar with getopt but I couldn't find a compilable version of it in a standalone version, so I decided to make a simple version of it myself.
The result is something that is something that uses the getopt
format specification
but isn't getopt
. It is basic but that is what I wanted - something quick to
learn, use and debug. This is the result.
Features
- Accepts
getopt
command line specification - Simple
- Platform independent
- Case dependent or independent matching
Limitations
- It isn't
getopt
- All options on the command line must use flags. i.e. "program [-o] input_file" won't work, use "program [-o] -i input_file" instead
Notes
- Uses exceptions
- If using case-insensitive option matching, ensure that the string passed
through as
a_pszOptions
is all lowercase
Example Usage
try { CSimpleOptions cl( argc, argv, "Oa:" ); while ( cl.next() ) { switch ( cl.option() ) { case 'O': bOption = true; break; case 'a': strArgument = cl.arg(); } } } catch ( const SIMPLE_OPTIONS_EXCEPTION & e ) { fprintf( stderr, "Invalid argument: %s\n", e.what() ); return 1; }