1 /******************************************************************************
2 * Top contributors (to current version):
3 * Tim King, Gereon Kremer, 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 * Global (command-line, set-option, ...) parameters for SMT.
16 #include "options/options_public.h"
18 #if !defined(_BSD_SOURCE) && defined(__MINGW32__) && !defined(__MINGW64__)
19 // force use of optreset; mingw32 croaks on argv-switching otherwise
20 #include "base/cvc5config.h"
22 #undef HAVE_DECL_OPTRESET
23 #define HAVE_DECL_OPTRESET 1
24 #define CVC5_IS_NOT_REALLY_BSD
25 #endif /* !_BSD_SOURCE && __MINGW32__ && !__MINGW64__ */
29 #endif /* __MINGW64__ */
34 #ifdef CVC5_IS_NOT_REALLY_BSD
36 #endif /* CVC5_IS_NOT_REALLY_BSD */
38 #include "base/check.h"
39 #include "base/output.h"
40 #include "options/didyoumean.h"
41 #include "options/options_handler.h"
42 #include "options/options_listener.h"
43 #include "options/options.h"
44 #include "options/uf_options.h"
52 namespace cvc5::options
{
54 bool getUfHo(const Options
& opts
) { return opts
.uf
.ufHo
; }
57 static const std::string mostCommonOptionsDescription
=
59 Most commonly-used cvc5 options:\n"
63 static const std::string optionsDescription
=
64 mostCommonOptionsDescription
+ "\n\nAdditional cvc5 options:\n"
67 static const std::string optionsFootnote
= "\n\
68 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
69 sense of the option.\n\
72 static const std::string languageDescription
=
74 Languages currently supported as arguments to the -L / --lang option:\n\
75 auto attempt to automatically determine language\n\
76 cvc | presentation | pl CVC presentation language\n\
77 smt | smtlib | smt2 |\n\
78 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
79 tptp TPTP format (cnf, fof and tff)\n\
80 sygus | sygus2 SyGuS version 2.0\n\
82 Languages currently supported as arguments to the --output-lang option:\n\
83 auto match output language to input language\n\
84 cvc | presentation | pl CVC presentation language\n\
85 smt | smtlib | smt2 |\n\
86 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
88 ast internal format (simple syntax trees)\n\
92 const std::string
& getDescription()
94 return optionsDescription
;
97 void printUsage(const std::string
& msg
, std::ostream
& os
) {
98 os
<< msg
<< optionsDescription
<< std::endl
99 << optionsFootnote
<< std::endl
<< std::flush
;
102 void printShortUsage(const std::string
& msg
, std::ostream
& os
) {
103 os
<< msg
<< mostCommonOptionsDescription
<< std::endl
104 << optionsFootnote
<< std::endl
105 << "For full usage, please use --help."
106 << std::endl
<< std::endl
<< std::flush
;
109 void printLanguageHelp(std::ostream
& os
) {
110 os
<< languageDescription
<< std::flush
;
113 /** Set a given Options* as "current" just for a particular scope. */
118 OptionsGuard(Options
** field
, Options
* opts
) :
126 };/* class OptionsGuard */
129 * This is a table of long options. By policy, each short option
130 * should have an equivalent long option (but the reverse isn't the
131 * case), so this table should thus contain all command-line options.
133 * Each option in this array has four elements:
135 * 1. the long option string
136 * 2. argument behavior for the option:
137 * no_argument - no argument permitted
138 * required_argument - an argument is expected
139 * optional_argument - an argument is permitted but not required
140 * 3. this is a pointer to an int which is set to the 4th entry of the
141 * array if the option is present; or NULL, in which case
142 * getopt_long() returns the 4th entry
143 * 4. the return value for getopt_long() when this long option (or the
144 * value to set the 3rd entry to; see #3)
146 * If you add something here, you should add it in src/main/usage.h
147 * also, to document it.
149 * If you add something that has a short option equivalent, you should
150 * add it to the getopt_long() call in parse().
153 static struct option cmdlineOptions
[] = {
155 {nullptr, no_argument
, nullptr, '\0'}};
158 std::string
suggestCommandLineOptions(const std::string
& optionName
)
160 DidYouMean didYouMean
;
163 for(size_t i
= 0; (opt
= cmdlineOptions
[i
].name
) != nullptr; ++i
) {
164 didYouMean
.addWord(std::string("--") + cmdlineOptions
[i
].name
);
167 return didYouMean
.getMatchAsString(optionName
.substr(0, optionName
.find('=')));
171 * This is a default handler for options of built-in C++ type. This
172 * template is really just a helper for the handleOption() template,
173 * below. Variants of this template handle numeric and non-numeric,
174 * integral and non-integral, signed and unsigned C++ types.
175 * handleOption() makes sure to instantiate the right one.
177 * This implements default behavior when e.g. an option is
178 * unsigned but the user specifies a negative argument; etc.
180 template <class T
, bool is_numeric
, bool is_integer
>
181 struct OptionHandler
{
182 static T
handle(const std::string
& option
, const std::string
& flag
, const std::string
& optionarg
);
183 };/* struct OptionHandler<> */
185 /** Variant for integral C++ types */
187 struct OptionHandler
<T
, true, true> {
188 static bool stringToInt(T
& t
, const std::string
& str
) {
189 std::istringstream
ss(str
);
192 return !(ss
.fail() || ss
.get(tmp
));
195 static bool containsMinus(const std::string
& str
) {
196 return str
.find('-') != std::string::npos
;
199 static T
handle(const std::string
& option
, const std::string
& flag
, const std::string
& optionarg
) {
202 bool success
= stringToInt(i
, optionarg
);
205 throw OptionException(flag
+ ": failed to parse "+ optionarg
+
206 " as an integer of the appropriate type.");
209 // Depending in the platform unsigned numbers with '-' signs may parse.
210 // Reject these by looking for any minus if it is not signed.
211 if( (! std::numeric_limits
<T
>::is_signed
) && containsMinus(optionarg
) ) {
212 // unsigned type but user gave negative argument
213 throw OptionException(flag
+ " requires a nonnegative argument");
214 } else if(i
< std::numeric_limits
<T
>::min()) {
215 // negative overflow for type
216 std::stringstream ss
;
217 ss
<< flag
<< " requires an argument >= "
218 << std::numeric_limits
<T
>::min();
219 throw OptionException(ss
.str());
220 } else if(i
> std::numeric_limits
<T
>::max()) {
221 // positive overflow for type
222 std::stringstream ss
;
223 ss
<< flag
<< " requires an argument <= "
224 << std::numeric_limits
<T
>::max();
225 throw OptionException(ss
.str());
230 // if(std::numeric_limits<T>::is_signed) {
231 // return T(i.getLong());
233 // return T(i.getUnsignedLong());
235 } catch(std::invalid_argument
&) {
236 // user gave something other than an integer
237 throw OptionException(flag
+ " requires an integer argument");
240 };/* struct OptionHandler<T, true, true> */
242 /** Variant for numeric but non-integral C++ types */
244 struct OptionHandler
<T
, true, false> {
245 static T
handle(const std::string
& option
, const std::string
& flag
, const std::string
& optionarg
) {
246 std::stringstream
inss(optionarg
);
250 // we didn't consume the whole string (junk at end)
251 throw OptionException(flag
+ " requires a numeric argument");
254 if(! std::numeric_limits
<T
>::is_signed
&& r
< 0.0) {
255 // unsigned type but user gave negative value
256 throw OptionException(flag
+ " requires a nonnegative argument");
257 } else if(r
< -std::numeric_limits
<T
>::max()) {
258 // negative overflow for type
259 std::stringstream ss
;
260 ss
<< flag
<< " requires an argument >= "
261 << -std::numeric_limits
<T
>::max();
262 throw OptionException(ss
.str());
263 } else if(r
> std::numeric_limits
<T
>::max()) {
264 // positive overflow for type
265 std::stringstream ss
;
266 ss
<< flag
<< " requires an argument <= "
267 << std::numeric_limits
<T
>::max();
268 throw OptionException(ss
.str());
273 };/* struct OptionHandler<T, true, false> */
275 /** Variant for non-numeric C++ types */
277 struct OptionHandler
<T
, false, false> {
278 static T
handle(const std::string
& option
, const std::string
& flag
, const std::string
& optionarg
) {
279 T::unsupported_handleOption_call___please_write_me
;
280 // The above line causes a compiler error if this version of the template
281 // is ever instantiated (meaning that a specialization is missing). So
282 // don't worry about the segfault in the next line, the "return" is only
283 // there to keep the compiler from giving additional, distracting errors
287 };/* struct OptionHandler<T, false, false> */
289 /** Handle an option of type T in the default way. */
291 T
handleOption(const std::string
& option
, const std::string
& flag
, const std::string
& optionarg
) {
292 return OptionHandler
<T
, std::numeric_limits
<T
>::is_specialized
, std::numeric_limits
<T
>::is_integer
>::handle(option
, flag
, optionarg
);
295 /** Handle an option of type std::string in the default way. */
297 std::string handleOption
<std::string
>(const std::string
& option
, const std::string
& flag
, const std::string
& optionarg
) {
305 void parseInternal(Options
& opts
, int argc
,
307 std::vector
<std::string
>& nonoptions
)
309 Assert(argv
!= nullptr);
310 if(Debug
.isOn("options")) {
311 Debug("options") << "starting a new parseInternal with "
312 << argc
<< " arguments" << std::endl
;
313 for( int i
= 0; i
< argc
; i
++ ){
314 Assert(argv
[i
] != NULL
);
315 Debug("options") << " argv[" << i
<< "] = " << argv
[i
] << std::endl
;
319 // Reset getopt(), in the case of multiple calls to parse().
320 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
322 #if HAVE_DECL_OPTRESET
323 optreset
= 1; // on BSD getopt() (e.g. Mac OS), might need this
324 #endif /* HAVE_DECL_OPTRESET */
326 // We must parse the binary name, which is manually ignored below. Setting
327 // this to 1 leads to incorrect behavior on some platforms.
332 while(true) { // Repeat Forever
335 std::string option
, optionarg
;
337 optind
= main_optind
;
338 old_optind
= main_optind
;
340 // If we encounter an element that is not at zero and does not start
341 // with a "-", this is a non-option. We consume this element as a
343 if (main_optind
> 0 && main_optind
< argc
&&
344 argv
[main_optind
][0] != '-') {
345 Debug("options") << "enqueueing " << argv
[main_optind
]
346 << " as a non-option." << std::endl
;
347 nonoptions
.push_back(argv
[main_optind
]);
353 Debug("options") << "[ before, main_optind == " << main_optind
<< " ]"
355 Debug("options") << "[ before, optind == " << optind
<< " ]" << std::endl
;
356 Debug("options") << "[ argc == " << argc
<< ", argv == " << argv
<< " ]"
359 int c
= getopt_long(argc
, argv
,
360 "+:${options_short}$",
361 cmdlineOptions
, NULL
);
364 main_optind
= optind
;
366 Debug("options") << "[ got " << int(c
) << " (" << char(c
) << ") ]"
367 << "[ next option will be at pos: " << optind
<< " ]"
370 // The initial getopt_long call should always determine that argv[0]
371 // is not an option and returns -1. We always manually advance beyond
373 if ( old_optind
== 0 && c
== -1 ) {
374 Assert(main_optind
> 0);
379 if(Debug
.isOn("options")) {
380 Debug("options") << "done with option parsing" << std::endl
;
381 for(int index
= optind
; index
< argc
; ++index
) {
382 Debug("options") << "remaining " << argv
[index
] << std::endl
;
388 option
= argv
[old_optind
== 0 ? 1 : old_optind
];
389 optionarg
= (optarg
== NULL
) ? "" : optarg
;
391 Debug("preemptGetopt") << "processing option " << c
392 << " (`" << char(c
) << "'), " << option
<< std::endl
;
400 // This can be a long or short option, and the way to get at the
401 // name of it is different.
402 throw OptionException(std::string("option `") + option
403 + "' missing its required argument");
407 throw OptionException(std::string("can't understand option `") + option
408 + "'" + suggestCommandLineOptions(option
));
413 Debug("options") << "got " << nonoptions
.size()
414 << " non-option arguments." << std::endl
;
418 * Parse argc/argv and put the result into a cvc5::Options.
419 * The return value is what's left of the command line (that is, the
420 * non-option arguments).
422 * Throws OptionException on failures.
424 std::vector
<std::string
> parse(
425 Options
& opts
, int argc
, char* argv
[], std::string
& binaryName
)
427 Assert(argv
!= nullptr);
429 Options
* cur
= &Options::current();
430 OptionsGuard
guard(&cur
, &opts
);
432 const char *progName
= argv
[0];
434 // To debug options parsing, you may prefer to simply uncomment this
435 // and recompile. Debug flags have not been parsed yet so these have
437 //DebugChannel.on("options");
439 Debug("options") << "options::parse == " << &opts
<< std::endl
;
440 Debug("options") << "argv == " << argv
<< std::endl
;
442 // Find the base name of the program.
443 const char *x
= strrchr(progName
, '/');
447 binaryName
= std::string(progName
);
449 std::vector
<std::string
> nonoptions
;
450 parseInternal(opts
, argc
, argv
, nonoptions
);
451 if (Debug
.isOn("options")){
452 for(std::vector
<std::string
>::const_iterator i
= nonoptions
.begin(),
453 iend
= nonoptions
.end(); i
!= iend
; ++i
){
454 Debug("options") << "nonoptions " << *i
<< std::endl
;
461 std::string
get(const Options
& options
, const std::string
& key
)
463 Trace("options") << "Options::getOption(" << key
<< ")" << std::endl
;
464 $
{getoption_handlers
}$
466 throw UnrecognizedOptionException(key
);
469 void setInternal(Options
& opts
, const std::string
& key
,
470 const std::string
& optionarg
)
472 $
{setoption_handlers
}$
473 throw UnrecognizedOptionException(key
);
476 void set(Options
& opts
, const std::string
& key
, const std::string
& optionarg
)
479 Trace("options") << "setOption(" << key
<< ", " << optionarg
<< ")"
481 // first update this object
482 setInternal(opts
, key
, optionarg
);
483 // then, notify the provided listener
484 opts
.notifyListener(key
);
487 std::vector
<std::vector
<std::string
> > getAll(const Options
& opts
)
489 std::vector
<std::vector
<std::string
>> res
;
496 } // namespace cvc5::options