aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/Usage.hpp
blob: 40b933fb8f84de30ae269b7c8e7f5db94b9b1cbb (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdlib>

#include "utility.h"

extern "C"{
#include <unistd.h>
}

namespace meow{
  inline Usage::Usage(): name("nobody") { }
  inline Usage::Usage(Usage::String const& _name): name(_name){ }
  inline bool Usage::import(Usage const& usage){
    OptionsIterator it;
    for(it = usage.options.begin(); it != usage.options.end(); it++){
      unsigned char const& chr = it->first;
      Option        const& opt = it->second;
      if(options.find(chr) == options.end()){
        options[chr] = opt;
      }else{
        return false;
      }
    }
    for(size_t i = 0; i < usage.usage_begin.size(); i++){
      usage_begin.push_back(usage.usage_begin[i]);
    }
    for(size_t i = 0; i < usage.usage_end.size(); i++){
      usage_end.push_back(usage.usage_end[i]);
    }
    return true;
  }
  inline bool Usage::update(Usage const& usage){
    OptionsIterator it;
    for(it = usage.options.begin(); it != usage.options.end(); it++){
      unsigned char const& chr = it->first;
      if(options.find(chr) == options.end()){
        continue;
      }
      options[chr] = it->second;
    }
    return true;
  }
  inline bool Usage::addOption(unsigned char opt, Usage::String const& des){
    if(options.find(opt) != options.end()){
      return false;
    }
    options[opt] = Option(des);
    return true;
  }
  inline bool Usage::addOption(unsigned char opt, Usage::String const& des,
                               Usage::String const& val_type,
                               Usage::String const& val_default,
                               bool must){
    if(options.find(opt) != options.end()){
      return false;
    }
    options[opt] = Option(des, val_type, val_default, must);
    return true;
  }
  inline bool Usage::addOptionValueAccept(unsigned char opt,
                                          Usage::String const& val,
                                          Usage::String const& des){
    if(options.find(opt) == options.end()){
      return false;
    }
    return options[opt].addValueAccept(val, des);
  }
  inline bool Usage::hasOptionSetup(unsigned char opt) const {
    return (options.find(opt) != options.end() &&
            options.find(opt)->second.hasSetup());
  }
  inline size_t Usage::getOptionValuesCount(unsigned char opt) const {
    if(options.find(opt) == options.end()){
      return 0;
    }
    return options.find(opt)->second.getValuesCount();
  }
  inline Usage::String Usage::getOptionValue(unsigned char opt,
                                             size_t index) const {
    if(options.find(opt) == options.end()){
      return Usage::String();
    }
    return options.find(opt)->second.getValue(index);
  }
  inline size_t Usage::getProcArgsCount() const{
    return proc_arguments.size();
  }
  inline Usage::String Usage::getProcArg(size_t index) const {
    if(index >= proc_arguments.size()){
      return Usage::String();
    }
    return proc_arguments[index];
  }
  inline Usage::Strings Usage::getProcArgs() const {
    return proc_arguments;
  }
  inline void Usage::addUsageBegin(Usage::String const& des){
    usage_begin.push_back(stringReplace(des, "<name>", name));
  }
  inline void Usage::addUsageEnd(Usage::String const& des){
    usage_end.push_back(stringReplace(des, "<name>", name));
  }
  inline Usage::String Usage::getUsage() const {
    Usage::String out = stringPrintf("USAGE\n    %s", name.c_str());
    OptionsIterator it;
    for(it = options.begin(); it != options.end(); it++){
      out += " " + it->second.getUsage(it->first, false);
    }
    out += "\n\nDESCRIPTION\n";
    for(size_t i = 0; i < usage_begin.size(); i++){
      out += "    " + usage_begin[i] + "\n\n";
    }
    for(it = options.begin(); it != options.end(); it++){
      out += it->second.getUsage(it->first, true);
    }
    for(size_t i = 0; i < usage_end.size(); i++){
      out += "    " + usage_end[i] + "\n\n";
    }
    return out;
  }
  inline bool Usage::setArguments(int argc, char** argv, Usage::String *errmsg){
    opterr = 0;
    Usage::String s;
    OptionsIterator it;
    Usage::String zzz;
    Usage::String& err = (errmsg == NULL ? zzz : *errmsg);
    for(it = options.begin(); it != options.end(); it++){
      s += (char)(it->first);
      if(it->second.hasValue()){
        s += ":";
      }
    }
    for(int opt; (opt = getopt(argc, argv, s.c_str())) != -1; ){
      if(options.find(opt) == options.end()){
        if(options.find(optopt) == options.end()){
          err += stringPrintf("Unknown option '-%c'\n", optopt);
        }else{
          err += stringPrintf("No specify argument to '-%c'\n",
                              optopt);
        }
        opt = optopt;
        return false;
      }
      if(options[opt].setValue(optarg == NULL ? "" : optarg) == false){
        err += stringPrintf(
          "Option argument '%s' to '-%c' is not allowed\n"
          , optarg, opt);
        return false;
      }
    }
    for(it = options.begin(); it != options.end(); it++){
      if(it->second.chkSetup() == false){
        err += stringPrintf("No specify argument to '-%c'\n",
                            it->first);
        return false;
      }
    }
    for(int i = optind; i < argc; i++){
      proc_arguments.push_back(Usage::String(argv[i]));
    }
    return true;
  }
  //
  inline Usage::Value::Value(){ }
  inline Usage::Value::Value(Usage::String const& v){
    value = v;
    description = "";
  }
  inline Usage::Value::Value(Usage::String const& v, Usage::String const& d){
    value       = v;
    description = stringReplace(d, "<value>", v);
  }
  inline Usage::String Usage::Value::getUsage() const {
    if(description.length() > 0)
      return stringPrintf("%8s%s : %s\n",
                          " ", value.c_str(), description.c_str());
    else
      return "";
  }
  inline Usage::String Usage::Value::getValue() const {
    return value;
  }
  inline bool Usage::Value::operator==(Value const& b) const {
    return (value == b.value);
  }
  //
  inline Usage::Option::Option(){ }
  inline Usage::Option::Option(Usage::String const& des){
    has_setup = false;
    has_value = false;
    description = des;
    must_setup = false;
  }
  inline Usage::Option::Option(Usage::String const& des,
                               Usage::String const& typ,
                               Usage::String const& def,
                               bool must){
    has_setup   = false;
    has_value   = true;
    description = des;
    value_type    = typ;
    value_default = def;
    must_setup = must;
  }
  inline bool Usage::Option::setValue(Usage::String const& str){
    if(has_value){
      if(values_accept.size() > 0 &&
         std::find(values_accept.begin(), values_accept.end(),
                   Value(str, "")) == values_accept.end()){
        return false;
      }
      values.push_back(str);
    }
    has_setup = true;
    return true;
  }
  inline size_t Usage::Option::getValuesCount()const{return values.size();}
  inline Usage::String Usage::Option::getValue(size_t index) const{
    if(!has_value){
      return "";
    }
    if(!has_setup || index >= values.size()){
      return value_default;
    }
    return values[index];
  }
  inline bool Usage::Option::addValueAccept(Usage::String const& val,
                                            Usage::String const& des){
    if(!has_value){
      return false;
    }
    if(std::find(values_accept.begin(), values_accept.end(), Value(val))
       == values_accept.end()){
      values_accept.push_back(Value(val, des));
    }
    return true;
  }
  inline bool Usage::Option::hasSetup() const { return has_setup; }
  inline bool Usage::Option::hasValue() const { return has_value; }
  inline bool Usage::Option::chkSetup() const {
    return !(must_setup && !has_setup);
  }
  inline Usage::String Usage::Option::getUsage(unsigned char opt,
                                               bool detail) const {
    Usage::String ret;
    if(!detail){
      if(!has_value){
        ret = stringPrintf("-%c", opt);
      }else{
        ret = stringPrintf("-%c %s", opt, value_type.c_str());
      }
      if(!must_setup){
        ret = stringPrintf("[%s]", ret.c_str());
      }
    }else{
      Usage::String tmp;
      if(has_value){
        Usage::String tmp2;
        if(value_default != ""){
          tmp2=stringPrintf("defuault='%s'",value_default.c_str());
        }
        Usage::String tmp3 = must_setup ? "" : "optional";
        if(tmp2.length() + tmp3.length() > 0){
          if(tmp2.length() > 0 && tmp3.length() > 0){
            tmp = "(" + tmp3 + ", " + tmp2 + ")";
          }else{
            tmp = "(" + tmp3 + tmp2 + ")";
          }
        }
        tmp = value_type + " " + tmp;
      }
      ret = stringPrintf("    -%c %s\n", opt, tmp.c_str());
      tmp = stringReplace(description, "<type>", value_type);
      Usage::String vs;
      for(size_t i = 0; i < values_accept.size(); i++){
        if(i > 0){
          vs += (i + 1 < values_accept.size() ? ", " : " or ");
        }
        vs += "'" + values_accept[i].getValue() + "'";
      }
      if(vs.length() == 0){
        vs = "... (anything)";
      }
      tmp = stringReplace(tmp, "<values>", vs);
      ret += "        " + tmp + "\n";
      for(size_t i = 0; i < values_accept.size(); i++){
        ret += values_accept[i].getUsage();
      }
      ret += "\n";
    }
    return ret;
  }
}