1 /******************************************************************************
2 * Top contributors (to current version):
3 * Morgan Deters, Tim King, Andrew Reynolds
5 * This file is part of the cvc5 project.
7 * Copyright (c) 2009-2021 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.
11 * ****************************************************************************
13 * Contains code for handling command-line options.
16 #if !defined(_BSD_SOURCE) && defined(__MINGW32__) && !defined(__MINGW64__)
17 // force use of optreset; mingw32 croaks on argv-switching otherwise
18 #include "base/cvc5config.h"
20 #undef HAVE_DECL_OPTRESET
21 #define HAVE_DECL_OPTRESET 1
22 #define CVC5_IS_NOT_REALLY_BSD
23 #endif /* !_BSD_SOURCE && __MINGW32__ && !__MINGW64__ */
27 #endif /* __MINGW64__ */
32 #ifdef CVC5_IS_NOT_REALLY_BSD
34 #endif /* CVC5_IS_NOT_REALLY_BSD */
49 #include "base/check.h"
50 #include "base/exception.h"
51 #include "base/output.h"
52 #include "options/didyoumean.h"
53 #include "options/language.h"
54 #include "options/options_handler.h"
55 #include "options/options_listener.h"
60 #include "base/cvc5config.h"
61 #include "options/base_handlers.h"
66 using namespace cvc5::options
;
71 thread_local Options
* Options::s_current
= NULL
;
74 * This is a default handler for options of built-in C++ type. This
75 * template is really just a helper for the handleOption() template,
76 * below. Variants of this template handle numeric and non-numeric,
77 * integral and non-integral, signed and unsigned C++ types.
78 * handleOption() makes sure to instantiate the right one.
80 * This implements default behavior when e.g. an option is
81 * unsigned but the user specifies a negative argument; etc.
83 template <class T
, bool is_numeric
, bool is_integer
>
84 struct OptionHandler
{
85 static T
handle(std::string option
, std::string optionarg
);
86 };/* struct OptionHandler<> */
88 /** Variant for integral C++ types */
90 struct OptionHandler
<T
, true, true> {
91 static bool stringToInt(T
& t
, const std::string
& str
) {
92 std::istringstream
ss(str
);
95 return !(ss
.fail() || ss
.get(tmp
));
98 static bool containsMinus(const std::string
& str
) {
99 return str
.find('-') != std::string::npos
;
102 static T
handle(const std::string
& option
, const std::string
& optionarg
) {
105 bool success
= stringToInt(i
, optionarg
);
108 throw OptionException(option
+ ": failed to parse "+ optionarg
+
109 " as an integer of the appropriate type.");
112 // Depending in the platform unsigned numbers with '-' signs may parse.
113 // Reject these by looking for any minus if it is not signed.
114 if( (! std::numeric_limits
<T
>::is_signed
) && containsMinus(optionarg
) ) {
115 // unsigned type but user gave negative argument
116 throw OptionException(option
+ " requires a nonnegative argument");
117 } else if(i
< std::numeric_limits
<T
>::min()) {
118 // negative overflow for type
119 std::stringstream ss
;
120 ss
<< option
<< " requires an argument >= "
121 << std::numeric_limits
<T
>::min();
122 throw OptionException(ss
.str());
123 } else if(i
> std::numeric_limits
<T
>::max()) {
124 // positive overflow for type
125 std::stringstream ss
;
126 ss
<< option
<< " requires an argument <= "
127 << std::numeric_limits
<T
>::max();
128 throw OptionException(ss
.str());
133 // if(std::numeric_limits<T>::is_signed) {
134 // return T(i.getLong());
136 // return T(i.getUnsignedLong());
138 } catch(std::invalid_argument
&) {
139 // user gave something other than an integer
140 throw OptionException(option
+ " requires an integer argument");
143 };/* struct OptionHandler<T, true, true> */
145 /** Variant for numeric but non-integral C++ types */
147 struct OptionHandler
<T
, true, false> {
148 static T
handle(std::string option
, std::string optionarg
) {
149 std::stringstream
in(optionarg
);
153 // we didn't consume the whole string (junk at end)
154 throw OptionException(option
+ " requires a numeric argument");
157 if(! std::numeric_limits
<T
>::is_signed
&& r
< 0.0) {
158 // unsigned type but user gave negative value
159 throw OptionException(option
+ " requires a nonnegative argument");
160 } else if(r
< -std::numeric_limits
<T
>::max()) {
161 // negative overflow for type
162 std::stringstream ss
;
163 ss
<< option
<< " requires an argument >= "
164 << -std::numeric_limits
<T
>::max();
165 throw OptionException(ss
.str());
166 } else if(r
> std::numeric_limits
<T
>::max()) {
167 // positive overflow for type
168 std::stringstream ss
;
169 ss
<< option
<< " requires an argument <= "
170 << std::numeric_limits
<T
>::max();
171 throw OptionException(ss
.str());
176 };/* struct OptionHandler<T, true, false> */
178 /** Variant for non-numeric C++ types */
180 struct OptionHandler
<T
, false, false> {
181 static T
handle(std::string option
, std::string optionarg
) {
182 T::unsupported_handleOption_call___please_write_me
;
183 // The above line causes a compiler error if this version of the template
184 // is ever instantiated (meaning that a specialization is missing). So
185 // don't worry about the segfault in the next line, the "return" is only
186 // there to keep the compiler from giving additional, distracting errors
190 };/* struct OptionHandler<T, false, false> */
192 /** Handle an option of type T in the default way. */
194 T
handleOption(std::string option
, std::string optionarg
) {
195 return OptionHandler
<T
, std::numeric_limits
<T
>::is_specialized
, std::numeric_limits
<T
>::is_integer
>::handle(option
, optionarg
);
198 /** Handle an option of type std::string in the default way. */
200 std::string handleOption
<std::string
>(std::string option
, std::string optionarg
) {
205 * Run handler, and any user-given predicates, for option T.
206 * If a user specifies a :handler or :predicates, it overrides this.
209 typename
T::type
runHandlerAndPredicates(T
, std::string option
, std::string optionarg
, options::OptionsHandler
* handler
) {
210 // By default, parse the option argument in a way appropriate for its type.
211 // E.g., for "unsigned int" options, ensure that the provided argument is
212 // a nonnegative integer that fits in the unsigned int type.
214 return handleOption
<typename
T::type
>(option
, optionarg
);
218 void runBoolPredicates(T
, std::string option
, bool b
, options::OptionsHandler
* handler
) {
219 // By default, nothing to do for bool. Users add things with
220 // :predicate in options files to provide custom checking routines
221 // that can throw exceptions.
224 Options::Options(OptionsListener
* ol
)
225 : d_handler(new options::OptionsHandler(this)),
233 Options::~Options() {
237 void Options::copyValues(const Options
& options
){
238 if(this != &options
) {
245 std::string
Options::formatThreadOptionException(const std::string
& option
) {
246 std::stringstream ss
;
247 ss
<< "can't understand option `" << option
248 << "': expected something like --threadN=\"--option1 --option2\","
249 << " where N is a nonnegative integer";
253 void Options::setListener(OptionsListener
* ol
) { d_olisten
= ol
; }
259 static const std::string mostCommonOptionsDescription
=
261 Most commonly-used cvc5 options:\n"
268 static const std::string optionsDescription
=
269 mostCommonOptionsDescription
+ "\n\nAdditional cvc5 options:\n"
273 static const std::string optionsFootnote
= "\n\
274 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
275 sense of the option.\n\
278 static const std::string languageDescription
=
280 Languages currently supported as arguments to the -L / --lang option:\n\
281 auto attempt to automatically determine language\n\
282 cvc | presentation | pl CVC presentation language\n\
283 smt | smtlib | smt2 |\n\
284 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
285 tptp TPTP format (cnf, fof and tff)\n\
286 sygus | sygus2 SyGuS version 2.0\n\
288 Languages currently supported as arguments to the --output-lang option:\n\
289 auto match output language to input language\n\
290 cvc | presentation | pl CVC presentation language\n\
291 smt | smtlib | smt2 |\n\
292 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
294 ast internal format (simple syntax trees)\n\
297 std::string
Options::getDescription() const {
298 return optionsDescription
;
301 void Options::printUsage(const std::string msg
, std::ostream
& out
) {
302 out
<< msg
<< optionsDescription
<< std::endl
303 << optionsFootnote
<< std::endl
<< std::flush
;
306 void Options::printShortUsage(const std::string msg
, std::ostream
& out
) {
307 out
<< msg
<< mostCommonOptionsDescription
<< std::endl
308 << optionsFootnote
<< std::endl
309 << "For full usage, please use --help."
310 << std::endl
<< std::endl
<< std::flush
;
313 void Options::printLanguageHelp(std::ostream
& out
) {
314 out
<< languageDescription
<< std::flush
;
318 * This is a table of long options. By policy, each short option
319 * should have an equivalent long option (but the reverse isn't the
320 * case), so this table should thus contain all command-line options.
322 * Each option in this array has four elements:
324 * 1. the long option string
325 * 2. argument behavior for the option:
326 * no_argument - no argument permitted
327 * required_argument - an argument is expected
328 * optional_argument - an argument is permitted but not required
329 * 3. this is a pointer to an int which is set to the 4th entry of the
330 * array if the option is present; or NULL, in which case
331 * getopt_long() returns the 4th entry
332 * 4. the return value for getopt_long() when this long option (or the
333 * value to set the 3rd entry to; see #3)
335 * If you add something here, you should add it in src/main/usage.h
336 * also, to document it.
338 * If you add something that has a short option equivalent, you should
339 * add it to the getopt_long() call in parseOptions().
342 static struct option cmdlineOptions
[] = {
344 {nullptr, no_argument
, nullptr, '\0'}};
349 /** Set a given Options* as "current" just for a particular scope. */
354 OptionsGuard(Options
** field
, Options
* opts
) :
362 };/* class OptionsGuard */
364 } // namespace options
367 * Parse argc/argv and put the result into a cvc5::Options.
368 * The return value is what's left of the command line (that is, the
369 * non-option arguments).
371 * Throws OptionException on failures.
373 std::vector
<std::string
> Options::parseOptions(Options
* options
,
377 Assert(options
!= NULL
);
378 Assert(argv
!= NULL
);
380 options::OptionsGuard
guard(&s_current
, options
);
382 const char *progName
= argv
[0];
384 // To debug options parsing, you may prefer to simply uncomment this
385 // and recompile. Debug flags have not been parsed yet so these have
387 //DebugChannel.on("options");
389 Debug("options") << "Options::parseOptions == " << options
<< std::endl
;
390 Debug("options") << "argv == " << argv
<< std::endl
;
392 // Find the base name of the program.
393 const char *x
= strrchr(progName
, '/');
397 options
->base
.binary_name
= std::string(progName
);
399 std::vector
<std::string
> nonoptions
;
400 options
->parseOptionsRecursive(argc
, argv
, &nonoptions
);
401 if(Debug
.isOn("options")){
402 for(std::vector
<std::string
>::const_iterator i
= nonoptions
.begin(),
403 iend
= nonoptions
.end(); i
!= iend
; ++i
){
404 Debug("options") << "nonoptions " << *i
<< std::endl
;
411 std::string
suggestCommandLineOptions(const std::string
& optionName
)
413 DidYouMean didYouMean
;
416 for(size_t i
= 0; (opt
= cmdlineOptions
[i
].name
) != nullptr; ++i
) {
417 didYouMean
.addWord(std::string("--") + cmdlineOptions
[i
].name
);
420 return didYouMean
.getMatchAsString(optionName
.substr(0, optionName
.find('=')));
423 void Options::parseOptionsRecursive(int argc
,
425 std::vector
<std::string
>* nonoptions
)
428 if(Debug
.isOn("options")) {
429 Debug("options") << "starting a new parseOptionsRecursive with "
430 << argc
<< " arguments" << std::endl
;
431 for( int i
= 0; i
< argc
; i
++ ){
432 Assert(argv
[i
] != NULL
);
433 Debug("options") << " argv[" << i
<< "] = " << argv
[i
] << std::endl
;
437 // Reset getopt(), in the case of multiple calls to parseOptions().
438 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
440 #if HAVE_DECL_OPTRESET
441 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
442 #endif /* HAVE_DECL_OPTRESET */
444 // We must parse the binary name, which is manually ignored below. Setting
445 // this to 1 leads to incorrect behavior on some platforms.
450 while(true) { // Repeat Forever
453 std::string option
, optionarg
;
455 optind
= main_optind
;
456 old_optind
= main_optind
;
458 // If we encounter an element that is not at zero and does not start
459 // with a "-", this is a non-option. We consume this element as a
461 if (main_optind
> 0 && main_optind
< argc
&&
462 argv
[main_optind
][0] != '-') {
463 Debug("options") << "enqueueing " << argv
[main_optind
]
464 << " as a non-option." << std::endl
;
465 nonoptions
->push_back(argv
[main_optind
]);
471 Debug("options") << "[ before, main_optind == " << main_optind
<< " ]"
473 Debug("options") << "[ before, optind == " << optind
<< " ]" << std::endl
;
474 Debug("options") << "[ argc == " << argc
<< ", argv == " << argv
<< " ]"
477 int c
= getopt_long(argc
, argv
,
478 "+:${options_short}$",
479 cmdlineOptions
, NULL
);
482 main_optind
= optind
;
484 Debug("options") << "[ got " << int(c
) << " (" << char(c
) << ") ]"
485 << "[ next option will be at pos: " << optind
<< " ]"
488 // The initial getopt_long call should always determine that argv[0]
489 // is not an option and returns -1. We always manually advance beyond
491 if ( old_optind
== 0 && c
== -1 ) {
492 Assert(main_optind
> 0);
497 if(Debug
.isOn("options")) {
498 Debug("options") << "done with option parsing" << std::endl
;
499 for(int index
= optind
; index
< argc
; ++index
) {
500 Debug("options") << "remaining " << argv
[index
] << std::endl
;
506 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
507 optionarg
= (optarg
== NULL
) ? "" : optarg
;
509 Debug("preemptGetopt") << "processing option " << c
510 << " (`" << char(c
) << "'), " << option
<< std::endl
;
518 // This can be a long or short option, and the way to get at the
519 // name of it is different.
520 throw OptionException(std::string("option `") + option
521 + "' missing its required argument");
525 throw OptionException(std::string("can't understand option `") + option
526 + "'" + suggestCommandLineOptions(option
));
531 Debug("options") << "got " << nonoptions
->size()
532 << " non-option arguments." << std::endl
;
536 std::vector
<std::vector
<std::string
> > Options::getOptions() const
538 std::vector
< std::vector
<std::string
> > opts
;
540 $
{options_getoptions
}$
546 void Options::setOption(const std::string
& key
, const std::string
& optionarg
)
548 Trace("options") << "setOption(" << key
<< ", " << optionarg
<< ")"
550 // first update this object
551 setOptionInternal(key
, optionarg
);
552 // then, notify the provided listener
553 if (d_olisten
!= nullptr)
555 d_olisten
->notifySetOption(key
);
560 void Options::setOptionInternal(const std::string
& key
,
561 const std::string
& optionarg
)
563 options::OptionsHandler
* handler
= d_handler
;
564 $
{setoption_handlers
}$
565 throw UnrecognizedOptionException(key
);
570 std::string
Options::getOption(const std::string
& key
) const
572 Trace("options") << "Options::getOption(" << key
<< ")" << std::endl
;
573 $
{getoption_handlers
}$
575 throw UnrecognizedOptionException(key
);