home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / options.h < prev    next >
C/C++ Source or Header  |  1995-04-29  |  2KB  |  93 lines

  1. /*
  2.  *    (c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
  3.  *
  4.  *    Include file for generic option parsing
  5.  */
  6.  
  7. /*
  8.  * To use this routine, you must a table called an Option_Description.
  9.  * Each element in this table describes one possible option:
  10.  *    Its option letter
  11.  *    Its argument type (if any)
  12.  *    Whether an argument is mandatory or optional
  13.  *    The address of the variable holding the option value
  14.  *    The defualt value if argument is optional
  15.  *
  16.  * Example:
  17.  *
  18.  *    A program accepts the following options:
  19.  *        -a    [no value]
  20.  *        -b N    [a numeric value]
  21.  *        -p [N]    [an optional numeric value]
  22.  *        -t S    [a string value]
  23.  *
  24.  * The corresponding option description table would then look like:
  25.  *
  26.  *    #include <options.h>
  27.  *    int a_flg = 1, b_value = 0, p_value = 0;
  28.  *    char *t_string = "default";
  29.  *
  30.  *    Option_Description( options ) {
  31.  *        'a', Bool_Option(a_flg),
  32.  *        'b', Int_Option(b_value),
  33.  *        'p', Int_Option_Optional(p_value, -1),
  34.  *        't', String_Option(t_string),
  35.  *        '\0',
  36.  *     }
  37.  * To parse the argument list - and the contents of the environment variable
  38.  * XXINIT, all that has to be done is to issue the following call:
  39.  *
  40.  *    files = parse_options(argc, argv, "XXINIT", options, NULL);
  41.  *
  42.  * If no environment variable is associated with the program, use NULL as
  43.  * the third parameter.
  44.  *
  45.  * Upon return, the elements argv[1] .. argv[files] will contain
  46.  * the file names (and other 'non-options') that occur in the argument list.
  47.  *
  48.  * The last NULL argument may be replaced by your own 'usage routine'
  49.  * which will be called in the following way:
  50.  *
  51.  *    usage(pname)
  52.  *    char *pname; /+ argv[0] without path +/
  53.  *
  54.  *
  55.  * char *program_name(argv)
  56.  *
  57.  * return a pointer to the last component of argv[0] (the program name with
  58.  * with the path deleted).
  59.  *
  60.  
  61.  */
  62.  
  63. #ifndef _NN_OPTIONS_H
  64. #define _NN_OPTIONS_H 1
  65.  
  66. struct option_descr {
  67.     char    option_letter;
  68.     char    option_type;
  69.     char **    option_address;
  70.     char *    option_default;
  71. } ;
  72.  
  73.  
  74. #define    Option_Description(name) \
  75.     struct option_descr name[] =
  76.  
  77. #define    Bool_Option(addr) \
  78.     1, (char **)(&addr), (char *)0
  79.  
  80. #define    String_Option(addr) \
  81.     2, &addr, (char *)0
  82.  
  83. #define String_Option_Optional(addr, default) \
  84.     3, &addr, default
  85.  
  86. #define    Int_Option(addr) \
  87.     4, (char **)(&addr), (char *)0
  88.  
  89. #define    Int_Option_Optional(addr, default) \
  90.     5, (char **)(&addr), (char *)default
  91.  
  92. #endif /* _NN_OPTIONS_H */
  93.