File: GetOpt.hpp

package info (click to toggle)
vbindiff 3.0-beta4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 588 kB
  • ctags: 370
  • sloc: cpp: 2,296; sh: 787; perl: 108; makefile: 96; lisp: 13
file content (85 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//--------------------------------------------------------------------
// $Id: GetOpt.hpp 4570 2004-07-05 21:57:23Z cjm $
//--------------------------------------------------------------------
//
//   Free GetOpt
//   Copyright 2000 by Christopher J. Madsen
//   See GetOpt.cpp for license information
//
//   Process command line arguments
//
//--------------------------------------------------------------------

#ifndef INCLUDED_GETOPT_HPP
#define INCLUDED_GETOPT_HPP

class GetOpt
{
 public:
  struct Option;
  enum Connection { nextArg, withEquals, adjacent     };
  enum Flag       { needArg = 0x01, repeatable = 0x02 };
  enum Found      { notFound, noArg, withArg          };
  enum Type       { optArg, optLong, optShort         };
  typedef bool (ArgFunc)(GetOpt* getopt, const Option* option,
                         const char* asEntered,
                         Connection connected, const char* argument,
                         int* usedChars);
  typedef void (ErrorFunc)(const char* option, const char* message);

  struct Option
  {
    char         shortName;
    const char*  longName;
    Found*       found;
    unsigned     flag;
    ArgFunc*     function;
    void*        data;
  }; // end GetOpt::Option

  bool           error;
  ErrorFunc*     errorOutput;
  const char*    optionStart;

 protected:
  const Option*  optionList;
  int            argc;
  int            argi, chari;
  const char**   argv;
  bool           normalOnly;
  const Option*  returningAll;
  char           shortOptionBuf[3];

 public:
  explicit GetOpt(const Option* aList);
  void  init(int theArgc, const char** theArgv);
  int   currentArg() const { return argi; };
  bool  nextOption(const Option*& option, const char*& asEntered);
  int   process(int theArgc, const char** theArgv);
  void  reportError(const char* option, const char* message);

  // Standard callback functions:
#ifndef GETOPT_NO_STDIO
  static void  printError(const char* option, const char* message);
#endif
  static bool  isFloat(GetOpt* getopt, const Option* option,
                       const char* asEntered,
                       Connection connected, const char* argument,
                       int* usedChars);
  static bool  isLong(GetOpt* getopt, const Option* option,
                      const char* asEntered,
                      Connection connected, const char* argument,
                      int* usedChars);
  static bool  isString(GetOpt* getopt, const Option* option,
                        const char* asEntered,
                        Connection connected, const char* argument,
                        int* usedChars);

 protected:
  void  checkReturnAll();
  const Option*  findShortOption(char option) const;
  const Option*  findLongOption(const char* option);
  bool  nextOption(const char*& option, Type& type, int& posArg);
}; // end GetOpt

#endif // INCLUDED_GETOPT_HPP