e7b6942bd06a2e0f6aa167d554ab447c7c3ed7e0
1 /***************************************************************************************[Options.h]
2 Copyright (c) 2008-2010, Niklas Sorensson
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 associated documentation files (the "Software"), to deal in the Software without restriction,
6 including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
10 The above copyright notice and this permission notice shall be included in all copies or
11 substantial portions of the Software.
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 **************************************************************************************************/
20 #ifndef BVMinisat_Options_h
21 #define BVMinisat_Options_h
28 #include "prop/bvminisat/mtl/IntTypes.h"
29 #include "prop/bvminisat/mtl/Vec.h"
30 #include "prop/bvminisat/utils/ParseUtils.h"
35 //==================================================================================================
36 // Top-level option parse/help functions:
39 extern void parseOptions (int& argc
, char** argv
, bool strict
= false);
40 extern void printUsageAndExit(int argc
, char** argv
, bool verbose
= false);
41 extern void setUsageHelp (const char* str
);
42 extern void setHelpPrefixStr (const char* str
);
45 //==================================================================================================
46 // Options is an abstract class that gives the interface for all types options:
53 const char* description
;
55 const char* type_name
;
57 static vec
<Option
*>& getOptionList () { static vec
<Option
*> options
; return options
; }
58 static const char*& getUsageString() { static const char* usage_str
; return usage_str
; }
59 static const char*& getHelpPrefixString() { static const char* help_prefix_str
= ""; return help_prefix_str
; }
62 bool operator()(const Option
* x
, const Option
* y
) {
63 int test1
= strcmp(x
->category
, y
->category
);
64 return test1
< 0 || (test1
== 0 && strcmp(x
->type_name
, y
->type_name
) < 0);
68 Option(const char* name_
,
77 getOptionList().push(this);
83 virtual bool parse (const char* str
) = 0;
84 virtual void help (bool verbose
= false) = 0;
86 friend void parseOptions (int& argc
, char** argv
, bool strict
);
87 friend void printUsageAndExit (int argc
, char** argv
, bool verbose
);
88 friend void setUsageHelp (const char* str
);
89 friend void setHelpPrefixStr (const char* str
);
93 //==================================================================================================
94 // Range classes with specialization for floating types:
100 IntRange(int b
, int e
) : begin(b
), end(e
) {}
106 Int64Range(int64_t b
, int64_t e
) : begin(b
), end(e
) {}
112 bool begin_inclusive
;
114 DoubleRange(double b
, bool binc
, double e
, bool einc
) : begin(b
), end(e
), begin_inclusive(binc
), end_inclusive(einc
) {}
118 //==================================================================================================
122 class DoubleOption
: public Option
129 DoubleOption(const char* c
, const char* n
, const char* d
, double def
= double(), DoubleRange r
= DoubleRange(-HUGE_VAL
, false, HUGE_VAL
, false))
130 : Option(n
, d
, c
, "<double>"), range(r
), value(def
) {
131 // FIXME: set LC_NUMERIC to "C" to make sure that strtof/strtod parses decimal point correctly.
134 operator double (void) const { return value
; }
135 operator double& (void) { return value
; }
136 DoubleOption
& operator=(double x
) { value
= x
; return *this; }
138 bool parse(const char* str
) override
140 const char* span
= str
;
142 if (!match(span
, "-") || !match(span
, name
) || !match(span
, "="))
146 double tmp
= strtod(span
, &end
);
150 else if (tmp
>= range
.end
&& (!range
.end_inclusive
|| tmp
!= range
.end
))
153 "ERROR! value <%s> is too large for option \"%s\".\n",
158 else if (tmp
<= range
.begin
159 && (!range
.begin_inclusive
|| tmp
!= range
.begin
))
162 "ERROR! value <%s> is too small for option \"%s\".\n",
169 // fprintf(stderr, "READ VALUE: %g\n", value);
174 void help(bool verbose
= false) override
177 " -%-12s = %-8s %c%4.2g .. %4.2g%c (default: %g)\n",
180 range
.begin_inclusive
? '[' : '(',
183 range
.end_inclusive
? ']' : ')',
187 fprintf(stderr
, "\n %s\n", description
);
188 fprintf(stderr
, "\n");
194 //==================================================================================================
198 class IntOption
: public Option
205 IntOption(const char* c
, const char* n
, const char* d
, int32_t def
= int32_t(), IntRange r
= IntRange(INT32_MIN
, INT32_MAX
))
206 : Option(n
, d
, c
, "<int32>"), range(r
), value(def
) {}
208 operator int32_t (void) const { return value
; }
209 operator int32_t& (void) { return value
; }
210 IntOption
& operator= (int32_t x
) { value
= x
; return *this; }
212 bool parse(const char* str
) override
214 const char* span
= str
;
216 if (!match(span
, "-") || !match(span
, name
) || !match(span
, "="))
220 int32_t tmp
= strtol(span
, &end
, 10);
224 else if (tmp
> range
.end
)
227 "ERROR! value <%s> is too large for option \"%s\".\n",
232 else if (tmp
< range
.begin
)
235 "ERROR! value <%s> is too small for option \"%s\".\n",
246 void help(bool verbose
= false) override
248 fprintf(stderr
, " -%-12s = %-8s [", name
, type_name
);
249 if (range
.begin
== INT32_MIN
)
250 fprintf(stderr
, "imin");
252 fprintf(stderr
, "%4d", range
.begin
);
254 fprintf(stderr
, " .. ");
255 if (range
.end
== INT32_MAX
)
256 fprintf(stderr
, "imax");
258 fprintf(stderr
, "%4d", range
.end
);
260 fprintf(stderr
, "] (default: %d)\n", value
);
263 fprintf(stderr
, "\n %s\n", description
);
264 fprintf(stderr
, "\n");
270 // Leave this out for visual C++ until Microsoft implements C99 and gets support for strtoll.
273 class Int64Option
: public Option
280 Int64Option(const char* c
, const char* n
, const char* d
, int64_t def
= int64_t(), Int64Range r
= Int64Range(INT64_MIN
, INT64_MAX
))
281 : Option(n
, d
, c
, "<int64>"), range(r
), value(def
) {}
283 operator int64_t (void) const { return value
; }
284 operator int64_t& (void) { return value
; }
285 Int64Option
& operator= (int64_t x
) { value
= x
; return *this; }
287 bool parse(const char* str
) override
289 const char* span
= str
;
291 if (!match(span
, "-") || !match(span
, name
) || !match(span
, "="))
295 int64_t tmp
= strtoll(span
, &end
, 10);
299 else if (tmp
> range
.end
)
302 "ERROR! value <%s> is too large for option \"%s\".\n",
307 else if (tmp
< range
.begin
)
310 "ERROR! value <%s> is too small for option \"%s\".\n",
321 void help(bool verbose
= false) override
323 fprintf(stderr
, " -%-12s = %-8s [", name
, type_name
);
324 if (range
.begin
== INT64_MIN
)
325 fprintf(stderr
, "imin");
327 fprintf(stderr
, "%4" PRIi64
, range
.begin
);
329 fprintf(stderr
, " .. ");
330 if (range
.end
== INT64_MAX
)
331 fprintf(stderr
, "imax");
333 fprintf(stderr
, "%4" PRIi64
, range
.end
);
335 fprintf(stderr
, "] (default: %" PRIi64
")\n", value
);
338 fprintf(stderr
, "\n %s\n", description
);
339 fprintf(stderr
, "\n");
345 //==================================================================================================
349 class StringOption
: public Option
353 StringOption(const char* c
, const char* n
, const char* d
, const char* def
= NULL
)
354 : Option(n
, d
, c
, "<string>"), value(def
) {}
356 operator const char* (void) const { return value
; }
357 operator const char*& (void) { return value
; }
358 StringOption
& operator= (const char* x
) { value
= x
; return *this; }
360 bool parse(const char* str
) override
362 const char* span
= str
;
364 if (!match(span
, "-") || !match(span
, name
) || !match(span
, "="))
371 void help(bool verbose
= false) override
373 fprintf(stderr
, " -%-10s = %8s\n", name
, type_name
);
376 fprintf(stderr
, "\n %s\n", description
);
377 fprintf(stderr
, "\n");
383 //==================================================================================================
387 class BoolOption
: public Option
392 BoolOption(const char* c
, const char* n
, const char* d
, bool v
)
393 : Option(n
, d
, c
, "<bool>"), value(v
) {}
395 operator bool (void) const { return value
; }
396 operator bool& (void) { return value
; }
397 BoolOption
& operator=(bool b
) { value
= b
; return *this; }
399 bool parse(const char* str
) override
401 const char* span
= str
;
403 if (match(span
, "-"))
405 bool b
= !match(span
, "no-");
407 if (strcmp(span
, name
) == 0)
417 void help(bool verbose
= false) override
419 fprintf(stderr
, " -%s, -no-%s", name
, name
);
421 for (uint32_t i
= 0; i
< 32 - strlen(name
) * 2; i
++) fprintf(stderr
, " ");
423 fprintf(stderr
, " ");
424 fprintf(stderr
, "(default: %s)\n", value
? "on" : "off");
427 fprintf(stderr
, "\n %s\n", description
);
428 fprintf(stderr
, "\n");
433 //=================================================================================================
434 } // namespace BVMinisat