File: ArgumentParser.h

package info (click to toggle)
bitseq 0.7.5+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 676 kB
  • sloc: cpp: 7,043; python: 562; makefile: 150; sh: 52
file content (110 lines) | stat: -rw-r--r-- 4,119 bytes parent folder | download | duplicates (4)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef ARGUMENTPARSER_H
#define ARGUMENTPARSER_H

#include<map>
#include<string>
#include<vector>

using namespace std;

enum OptionType {OTSTRING, OTLONG, OTBOOL, OTDOUBLE};
struct Option{//{{{
   OptionType type;
   string shortName,longName,description;
};//}}}

class ArgumentParser{
   private:
      map<string,string> mapS;
      map<string,long> mapL;
      map<string,bool> mapB;
      map<string,double> mapD;
      map<string,string> names;
      map<string,Option> validOptions;
      string programName, argumentDesc, programDesc;
      vector<string> arguments;
      vector<string> compulsory;
      long minimumArguments;

      bool existsOption(const string &name, bool warn = false) const;
      bool existsName(const string &name) const;
      template <typename valueType>
      void appendDescription(string *desc,valueType defValue);
   public:
      // The value of verbose option for direct access.
      bool verbose;

      // Constructor for the class sets: programDescription, additional string
      // and minimum number of required arguments.
      ArgumentParser(const string &pD="",const string &aD="[FILES]", long minArgs = 1){//{{{
         verbose = false;
         init(pD,aD,minArgs);
      }//}}}
      // Init function for initialization, sets the same values as constructor.
      void init(const string &pD="",const string &aD="[FILES]", long minArgs = 1){//{{{
         programDesc=pD; 
         argumentDesc=aD; 
         minimumArguments = minArgs;
      }//}}}
      // Parse function given number of arguments and array of arguments 
      // it processes the arguments and makes options available through 
      // get[S/L/D] functions and args() function.
      bool parse(int n,char * argv[]); 
      /*
       * SETTERS:
       */
      // Add option (string) adds new option, name is the name used for referring 
      // to it.
      void addOptionS(const string &shortName,
                      const string &longName,
                      const string &name,
                      bool comp,
                      const string &description="", 
                      const string &defValue="noDefault");
      // Add option (long).
      void addOptionL(const string &shortName, const string &longName,
                      const string &name, bool comp, const string &description="",
                      long defValue=-47);
      // Add option (double).
      void addOptionD(const string &shortName, const string &longName,
                      const string &name, bool comp, const string &description="",
                      double defValue=-47.47);
      // Add option (boolean or 'flag').
      void addOptionB(const string &shortName, const string &longName, 
                      const string &name, bool comp, const string &description="",
                      bool defValue=false);
      /*
       * GETTERS:
       */
      // Return reference to vector of arguments
      // (i.e. the strings provided with no -/-- modifier).
      const vector<string>& args() const { return arguments; }
      // Return true if option <name> was set.
      bool isSet(const string &name) const;
      // Return value of string option <name>.
      string getS(const string &name) const;
      // Return value of string option <name> in lower case.
      string getLowerS(const string &name) const;
      // Return value of integer option <name>.
      long getL(const string &name) const;
      // Return value of double option <name>.
      double getD(const string &name) const;
      // Return value of bool option <name>.
      bool flag(const string &name) const;
      // Return value of verbose.
      bool verb() const { return verbose; }

      /*
       * OTHER:
       */
      // (Advanced get) Return tokenized (comma separated) string as vector of doubles.
      vector<double> getTokenizedS2D(const string &name) const;
      // Write usage string.
      void usage();
      // Write all options.
      void writeAll();
      // Update value of existing string option.
      void updateS(const string &name, const string &value);
};

#endif