home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume35 / getlongopt / part01 / README.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-21  |  5.5 KB  |  156 lines

  1. /* $Id: README.cc,v 1.1 1993/01/02 19:11:54 mano Exp mano $ */
  2. /* S Manoharan. Advanced Computer Research Institute. Lyon. France */
  3.  
  4. /*
  5.  
  6. Yes. Yet another GetLongOpt. What's special here?
  7.  
  8. GetLongOpt supports long options. In fact, there is no support for
  9. explicit short options. For example, -a -b *cannot* be shortened
  10. to -ab. However, long options can be abbreviated as long as there
  11. is no ambiguity. An ambiguity is resolved by using the last option
  12. in the sequence of options (we will come to this later).
  13. If an option requires a value, then the value should be separated
  14. from the option either by whitespace  or by a "=".
  15.  
  16. Other features:
  17. o    GetLongOpt can be used to parse options given through environments.
  18. o    GetLongOpt provides a usage function to print usage.
  19. o    Flags & options with optional or mandatory values are supported.
  20. o    The option marker ('-' in Unix) can be customized.
  21. o    Parsing of command line returns optind (see getopt(3)).
  22. o    Descriptive error messages.
  23.  
  24. Let's take a walk through the usage. 
  25. */
  26.  
  27.  
  28. #include "GetLongOpt.h"
  29. #include <iostream.h>
  30. #include <stdlib.h>
  31.  
  32. int debug_index = 0;
  33.  
  34. int
  35. main(int argc, char **argv)
  36. {
  37.    GetLongOpt option;
  38. // Constructor for GetLongOpt takes an optional argument: the option
  39. // marker. If unspecified, this defaults to '-', the standard (?)
  40. // Unix option marker. For example, a DOS addict may want to have
  41. // "GetLongOpt option('/');" instead!!
  42.  
  43.    char *scid = "a.out version 1.0 dated 21.01.1993";
  44.  
  45.    option.usage("[options and args]");
  46.  
  47. // GetLongOpt::usage is overloaded. If passed a string "s", it sets the
  48. // internal usage string to "s". Otherwise it simply prints the
  49. // command usage. More on it in a while.
  50.  
  51.    option.enroll("help", GetLongOpt::NoValue,
  52.       "print this option summary", 0);
  53.    option.enroll("version", GetLongOpt::NoValue,
  54.       "print the version", 0);
  55.    option.enroll("output", GetLongOpt::MandatoryValue,
  56.       "print output in file $val", "a.out.output");
  57.    option.enroll("verify", GetLongOpt::NoValue,
  58.       "verify if ambiguities are resolved as they should be", "");
  59. #ifdef DEBUG
  60.    option.enroll("debug", GetLongOpt::MandatoryValue,
  61.       "set debug level to $val", "0");
  62. #endif DEBUG
  63.  
  64. // GetLongOpt::enroll adds option specifications to its internal
  65. // database. The first argument is the option sting. The second
  66. // is an enum saying if the option is a flag (GetLongOpt::NoValue),
  67. // if it requires a mandatory value (GetLongOpt::MandatoryValue) or
  68. // if it takes an optional value (GetLongOpt::OptionalValue).
  69. // The third argument is a string giving a brief description of
  70. // the option. This description will be used by GetLongOpt::usage.
  71. // GetLongOpt, for usage-printing, uses $val to represent values
  72. // needed by the options. <$val> is a mandatory value and [$val]
  73. // is an optional value. The final argument to GetLongOpt::enroll
  74. // is the default string to be returned if the option is not
  75. // specified. For flags (options with NoValue), use "" (empty
  76. // string, or in fact any arbitrary string) for specifying TRUE
  77. // and 0 (null pointer) to specify FALSE.
  78.  
  79. // Usage is printed with GetLongOpt::usage. The options and their 
  80. // descriptions (as specified during enroll) are printed in the
  81. // order they are enrolled.
  82.  
  83.    if ( option.parse(getenv("A_OUT"), "A_OUT") < 1 )
  84.       return -1;
  85.  
  86. // GetLongOpt::parse is overloaded. It can either parse a string of
  87. // options (typically given from the environment), or it can parse
  88. // the command line args (argc, argv). In either case a return
  89. // value < 1 represents a parse error. Appropriate error messages
  90. // are printed when errors are seen. GetLongOpt::parse, in its first
  91. // form, takes two strings: the first one is the string to be
  92. // parsed and the second one is a string to be prefixed to the
  93. // parse errors. In ts second form, GetLongOpt::parse returns the
  94. // the optind (see getopt(3)) if parsing is successful.
  95.  
  96.    int optind = option.parse(argc, argv);
  97.    if ( optind < 1 )
  98.        return -1;
  99.  
  100.    const char *outfile = option.retrieve("output");
  101.  
  102. #ifdef DEBUG
  103.    debug_index = atoi(option.retrieve("debug"));
  104. #endif DEBUG
  105.  
  106.    if ( option.retrieve("help") ) {
  107.       option.usage();
  108.       return 0;
  109.    }
  110.    if ( option.retrieve("version") ) {
  111.       cout << scid << "\n";
  112.       return 0;
  113.    }
  114.    if ( option.retrieve("verify") ) {
  115.       cout << "verify turned on by default" << "\n";
  116.    }
  117.    else {
  118.       cout << "verify turned off" << "\n";
  119.    }
  120.  
  121. // The values of the options that are enrolled in the database
  122. // can be retrieved using GetLongOpt::retrieve. This returns a string
  123. // and this string should be converted to whatever type you want.
  124. // See atoi, atof, atol etc. I suppose you would do a "parse" before
  125. // retrieving. Otherwise all you would get are the default values
  126. // you gave while enrolling!
  127. // Ambiguities while retrieving (may happen when options are
  128. // abbreviated) are resolved by taking the matching option that 
  129. // was enrolled last. For example, -v will expand to -verify.
  130.  
  131.    
  132.    for ( ; optind < argc; ++optind ) {
  133.    } /* process all the arguments here */
  134.  
  135.    option.retrieve("foo");
  136.  
  137. // If you try to retrieve something you didn't enroll, you will
  138. // get a warning message. If you had made a typo somewhere while
  139. // enrolling or retrieving, now is the time to correct it.
  140.  
  141.    return 0;
  142. }
  143.  
  144. /*
  145.  
  146. I tested GetLongOpt on gcc 2.3.3 and cfront 2.1 on Sun4s. It worked.
  147. (Therefore, it works on all C++ compilers and all machines! :-))
  148.  
  149. S Manoharan                                 Email    : mano@acri.fr
  150. Advanced Computer Research Institute        Fax      : +33 72 35 84 10
  151. 1 Boulevard Marius Vivier-Merle             Voice    : +33 72 35 80 44
  152. 69443 Lyon Cedex 03 France            
  153.  
  154. */
  155.  
  156.