1 /********************* */
2 /*! \file options_template.cpp
4 ** Top contributors (to current version):
5 ** Morgan Deters, Tim King, Mathias Preiner
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief Contains code for handling command-line options.
14 ** Contains code for handling command-line options
17 #if !defined(_BSD_SOURCE) && defined(__MINGW32__) && !defined(__MINGW64__)
18 // force use of optreset; mingw32 croaks on argv-switching otherwise
19 # include "cvc4autoconfig.h"
21 # undef HAVE_DECL_OPTRESET
22 # define HAVE_DECL_OPTRESET 1
23 # define CVC4_IS_NOT_REALLY_BSD
24 #endif /* !_BSD_SOURCE && __MINGW32__ && !__MINGW64__ */
28 #endif /* __MINGW64__ */
33 #ifdef CVC4_IS_NOT_REALLY_BSD
35 #endif /* CVC4_IS_NOT_REALLY_BSD */
51 #include "base/check.h"
52 #include "base/exception.h"
53 #include "base/output.h"
54 #include "options/didyoumean.h"
55 #include "options/language.h"
56 #include "options/options_handler.h"
57 #include "options/options_listener.h"
62 #include "options/options_holder.h"
63 #include "cvc4autoconfig.h"
64 #include "options/base_handlers.h"
70 using namespace CVC4::options
;
74 thread_local Options
* Options::s_current
= NULL
;
77 * This is a default handler for options of built-in C++ type. This
78 * template is really just a helper for the handleOption() template,
79 * below. Variants of this template handle numeric and non-numeric,
80 * integral and non-integral, signed and unsigned C++ types.
81 * handleOption() makes sure to instantiate the right one.
83 * This implements default behavior when e.g. an option is
84 * unsigned but the user specifies a negative argument; etc.
86 template <class T
, bool is_numeric
, bool is_integer
>
87 struct OptionHandler
{
88 static T
handle(std::string option
, std::string optionarg
);
89 };/* struct OptionHandler<> */
91 /** Variant for integral C++ types */
93 struct OptionHandler
<T
, true, true> {
94 static bool stringToInt(T
& t
, const std::string
& str
) {
95 std::istringstream
ss(str
);
98 return !(ss
.fail() || ss
.get(tmp
));
101 static bool containsMinus(const std::string
& str
) {
102 return str
.find('-') != std::string::npos
;
105 static T
handle(const std::string
& option
, const std::string
& optionarg
) {
108 bool success
= stringToInt(i
, optionarg
);
111 throw OptionException(option
+ ": failed to parse "+ optionarg
+
112 " as an integer of the appropriate type.");
115 // Depending in the platform unsigned numbers with '-' signs may parse.
116 // Reject these by looking for any minus if it is not signed.
117 if( (! std::numeric_limits
<T
>::is_signed
) && containsMinus(optionarg
) ) {
118 // unsigned type but user gave negative argument
119 throw OptionException(option
+ " requires a nonnegative argument");
120 } else if(i
< std::numeric_limits
<T
>::min()) {
121 // negative overflow for type
122 std::stringstream ss
;
123 ss
<< option
<< " requires an argument >= "
124 << std::numeric_limits
<T
>::min();
125 throw OptionException(ss
.str());
126 } else if(i
> std::numeric_limits
<T
>::max()) {
127 // positive overflow for type
128 std::stringstream ss
;
129 ss
<< option
<< " requires an argument <= "
130 << std::numeric_limits
<T
>::max();
131 throw OptionException(ss
.str());
136 // if(std::numeric_limits<T>::is_signed) {
137 // return T(i.getLong());
139 // return T(i.getUnsignedLong());
141 } catch(std::invalid_argument
&) {
142 // user gave something other than an integer
143 throw OptionException(option
+ " requires an integer argument");
146 };/* struct OptionHandler<T, true, true> */
148 /** Variant for numeric but non-integral C++ types */
150 struct OptionHandler
<T
, true, false> {
151 static T
handle(std::string option
, std::string optionarg
) {
152 std::stringstream
in(optionarg
);
156 // we didn't consume the whole string (junk at end)
157 throw OptionException(option
+ " requires a numeric argument");
160 if(! std::numeric_limits
<T
>::is_signed
&& r
< 0.0) {
161 // unsigned type but user gave negative value
162 throw OptionException(option
+ " requires a nonnegative argument");
163 } else if(r
< -std::numeric_limits
<T
>::max()) {
164 // negative overflow for type
165 std::stringstream ss
;
166 ss
<< option
<< " requires an argument >= "
167 << -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 <= "
173 << std::numeric_limits
<T
>::max();
174 throw OptionException(ss
.str());
179 };/* struct OptionHandler<T, true, false> */
181 /** Variant for non-numeric C++ types */
183 struct OptionHandler
<T
, false, false> {
184 static T
handle(std::string option
, std::string optionarg
) {
185 T::unsupported_handleOption_call___please_write_me
;
186 // The above line causes a compiler error if this version of the template
187 // is ever instantiated (meaning that a specialization is missing). So
188 // don't worry about the segfault in the next line, the "return" is only
189 // there to keep the compiler from giving additional, distracting errors
193 };/* struct OptionHandler<T, false, false> */
195 /** Handle an option of type T in the default way. */
197 T
handleOption(std::string option
, std::string optionarg
) {
198 return OptionHandler
<T
, std::numeric_limits
<T
>::is_specialized
, std::numeric_limits
<T
>::is_integer
>::handle(option
, optionarg
);
201 /** Handle an option of type std::string in the default way. */
203 std::string handleOption
<std::string
>(std::string option
, std::string optionarg
) {
208 * Run handler, and any user-given predicates, for option T.
209 * If a user specifies a :handler or :predicates, it overrides this.
212 typename
T::type
runHandlerAndPredicates(T
, std::string option
, std::string optionarg
, options::OptionsHandler
* handler
) {
213 // By default, parse the option argument in a way appropriate for its type.
214 // E.g., for "unsigned int" options, ensure that the provided argument is
215 // a nonnegative integer that fits in the unsigned int type.
217 return handleOption
<typename
T::type
>(option
, optionarg
);
221 void runBoolPredicates(T
, std::string option
, bool b
, options::OptionsHandler
* handler
) {
222 // By default, nothing to do for bool. Users add things with
223 // :predicate in options files to provide custom checking routines
224 // that can throw exceptions.
228 Options::Options(OptionsListener
* ol
)
229 : d_holder(new options::OptionsHolder())
230 , d_handler(new options::OptionsHandler(this))
231 , d_beforeSearchListeners(),
235 Options::~Options() {
240 void Options::copyValues(const Options
& options
){
241 if(this != &options
) {
243 d_holder
= new options::OptionsHolder(*options
.d_holder
);
247 std::string
Options::formatThreadOptionException(const std::string
& option
) {
248 std::stringstream ss
;
249 ss
<< "can't understand option `" << option
250 << "': expected something like --threadN=\"--option1 --option2\","
251 << " where N is a nonnegative integer";
255 void Options::setListener(OptionsListener
* ol
) { d_olisten
= ol
; }
257 ListenerCollection::Registration
* Options::registerAndNotify(
258 ListenerCollection
& collection
, Listener
* listener
, bool notify
)
260 ListenerCollection::Registration
* registration
=
261 collection
.registerListener(listener
);
267 catch (OptionException
& e
)
269 // It can happen that listener->notify() throws an OptionException. In
270 // that case, we have to make sure that we delete the registration of our
271 // listener before rethrowing the exception. Otherwise the
272 // ListenerCollection deconstructor will complain that not all
273 // registrations have been removed before invoking the deconstructor.
275 throw OptionException(e
.getRawMessage());
281 ListenerCollection::Registration
* Options::registerBeforeSearchListener(
284 return d_beforeSearchListeners
.registerListener(listener
);
287 ListenerCollection::Registration
* Options::registerSetDefaultExprDepthListener(
288 Listener
* listener
, bool notifyIfSet
)
290 bool notify
= notifyIfSet
&& wasSetByUser(options::defaultExprDepth
);
291 return registerAndNotify(d_setDefaultExprDepthListeners
, listener
, notify
);
294 ListenerCollection::Registration
* Options::registerSetDefaultExprDagListener(
295 Listener
* listener
, bool notifyIfSet
)
297 bool notify
= notifyIfSet
&& wasSetByUser(options::defaultDagThresh
);
298 return registerAndNotify(d_setDefaultDagThreshListeners
, listener
, notify
);
301 ListenerCollection::Registration
* Options::registerSetPrintExprTypesListener(
302 Listener
* listener
, bool notifyIfSet
)
304 bool notify
= notifyIfSet
&& wasSetByUser(options::printExprTypes
);
305 return registerAndNotify(d_setPrintExprTypesListeners
, listener
, notify
);
308 ListenerCollection::Registration
* Options::registerSetDumpModeListener(
309 Listener
* listener
, bool notifyIfSet
)
311 bool notify
= notifyIfSet
&& wasSetByUser(options::dumpModeString
);
312 return registerAndNotify(d_setDumpModeListeners
, listener
, notify
);
315 ListenerCollection::Registration
* Options::registerSetPrintSuccessListener(
316 Listener
* listener
, bool notifyIfSet
)
318 bool notify
= notifyIfSet
&& wasSetByUser(options::printSuccess
);
319 return registerAndNotify(d_setPrintSuccessListeners
, listener
, notify
);
322 ListenerCollection::Registration
* Options::registerDumpToFileNameListener(
323 Listener
* listener
, bool notifyIfSet
)
325 bool notify
= notifyIfSet
&& wasSetByUser(options::dumpToFileName
);
326 return registerAndNotify(d_dumpToFileListeners
, listener
, notify
);
329 ListenerCollection::Registration
*
330 Options::registerSetRegularOutputChannelListener(
331 Listener
* listener
, bool notifyIfSet
)
333 bool notify
= notifyIfSet
&& wasSetByUser(options::regularChannelName
);
334 return registerAndNotify(d_setRegularChannelListeners
, listener
, notify
);
337 ListenerCollection::Registration
*
338 Options::registerSetDiagnosticOutputChannelListener(
339 Listener
* listener
, bool notifyIfSet
)
341 bool notify
= notifyIfSet
&& wasSetByUser(options::diagnosticChannelName
);
342 return registerAndNotify(d_setDiagnosticChannelListeners
, listener
, notify
);
347 #if defined(CVC4_MUZZLED) || defined(CVC4_COMPETITION_MODE)
348 # define DO_SEMANTIC_CHECKS_BY_DEFAULT false
349 #else /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
350 # define DO_SEMANTIC_CHECKS_BY_DEFAULT true
351 #endif /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
353 options::OptionsHolder::OptionsHolder() :
359 static const std::string mostCommonOptionsDescription
= "\
360 Most commonly-used CVC4 options:\n"
364 static const std::string optionsDescription
= mostCommonOptionsDescription
+ "\n\
366 Additional CVC4 options:\n"
370 static const std::string optionsFootnote
= "\n\
371 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
372 sense of the option.\n\
375 static const std::string languageDescription
=
377 Languages currently supported as arguments to the -L / --lang option:\n\
378 auto attempt to automatically determine language\n\
379 cvc4 | presentation | pl CVC4 presentation language\n\
380 smt | smtlib | smt2 |\n\
381 smt2.0 | smtlib2 | smtlib2.0 SMT-LIB format 2.0\n\
382 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
383 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
384 tptp TPTP format (cnf, fof and tff)\n\
385 sygus | sygus2 SyGuS version 2.0\n\
387 Languages currently supported as arguments to the --output-lang option:\n\
388 auto match output language to input language\n\
389 cvc4 | presentation | pl CVC4 presentation language\n\
390 cvc3 CVC3 presentation language\n\
391 smt | smtlib | smt2 |\n\
392 smt2.0 | smtlib2.0 | smtlib2 SMT-LIB format 2.0\n\
393 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
394 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
396 ast internal format (simple syntax trees)\n\
399 std::string
Options::getDescription() const {
400 return optionsDescription
;
403 void Options::printUsage(const std::string msg
, std::ostream
& out
) {
404 out
<< msg
<< optionsDescription
<< std::endl
405 << optionsFootnote
<< std::endl
<< std::flush
;
408 void Options::printShortUsage(const std::string msg
, std::ostream
& out
) {
409 out
<< msg
<< mostCommonOptionsDescription
<< std::endl
410 << optionsFootnote
<< std::endl
411 << "For full usage, please use --help."
412 << std::endl
<< std::endl
<< std::flush
;
415 void Options::printLanguageHelp(std::ostream
& out
) {
416 out
<< languageDescription
<< std::flush
;
420 * This is a table of long options. By policy, each short option
421 * should have an equivalent long option (but the reverse isn't the
422 * case), so this table should thus contain all command-line options.
424 * Each option in this array has four elements:
426 * 1. the long option string
427 * 2. argument behavior for the option:
428 * no_argument - no argument permitted
429 * required_argument - an argument is expected
430 * optional_argument - an argument is permitted but not required
431 * 3. this is a pointer to an int which is set to the 4th entry of the
432 * array if the option is present; or NULL, in which case
433 * getopt_long() returns the 4th entry
434 * 4. the return value for getopt_long() when this long option (or the
435 * value to set the 3rd entry to; see #3)
437 * If you add something here, you should add it in src/main/usage.h
438 * also, to document it.
440 * If you add something that has a short option equivalent, you should
441 * add it to the getopt_long() call in parseOptions().
443 static struct option cmdlineOptions
[] = {
445 { NULL
, no_argument
, NULL
, '\0' }
446 };/* cmdlineOptions */
450 /** Set a given Options* as "current" just for a particular scope. */
455 OptionsGuard(Options
** field
, Options
* opts
) :
463 };/* class OptionsGuard */
465 }/* CVC4::options namespace */
468 * Parse argc/argv and put the result into a CVC4::Options.
469 * The return value is what's left of the command line (that is, the
470 * non-option arguments).
472 * Throws OptionException on failures.
474 std::vector
<std::string
> Options::parseOptions(Options
* options
,
478 Assert(options
!= NULL
);
479 Assert(argv
!= NULL
);
481 options::OptionsGuard
guard(&s_current
, options
);
483 const char *progName
= argv
[0];
485 // To debug options parsing, you may prefer to simply uncomment this
486 // and recompile. Debug flags have not been parsed yet so these have
488 //DebugChannel.on("options");
490 Debug("options") << "Options::parseOptions == " << options
<< std::endl
;
491 Debug("options") << "argv == " << argv
<< std::endl
;
493 // Find the base name of the program.
494 const char *x
= strrchr(progName
, '/');
498 options
->d_holder
->binary_name
= std::string(progName
);
501 std::vector
<std::string
> nonoptions
;
502 parseOptionsRecursive(options
, argc
, argv
, &nonoptions
);
503 if(Debug
.isOn("options")){
504 for(std::vector
<std::string
>::const_iterator i
= nonoptions
.begin(),
505 iend
= nonoptions
.end(); i
!= iend
; ++i
){
506 Debug("options") << "nonoptions " << *i
<< std::endl
;
513 void Options::parseOptionsRecursive(Options
* options
,
516 std::vector
<std::string
>* nonoptions
)
519 if(Debug
.isOn("options")) {
520 Debug("options") << "starting a new parseOptionsRecursive with "
521 << argc
<< " arguments" << std::endl
;
522 for( int i
= 0; i
< argc
; i
++ ){
523 Assert(argv
[i
] != NULL
);
524 Debug("options") << " argv[" << i
<< "] = " << argv
[i
] << std::endl
;
528 // Having this synonym simplifies the generation code in mkoptions.
529 options::OptionsHandler
* handler
= options
->d_handler
;
531 // Reset getopt(), in the case of multiple calls to parseOptions().
532 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
534 #if HAVE_DECL_OPTRESET
535 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
536 #endif /* HAVE_DECL_OPTRESET */
538 // We must parse the binary name, which is manually ignored below. Setting
539 // this to 1 leads to incorrect behavior on some platforms.
544 while(true) { // Repeat Forever
547 std::string option
, optionarg
;
549 optind
= main_optind
;
550 old_optind
= main_optind
;
552 // If we encounter an element that is not at zero and does not start
553 // with a "-", this is a non-option. We consume this element as a
555 if (main_optind
> 0 && main_optind
< argc
&&
556 argv
[main_optind
][0] != '-') {
557 Debug("options") << "enqueueing " << argv
[main_optind
]
558 << " as a non-option." << std::endl
;
559 nonoptions
->push_back(argv
[main_optind
]);
565 Debug("options") << "[ before, main_optind == " << main_optind
<< " ]"
567 Debug("options") << "[ before, optind == " << optind
<< " ]" << std::endl
;
568 Debug("options") << "[ argc == " << argc
<< ", argv == " << argv
<< " ]"
570 int c
= getopt_long(argc
, argv
,
571 "+:${options_short}$",
572 cmdlineOptions
, NULL
);
574 main_optind
= optind
;
576 Debug("options") << "[ got " << int(c
) << " (" << char(c
) << ") ]"
577 << "[ next option will be at pos: " << optind
<< " ]"
580 // The initial getopt_long call should always determine that argv[0]
581 // is not an option and returns -1. We always manually advance beyond
583 if ( old_optind
== 0 && c
== -1 ) {
584 Assert(main_optind
> 0);
589 if(Debug
.isOn("options")) {
590 Debug("options") << "done with option parsing" << std::endl
;
591 for(int index
= optind
; index
< argc
; ++index
) {
592 Debug("options") << "remaining " << argv
[index
] << std::endl
;
598 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
599 optionarg
= (optarg
== NULL
) ? "" : optarg
;
601 Debug("preemptGetopt") << "processing option " << c
602 << " (`" << char(c
) << "'), " << option
<< std::endl
;
609 // This can be a long or short option, and the way to get at the
610 // name of it is different.
611 throw OptionException(std::string("option `") + option
+
612 "' missing its required argument");
616 throw OptionException(std::string("can't understand option `") + option
+
617 "'" + suggestCommandLineOptions(option
));
621 Debug("options") << "got " << nonoptions
->size()
622 << " non-option arguments." << std::endl
;
625 std::string
Options::suggestCommandLineOptions(const std::string
& optionName
)
627 DidYouMean didYouMean
;
630 for(size_t i
= 0; (opt
= cmdlineOptions
[i
].name
) != NULL
; ++i
) {
631 didYouMean
.addWord(std::string("--") + cmdlineOptions
[i
].name
);
634 return didYouMean
.getMatchAsString(optionName
.substr(0, optionName
.find('=')));
637 static const char* smtOptions
[] = {
642 std::vector
<std::string
> Options::suggestSmtOptions(
643 const std::string
& optionName
)
645 std::vector
<std::string
> suggestions
;
648 for(size_t i
= 0; (opt
= smtOptions
[i
]) != NULL
; ++i
) {
649 if(std::strstr(opt
, optionName
.c_str()) != NULL
) {
650 suggestions
.push_back(opt
);
657 std::vector
<std::vector
<std::string
> > Options::getOptions() const
659 std::vector
< std::vector
<std::string
> > opts
;
661 $
{options_getoptions
}$
667 void Options::setOption(const std::string
& key
, const std::string
& optionarg
)
669 Trace("options") << "setOption(" << key
<< ", " << optionarg
<< ")"
671 // first update this object
672 setOptionInternal(key
, optionarg
);
673 // then, notify the provided listener
674 if (d_olisten
!= nullptr)
676 d_olisten
->notifySetOption(key
);
680 void Options::setOptionInternal(const std::string
& key
,
681 const std::string
& optionarg
)
683 options::OptionsHandler
* handler
= d_handler
;
684 Options
* options
= this;
685 $
{setoption_handlers
}$
686 throw UnrecognizedOptionException(key
);
689 std::string
Options::getOption(const std::string
& key
) const
691 Trace("options") << "SMT getOption(" << key
<< ")" << std::endl
;
693 $
{getoption_handlers
}$
696 throw UnrecognizedOptionException(key
);
699 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
700 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT