Adding support for reading wrong assumptions
[satlib.git] / glucose-syrup / utils / Options.cc
1 /**************************************************************************************[Options.cc]
2 Copyright (c) 2008-2010, Niklas Sorensson
3
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:
9
10 The above copyright notice and this permission notice shall be included in all copies or
11 substantial portions of the Software.
12
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 **************************************************************************************************/
19
20 #include "mtl/Sort.h"
21 #include "utils/Options.h"
22 #include "utils/ParseUtils.h"
23
24 using namespace Glucose;
25
26 void Glucose::parseOptions(int& argc, char** argv, bool strict)
27 {
28     int i, j;
29     for (i = j = 1; i < argc; i++){
30         const char* str = argv[i];
31         if (match(str, "--") && match(str, Option::getHelpPrefixString()) && match(str, "help")){
32             if (*str == '\0')
33                 printUsageAndExit(argc, argv);
34             else if (match(str, "-verb"))
35                 printUsageAndExit(argc, argv, true);
36         } else {
37             bool parsed_ok = false;
38         
39             for (int k = 0; !parsed_ok && k < Option::getOptionList().size(); k++){
40                 parsed_ok = Option::getOptionList()[k]->parse(argv[i]);
41
42                 // fprintf(stderr, "checking %d: %s against flag <%s> (%s)\n", i, argv[i], Option::getOptionList()[k]->name, parsed_ok ? "ok" : "skip");
43             }
44
45             if (!parsed_ok)
46                 if (strict && match(argv[i], "-"))
47                     fprintf(stderr, "ERROR! Unknown flag \"%s\". Use '--%shelp' for help.\n", argv[i], Option::getHelpPrefixString()), exit(1);
48                 else
49                     argv[j++] = argv[i];
50         }
51     }
52
53     argc -= (i - j);
54 }
55
56
57 void Glucose::setUsageHelp      (const char* str){ Option::getUsageString() = str; }
58 void Glucose::setHelpPrefixStr  (const char* str){ Option::getHelpPrefixString() = str; }
59 void Glucose::printUsageAndExit (int argc, char** argv, bool verbose)
60 {
61     const char* usage = Option::getUsageString();
62     if (usage != NULL)
63         fprintf(stderr, usage, argv[0]);
64
65         sort(Option::getOptionList(), Option::OptionLt());
66
67     const char* prev_cat  = NULL;
68     const char* prev_type = NULL;
69
70     for (int i = 0; i < Option::getOptionList().size(); i++){
71         const char* cat  = Option::getOptionList()[i]->category;
72         const char* type = Option::getOptionList()[i]->type_name;
73
74         if (cat != prev_cat)
75             fprintf(stderr, "\n%s OPTIONS:\n\n", cat);
76         else if (type != prev_type)
77             fprintf(stderr, "\n");
78
79         Option::getOptionList()[i]->help(verbose);
80
81         prev_cat  = Option::getOptionList()[i]->category;
82         prev_type = Option::getOptionList()[i]->type_name;
83     }
84
85     fprintf(stderr, "\nHELP OPTIONS:\n\n");
86     fprintf(stderr, "  --%shelp        Print help message.\n", Option::getHelpPrefixString());
87     fprintf(stderr, "  --%shelp-verb   Print verbose help message.\n", Option::getHelpPrefixString());
88     fprintf(stderr, "\n");
89     exit(0);
90 }
91