Geophysical Inversion and Modelling Library v1.5.4
Loading...
Searching...
No Matches
optionmap.h
1/******************************************************************************
2 * Copyright (C) 2009-2024 by the GIMLi development team *
3 * Carsten Rücker carsten@resistivity.net *
4 * *
5 * Licensed under the Apache License, Version 2.0 (the "License"); *
6 * you may not use this file except in compliance with the License. *
7 * You may obtain a copy of the License at *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 * See the License for the specific language governing permissions and *
15 * limitations under the License. *
16 * *
17 ******************************************************************************/
18
19#ifndef GIMLI_LONGOPTIONS__H
20#define GIMLI_LONGOPTIONS__H
21
22#include <list>
23#include <map>
24#include <string>
25#include <iostream>
26#include <algorithm>
27#include <cstdio>
28
29#ifdef _MSC_VER
30 #include <wingetopt.h>
31#else
32 #include <getopt.h>
33#endif
34
35#include "gimli.h"
36
37namespace GIMLI{
38
39class DLLEXPORT OptionBase{
40public:
41 virtual ~OptionBase(){}
42
43 virtual void assign_(char * opt=NULL) = 0;
44
45 virtual std::string typname() = 0;
46
47 virtual std::string defautToString() = 0;
48
49 inline const char key() const { return key_; }
50
51 void assign(char * opt){
52 if (hasArg_ == no_argument) assign_(NULL);
53 if (hasArg_ == required_argument) assign_(opt);
54 }
55
56 void * var_;
57 char key_;
58 std::string name_;
59 std::string help_;
60 int hasArg_;
61};
62
63inline std::ostringstream & operator << (std::ostringstream & s, const std::vector < std::string > & str){
64 for (uint i = 0; i < str.size(); i ++) s << str[ i ] << " ";
65 return s;
66}
67
68template < class T > class Option : public OptionBase{
69public:
70 Option(T & var){
71 var_ = &var;
72 }
73 Option(T & var, const T & defaultVar){
74 var_ = &var;
75 *(T*)(var_) = defaultVar;
76 defaultVar_ = defaultVar;
77 }
78
79 virtual ~Option(){}
80
81 virtual void assign_(char * opt = NULL){ convert(*(T*)(var_), opt); }
82
83 virtual std::string typname(){ return type(*(T*)(var_)); }
84
85 T value() { return *(T*)(var_); }
86
87 virtual std::string defautToString() {
88 std::ostringstream streamOut;
89 streamOut << defaultVar_;
90 return streamOut.str();
91 }
92
93 T defaultVar_;
94};
95
97
98class DLLEXPORT OptionMap{
99public:
100 OptionMap(const std::string & description = "");
101
102 virtual ~OptionMap();
103
104 typedef std::map< char, OptionBase * > CharOptionMap;
105 typedef std::map< std::string, OptionBase * > LongOptionMap;
106
107 template < class T > void addLastArg(T & var, const std::string & lastArgString){
108 lastArgString_ = lastArgString;
109 lastOption_ = new Option< T >(var);
110 lastOption_->hasArg_ = required_argument;
111 }
112
113 template < class T > void add(T & var, const std::string & key, const std::string & longOpt,
114 const std::string & help){
115 add(var, key, longOpt, help, T(var));
116 }
117
118 template < class T > void add(T & var, const std::string & key, const std::string & longOpt,
119 const std::string & help, const T & defaultVar){
120
121 if (key[ 0 ] != ':') allKeys_ += key;
122
123 OptionBase * o = new Option< T >(var, defaultVar);
124 o->name_ = longOpt;
125 if ((*--key.end()) == ':' || (*--longOpt.end() ==':')) o->hasArg_ = required_argument;
126 else o->hasArg_ = no_argument;
127
128 std::string k( key.substr(0, key.rfind(':')));
129 std::string lo(longOpt.substr(0, longOpt.rfind(':')));
130
131 if (k.length() > 0) {
132 o->key_ = k[ 0 ];
133 cMap_.insert(std::pair< char, OptionBase * >(k[ 0 ], o));
134 }
135
136 lMap_.insert(std::pair< std::string, OptionBase * >(lo, o));
137 options_.push_back(o);
138
139 o->help_ = help;
140 }
141
142 void parse(int argc, char * argv[]);
143
144 void buildLongOptions();
145
146 inline void setDescription(const std::string & description){ descriptionString_ = description; }
147
148 void printHelp(const std::string & main);
149
150 OptionBase * findOption(const char & key) const {
151 log(Warning, "command line parsing from core is obsolete and will be replaced.");
152 THROW_TO_IMPL
153 return NULL;
154 }
155
156protected:
157 struct option * opts_;
158 std::string allKeys_;
159 std::string lastArgString_;
160 std::string descriptionString_;
161
162 OptionBase * lastOption_;
163 std::list< OptionBase * > options_;
164 LongOptionMap lMap_;
165 CharOptionMap cMap_;
166 bool showVersion_;
167 bool debug_;
168};
169
170} // namespace GIMLI
171#endif //GIMLI_LONGOPTIONS__H
Definition optionmap.h:39
Definition optionmap.h:68
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
Definition wingetopt.h:63