Hi,

In a search for a small task to be done besides my other assignments, I
had a look at the current option handling and re-implemented it more
versatile in a way, that is handles long options more flexible
(understands GNU-like long options like --dbg or old (non-POSIX
conformant) options like -userdir)

The parsing is implemented in a class OptionParser which provides an
iterator to go through all the options. The iterator returns an Option
object which can be asked for the canonical option and the optional
argument. It works almost like the GNU getopt_long() without being
dependant on it. So now its possible to do something like this

-----------
 OptionParser options(expectedOptions, argc, argv);
 OptionParser::iterator option = options.begin();
 
 while (option != options.end()) {
    switch (options->getOptChar()) {
    case 'd': // -d, -dbg, --dbg
          ...
    case 'u': // -u, -userdir, --userdir
          ...
    }
   ++option
 }
-----------

advantage is, that it behaves like a STL container so its easy to
use and read the code.

This OptionParser _does not_ move elements in the argv[] array, since this
is not save to do (POSIX.2 actually requires that you handle it as 
'char const * const argv[]'), so this is not done (the GUI toolkit does it
anyway, but we should'nt do it..)

My Changes:
  * options are parsed now with the OptionParser with all the features
    (understands short and long options like -d, -dbg, --dbg; option
     arguments need not be separated -u/tmp/foo; easier extension)

  * exit() due to option errors are now exit(1) instead of exit(0)

I tried to document it well and adhere all programming rules in the lyx
project. The change comprises of a patch against the current CVS
(lyx_main.[hC]) and two new added files. Both is available at

  <http://linux.fh-heilbronn.de/~zeller/lyx-devel/lyx-options.patch>
  <http://linux.fh-heilbronn.de/~zeller/lyx-devel/lyx-OptionParser.tar.gz>

ciao,
 -hen

Reply via email to