(file) Return to getopt.h CVS log (file) (dir) Up to [OMI] / omi / base

  1 mike  1.1 /*
  2           **==============================================================================
  3           **
  4           ** Open Management Infrastructure (OMI)
  5           **
  6           ** Copyright (c) Microsoft Corporation
  7           ** 
  8           ** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  9           ** use this file except in compliance with the License. You may obtain a copy 
 10           ** of the License at 
 11           **
 12           **     http://www.apache.org/licenses/LICENSE-2.0 
 13           **
 14           ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15           ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
 16           ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
 17           ** MERCHANTABLITY OR NON-INFRINGEMENT. 
 18           **
 19           ** See the Apache 2 License for the specific language governing permissions 
 20           ** and limitations under the License.
 21           **
 22 mike  1.1 **==============================================================================
 23           */
 24           
 25           #ifndef _omi_getopt_h
 26           #define _omi_getopt_h
 27           
 28           #include <common.h>
 29           
 30           #define GETOPTSTATE_INITIALIZER { 0, { '\0' }, NULL, { '\0' } }
 31           
 32           #define GETOPT_OPT_SIZE 512
 33           #define GETOPT_ERR_SIZE 512
 34           
 35           BEGIN_EXTERNC
 36           
 37           typedef struct _GetOptState
 38           {
 39               int index;
 40               char opt[GETOPT_OPT_SIZE];
 41               const char* arg;
 42               char err[GETOPT_ERR_SIZE];
 43 mike  1.1 }
 44           GetOptState;
 45           
 46           
 47           /*
 48           ** GetOpt() extracts and removes options (and their arguments if any) from the 
 49           ** argc-argv parameters. The set of expected options are specified by the opts
 50           ** parameter (see example below). Returns 0 if an option is found and sets 
 51           ** state->opt and state->arg. Returns -1 on error and sets state->err. Returns
 52           ** 1 when all arguments are exhausted. After successful processing, no options
 53           ** nor their arguments remain in argc-argv. Here is an example:
 54           **
 55           **     static const char* opts[] =
 56           **     {
 57           **         "-h",
 58           **         "--help",
 59           **         "--output:",
 60           **         NULL,
 61           **     };
 62           **     GetOptState state = GETOPTSTATE_INITIALIZER;
 63           **     int help = 0;
 64 mike  1.1 **     const char* output = NULL;
 65           **
 66           **     for (;;)
 67           **     {
 68           **         int r = GetOpt(&argc, argv, opts, &state);
 69           **
 70           **         if (r == 1)
 71           **             break;
 72           **
 73           **         if (r == 0)
 74           **         {
 75           **             if (strcmp(state.opt, "-h") == 0)
 76           **                 help = 1;
 77           **             else if (strcmp(state.opt, "--help") == 0)
 78           **                 help = 1;
 79           **             else if (strcmp(state.opt, "--output") == 0)
 80           **                 outfile = state.arg;
 81           **         }
 82           **         else if (r == -1)
 83           **         {
 84           **             fprintf(stderr, "error: %s\n", state.err);
 85 mike  1.1 **             break;
 86           **         }
 87           **      }
 88           **
 89           ** Note that the opts[] array contains "--output:". The ':' character indicates
 90           ** that the --output option takes a required argument.
 91           */
 92           int GetOpt(
 93               int* argc, 
 94               const char* argv[], 
 95               const char* opts[],
 96               GetOptState* state);
 97           
 98           END_EXTERNC
 99           
100           #endif /* _omi_getopt_h */

ViewCVS 0.9.2