1 /********************* */
2 /*! \file options_template.cpp
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): Kshitij Bansal
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief Contains code for handling command-line options.
14 ** Contains code for handling command-line options
17 #warning "TODO: Remove ExprStream forward declaration from options.h."
19 #if !defined(_BSD_SOURCE) && defined(__MINGW32__) && !defined(__MINGW64__)
20 // force use of optreset; mingw32 croaks on argv-switching otherwise
21 # include "cvc4autoconfig.h"
23 # undef HAVE_DECL_OPTRESET
24 # define HAVE_DECL_OPTRESET 1
25 # define CVC4_IS_NOT_REALLY_BSD
26 #endif /* !_BSD_SOURCE && __MINGW32__ && !__MINGW64__ */
30 #endif /* __MINGW64__ */
35 #ifdef CVC4_IS_NOT_REALLY_BSD
37 #endif /* CVC4_IS_NOT_REALLY_BSD */
53 #include "base/cvc4_assert.h"
54 #include "base/exception.h"
55 #include "base/output.h"
57 #include "options/didyoumean.h"
58 #include "options/language.h"
59 #include "options/options_handler_interface.h"
61 $
{include_all_option_headers
}
63 #line 58 "${template}"
65 #include "options/options_holder.h"
66 #include "cvc4autoconfig.h"
67 #include "options/base_handlers.h"
69 $
{option_handler_includes
}
71 #line 67 "${template}"
74 using namespace CVC4::options
;
78 CVC4_THREADLOCAL(Options
*) Options::s_current
= NULL
;
81 * This is a default handler for options of built-in C++ type. This
82 * template is really just a helper for the handleOption() template,
83 * below. Variants of this template handle numeric and non-numeric,
84 * integral and non-integral, signed and unsigned C++ types.
85 * handleOption() makes sure to instantiate the right one.
87 * This implements default behavior when e.g. an option is
88 * unsigned but the user specifies a negative argument; etc.
90 template <class T
, bool is_numeric
, bool is_integer
>
91 struct OptionHandler
{
92 static T
handle(std::string option
, std::string optionarg
);
93 };/* struct OptionHandler<> */
95 /** Variant for integral C++ types */
97 struct OptionHandler
<T
, true, true> {
98 static bool stringToInt(T
& t
, const std::string
& str
) {
99 std::istringstream
ss(str
);
102 return !(ss
.fail() || ss
.get(tmp
));
105 static bool containsMinus(const std::string
& str
) {
106 return str
.find('-') != std::string::npos
;
109 static T
handle(const std::string
& option
, const std::string
& optionarg
) {
112 bool success
= stringToInt(i
, optionarg
);
115 throw OptionException(option
+ ": failed to parse "+ optionarg
+" as an integer of the appropraite type.");
118 // Depending in the platform unsigned numbers with '-' signs may parse.
119 // Reject these by looking for any minus if it is not signed.
120 if( (! std::numeric_limits
<T
>::is_signed
) && containsMinus(optionarg
) ) {
121 // unsigned type but user gave negative argument
122 throw OptionException(option
+ " requires a nonnegative argument");
123 } else if(i
< std::numeric_limits
<T
>::min()) {
124 // negative overflow for type
125 std::stringstream ss
;
126 ss
<< option
<< " requires an argument >= " << std::numeric_limits
<T
>::min();
127 throw OptionException(ss
.str());
128 } else if(i
> std::numeric_limits
<T
>::max()) {
129 // positive overflow for type
130 std::stringstream ss
;
131 ss
<< option
<< " requires an argument <= " << std::numeric_limits
<T
>::max();
132 throw OptionException(ss
.str());
137 // if(std::numeric_limits<T>::is_signed) {
138 // return T(i.getLong());
140 // return T(i.getUnsignedLong());
142 } catch(std::invalid_argument
&) {
143 // user gave something other than an integer
144 throw OptionException(option
+ " requires an integer argument");
147 };/* struct OptionHandler<T, true, true> */
149 /** Variant for numeric but non-integral C++ types */
151 struct OptionHandler
<T
, true, false> {
152 static T
handle(std::string option
, std::string optionarg
) {
153 std::stringstream
in(optionarg
);
157 // we didn't consume the whole string (junk at end)
158 throw OptionException(option
+ " requires a numeric argument");
161 if(! std::numeric_limits
<T
>::is_signed
&& r
< 0.0) {
162 // unsigned type but user gave negative value
163 throw OptionException(option
+ " requires a nonnegative argument");
164 } else if(r
< -std::numeric_limits
<T
>::max()) {
165 // negative overflow for type
166 std::stringstream ss
;
167 ss
<< option
<< " requires an argument >= " << -std::numeric_limits
<T
>::max();
168 throw OptionException(ss
.str());
169 } else if(r
> std::numeric_limits
<T
>::max()) {
170 // positive overflow for type
171 std::stringstream ss
;
172 ss
<< option
<< " requires an argument <= " << std::numeric_limits
<T
>::max();
173 throw OptionException(ss
.str());
178 };/* struct OptionHandler<T, true, false> */
180 /** Variant for non-numeric C++ types */
182 struct OptionHandler
<T
, false, false> {
183 static T
handle(std::string option
, std::string optionarg
) {
184 T::unsupported_handleOption_call___please_write_me
;
185 // The above line causes a compiler error if this version of the template
186 // is ever instantiated (meaning that a specialization is missing). So
187 // don't worry about the segfault in the next line, the "return" is only
188 // there to keep the compiler from giving additional, distracting errors
192 };/* struct OptionHandler<T, false, false> */
194 /** Handle an option of type T in the default way. */
196 T
handleOption(std::string option
, std::string optionarg
) {
197 return OptionHandler
<T
, std::numeric_limits
<T
>::is_specialized
, std::numeric_limits
<T
>::is_integer
>::handle(option
, optionarg
);
200 /** Handle an option of type std::string in the default way. */
202 std::string handleOption
<std::string
>(std::string option
, std::string optionarg
) {
207 * Run handler, and any user-given predicates, for option T.
208 * If a user specifies a :handler or :predicates, it overrides this.
211 typename
T::type
runHandlerAndPredicates(T
, std::string option
, std::string optionarg
, options::OptionsHandler
* handler
) {
212 // By default, parse the option argument in a way appropriate for its type.
213 // E.g., for "unsigned int" options, ensure that the provided argument is
214 // a nonnegative integer that fits in the unsigned int type.
216 return handleOption
<typename
T::type
>(option
, optionarg
);
220 void runBoolPredicates(T
, std::string option
, bool b
, options::OptionsHandler
* handler
) {
221 // By default, nothing to do for bool. Users add things with
222 // :predicate in options files to provide custom checking routines
223 // that can throw exceptions.
226 $
{all_custom_handlers
}
228 #line 204 "${template}"
231 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT true
232 #else /* CVC4_DEBUG */
233 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT false
234 #endif /* CVC4_DEBUG */
236 #if defined(CVC4_MUZZLED) || defined(CVC4_COMPETITION_MODE)
237 # define DO_SEMANTIC_CHECKS_BY_DEFAULT false
238 #else /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
239 # define DO_SEMANTIC_CHECKS_BY_DEFAULT true
240 #endif /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
243 d_holder(new options::OptionsHolder()) {
246 Options::Options(const Options
& options
) :
247 d_holder(new options::OptionsHolder(*options
.d_holder
)) {
250 Options::~Options() {
254 Options
& Options::operator=(const Options
& options
) {
255 if(this != &options
) {
257 d_holder
= new options::OptionsHolder(*options
.d_holder
);
262 options::OptionsHolder::OptionsHolder() : $
{all_modules_defaults
}
266 #line 242 "${template}"
268 static const std::string mostCommonOptionsDescription
= "\
269 Most commonly-used CVC4 options:${common_documentation}";
271 #line 247 "${template}"
273 static const std::string optionsDescription
= mostCommonOptionsDescription
+ "\n\
275 Additional CVC4 options:${remaining_documentation}";
277 #line 253 "${template}"
279 static const std::string optionsFootnote
= "\n\
280 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
281 sense of the option.\n\
284 static const std::string languageDescription
= "\
285 Languages currently supported as arguments to the -L / --lang option:\n\
286 auto attempt to automatically determine language\n\
287 cvc4 | presentation | pl CVC4 presentation language\n\
288 smt1 | smtlib1 SMT-LIB format 1.2\n\
289 smt | smtlib | smt2 |\n\
290 smt2.0 | smtlib2 | smtlib2.0 SMT-LIB format 2.0\n\
291 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
292 tptp TPTP format (cnf and fof)\n\
293 sygus SyGuS format\n\
295 Languages currently supported as arguments to the --output-lang option:\n\
296 auto match output language to input language\n\
297 cvc4 | presentation | pl CVC4 presentation language\n\
298 cvc3 CVC3 presentation language\n\
299 smt1 | smtlib1 SMT-LIB format 1.2\n\
300 smt | smtlib | smt2 |\n\
301 smt2.0 | smtlib2.0 | smtlib2 SMT-LIB format 2.0\n\
302 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
304 z3str SMT-LIB 2.0 with Z3-str string constraints\n\
305 ast internal format (simple syntax trees)\n\
308 std::string
Options::getDescription() const {
309 return optionsDescription
;
312 void Options::printUsage(const std::string msg
, std::ostream
& out
) {
313 out
<< msg
<< optionsDescription
<< std::endl
314 << optionsFootnote
<< std::endl
<< std::flush
;
317 void Options::printShortUsage(const std::string msg
, std::ostream
& out
) {
318 out
<< msg
<< mostCommonOptionsDescription
<< std::endl
319 << optionsFootnote
<< std::endl
320 << "For full usage, please use --help." << std::endl
<< std::endl
<< std::flush
;
323 void Options::printLanguageHelp(std::ostream
& out
) {
324 out
<< languageDescription
<< std::flush
;
328 * This is a table of long options. By policy, each short option
329 * should have an equivalent long option (but the reverse isn't the
330 * case), so this table should thus contain all command-line options.
332 * Each option in this array has four elements:
334 * 1. the long option string
335 * 2. argument behavior for the option:
336 * no_argument - no argument permitted
337 * required_argument - an argument is expected
338 * optional_argument - an argument is permitted but not required
339 * 3. this is a pointer to an int which is set to the 4th entry of the
340 * array if the option is present; or NULL, in which case
341 * getopt_long() returns the 4th entry
342 * 4. the return value for getopt_long() when this long option (or the
343 * value to set the 3rd entry to; see #3)
345 * If you add something here, you should add it in src/main/usage.h
346 * also, to document it.
348 * If you add something that has a short option equivalent, you should
349 * add it to the getopt_long() call in parseOptions().
351 static struct option cmdlineOptions
[] = {$
{all_modules_long_options
}
352 { NULL
, no_argument
, NULL
, '\0' }
353 };/* cmdlineOptions */
355 #line 330 "${template}"
357 static void preemptGetopt(int& argc
, char**& argv
, const char* opt
) {
358 const size_t maxoptlen
= 128;
360 Debug("preemptGetopt") << "preempting getopt() with " << opt
<< std::endl
;
362 AlwaysAssert(opt
!= NULL
&& *opt
!= '\0');
363 AlwaysAssert(strlen(opt
) <= maxoptlen
);
367 while(argv
[i
] != NULL
&& argv
[i
][0] != '\0') {
371 if(argv
[i
] == NULL
) {
372 argv
= (char**) realloc(argv
, (i
+ 6) * sizeof(char*));
373 for(unsigned j
= i
; j
< i
+ 5; ++j
) {
374 argv
[j
] = (char*) malloc(sizeof(char) * maxoptlen
);
380 strncpy(argv
[i
], opt
, maxoptlen
- 1);
381 argv
[i
][maxoptlen
- 1] = '\0'; // ensure NUL-termination even on overflow
386 /** Set a given Options* as "current" just for a particular scope. */
388 CVC4_THREADLOCAL_TYPE(Options
*)* d_field
;
391 OptionsGuard(CVC4_THREADLOCAL_TYPE(Options
*)* field
, Options
* opts
) :
399 };/* class OptionsGuard */
401 }/* CVC4::options namespace */
404 * Parse argc/argv and put the result into a CVC4::Options.
405 * The return value is what's left of the command line (that is, the
406 * non-option arguments).
408 std::vector
<std::string
> Options::parseOptions(int argc
, char* main_argv
[], options::OptionsHandler
* const handler
) throw(OptionException
) {
409 options::OptionsGuard
guard(&s_current
, this);
411 const char *progName
= main_argv
[0];
413 Debug("options") << "main_argv == " << main_argv
<< std::endl
;
415 // Reset getopt(), in the case of multiple calls to parseOptions().
416 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
418 #if HAVE_DECL_OPTRESET
419 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
420 #endif /* HAVE_DECL_OPTRESET */
422 // find the base name of the program
423 const char *x
= strrchr(progName
, '/');
427 d_holder
->binary_name
= std::string(progName
);
430 char **extra_argv
= (char**) malloc(2 * sizeof(char*));
431 extra_argv
[0] = NULL
;
432 extra_argv
[1] = NULL
;
434 int extra_optind
= 0, main_optind
= 0;
436 int *optind_ref
= &main_optind
;
438 char** argv
= main_argv
;
440 std::vector
<std::string
> nonOptions
;
445 std::string option
, optionarg
;
446 Debug("preemptGetopt") << "top of loop, extra_optind == " << extra_optind
<< ", extra_argc == " << extra_argc
<< std::endl
;
447 if((extra_optind
== 0 ? 1 : extra_optind
) < extra_argc
) {
448 #if HAVE_DECL_OPTRESET
449 if(optind_ref
!= &extra_optind
) {
450 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
452 #endif /* HAVE_DECL_OPTRESET */
453 old_optind
= optind
= extra_optind
;
454 optind_ref
= &extra_optind
;
456 Debug("preemptGetopt") << "in preempt code, next arg is " << extra_argv
[optind
== 0 ? 1 : optind
] << std::endl
;
457 if(extra_argv
[extra_optind
== 0 ? 1 : extra_optind
][0] != '-') {
458 InternalError("preempted args cannot give non-options command-line args (found `%s')", extra_argv
[extra_optind
== 0 ? 1 : extra_optind
]);
460 c
= getopt_long(extra_argc
, extra_argv
,
461 "+:${all_modules_short_options}",
462 cmdlineOptions
, NULL
);
463 Debug("preemptGetopt") << "in preempt code, c == " << c
<< " (`" << char(c
) << "') optind == " << optind
<< std::endl
;
465 ( optopt
>= $
{long_option_value_begin
} && optopt
<= $
{long_option_value_end
} )) {
467 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
468 optionarg
= (optarg
== NULL
) ? "" : optarg
;
471 option
= std::string("-") + char(optopt
);
472 optionarg
= (optarg
== NULL
) ? "" : optarg
;
474 if(optind
>= extra_argc
) {
475 Debug("preemptGetopt") << "-- no more preempt args" << std::endl
;
477 while(extra_argv
[i
] != NULL
&& extra_argv
[i
][0] != '\0') {
478 extra_argv
[i
][0] = '\0';
484 Debug("preemptGetopt") << "-- more preempt args" << std::endl
;
485 extra_optind
= optind
;
489 #if HAVE_DECL_OPTRESET
490 if(optind_ref
!= &main_optind
) {
491 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
493 #endif /* HAVE_DECL_OPTRESET */
494 old_optind
= optind
= main_optind
;
495 optind_ref
= &main_optind
;
497 if(main_optind
< argc
&& main_argv
[main_optind
][0] != '-') {
499 if(main_optind
!= 0) {
500 nonOptions
.push_back(main_argv
[main_optind
]);
503 } while(main_optind
< argc
&& main_argv
[main_optind
][0] != '-');
506 Debug("options") << "[ before, optind == " << optind
<< " ]" << std::endl
;
507 #if defined(__MINGW32__) || defined(__MINGW64__)
508 if(optreset
== 1 && optind
> 1) {
509 // on mingw, optreset will reset the optind, so we have to
510 // manually advance argc, argv
511 main_argv
[optind
- 1] = main_argv
[0];
512 argv
= main_argv
+= optind
- 1;
514 old_optind
= optind
= main_optind
= 1;
516 Debug("options") << "looking at : " << argv
[0] << std::endl
;
518 /*c = getopt_long(argc, main_argv,
519 "+:${all_modules_short_options}",
520 cmdlineOptions, NULL);
521 Debug("options") << "pre-emptory c is " << c << " (" << char(c) << ")" << std::endl;
522 Debug("options") << "optind was reset to " << optind << std::endl;
523 optind = main_optind;
524 Debug("options") << "I restored optind to " << optind << std::endl;*/
526 #endif /* __MINGW32__ || __MINGW64__ */
527 Debug("options") << "[ argc == " << argc
<< ", main_argv == " << main_argv
<< " ]" << std::endl
;
528 c
= getopt_long(argc
, main_argv
,
529 "+:${all_modules_short_options}",
530 cmdlineOptions
, NULL
);
531 main_optind
= optind
;
532 Debug("options") << "[ got " << int(c
) << " (" << char(c
) << ") ]" << std::endl
;
533 Debug("options") << "[ next option will be at pos: " << optind
<< " ]" << std::endl
;
535 Debug("options") << "done with option parsing" << std::endl
;
538 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
539 optionarg
= (optarg
== NULL
) ? "" : optarg
;
542 Debug("preemptGetopt") << "processing option " << c
<< " (`" << char(c
) << "'), " << option
<< std::endl
;
545 $
{all_modules_option_handlers
}
547 #line 523 "${template}"
550 // This can be a long or short option, and the way to get at the
551 // name of it is different.
552 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
556 if( ( optopt
== 0 || ( optopt
>= $
{long_option_value_begin
} && optopt
<= $
{long_option_value_end
} ) ) &&
557 !strncmp(argv
[optind
- 1], "--thread", 8) &&
558 strlen(argv
[optind
- 1]) > 8 ) {
559 if(! isdigit(argv
[optind
- 1][8])) {
560 throw OptionException(std::string("can't understand option `") + option
+ "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
562 std::vector
<std::string
>& threadArgv
= d_holder
->threadArgv
;
564 long tnum
= strtol(argv
[optind
- 1] + 8, &end
, 10);
565 if(tnum
< 0 || (*end
!= '\0' && *end
!= '=')) {
566 throw OptionException(std::string("can't understand option `") + option
+ "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
568 if(threadArgv
.size() <= size_t(tnum
)) {
569 threadArgv
.resize(tnum
+ 1);
571 if(threadArgv
[tnum
] != "") {
572 threadArgv
[tnum
] += " ";
574 if(*end
== '\0') { // e.g., we have --thread0 "foo"
576 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
578 Debug("options") << "thread " << tnum
<< " gets option " << argv
[optind
] << std::endl
;
579 threadArgv
[tnum
] += argv
[(*optind_ref
)++];
580 } else { // e.g., we have --thread0="foo"
582 throw OptionException(std::string("option `") + option
+ "' missing its required argument");
584 Debug("options") << "thread " << tnum
<< " gets option " << (end
+ 1) << std::endl
;
585 threadArgv
[tnum
] += end
+ 1;
587 Debug("options") << "thread " << tnum
<< " now has " << threadArgv
[tnum
] << std::endl
;
591 throw OptionException(std::string("can't understand option `") + option
+ "'"
592 + suggestCommandLineOptions(option
));
596 Debug("options") << "returning " << nonOptions
.size() << " non-option arguments." << std::endl
;
603 std::string
Options::suggestCommandLineOptions(const std::string
& optionName
) throw() {
604 DidYouMean didYouMean
;
607 for(size_t i
= 0; (opt
= cmdlineOptions
[i
].name
) != NULL
; ++i
) {
608 didYouMean
.addWord(std::string("--") + cmdlineOptions
[i
].name
);
611 return didYouMean
.getMatchAsString(optionName
.substr(0, optionName
.find('=')));
614 static const char* smtOptions
[] = {
615 $
{all_modules_smt_options
},
616 #line 592 "${template}"
620 std::vector
<std::string
> Options::suggestSmtOptions(const std::string
& optionName
) throw() {
621 std::vector
<std::string
> suggestions
;
624 for(size_t i
= 0; (opt
= smtOptions
[i
]) != NULL
; ++i
) {
625 if(std::strstr(opt
, optionName
.c_str()) != NULL
) {
626 suggestions
.push_back(opt
);
633 std::vector
< std::vector
<std::string
> > Options::getOptions() const throw() {
634 std::vector
< std::vector
<std::string
> > opts
;
636 $
{all_modules_get_options
}
638 #line 614 "${template}"
643 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
644 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
646 }/* CVC4 namespace */