abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Options.cpp
Go to the documentation of this file.
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 "Sort.h"
21 #include "Options.h"
22 #include "ParseUtils.h"
23 
24 using namespace Minisat;
25 
26 int Minisat::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  return printUsageAndExit(argc, argv);
34  else if (match(str, "-verb"))
35  return 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()); return 0; } // exit(0);
48  else
49  argv[j++] = argv[i];
50  }
51  }
52 
53  argc -= (i - j);
54  return 1;
55 }
56 
57 
58 void Minisat::setUsageHelp (const char* str){ Option::getUsageString() = str; }
59 void Minisat::setHelpPrefixStr (const char* str){ Option::getHelpPrefixString() = str; }
60 int Minisat::printUsageAndExit (int argc, char** argv, bool verbose)
61 {
62  const char* usage = Option::getUsageString();
63  if (usage != NULL)
64  fprintf(stderr, usage, argv[0]);
65 
67 
68  const char* prev_cat = NULL;
69  const char* prev_type = NULL;
70 
71  for (int i = 0; i < Option::getOptionList().size(); i++){
72  const char* cat = Option::getOptionList()[i]->category;
73  const char* type = Option::getOptionList()[i]->type_name;
74 
75  if (cat != prev_cat)
76  fprintf(stderr, "\n%s OPTIONS:\n\n", cat);
77  else if (type != prev_type)
78  fprintf(stderr, "\n");
79 
80  Option::getOptionList()[i]->help(verbose);
81 
82  prev_cat = Option::getOptionList()[i]->category;
83  prev_type = Option::getOptionList()[i]->type_name;
84  }
85 
86  fprintf(stderr, "\nHELP OPTIONS:\n\n");
87  fprintf(stderr, " --%shelp Print help message.\n", Option::getHelpPrefixString());
88  fprintf(stderr, " --%shelp-verb Print verbose help message.\n", Option::getHelpPrefixString());
89  fprintf(stderr, "\n");
90 // exit(0);
91  return 0;
92 }
93 
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
void setHelpPrefixStr(const char *str)
Definition: Options.cpp:59
int parseOptions(int &argc, char **argv, bool strict=false)
Definition: Options.cpp:26
static const char *& getHelpPrefixString()
Definition: Options.h:58
static vec< Option * > & getOptionList()
Definition: Options.h:56
static const char *& getUsageString()
Definition: Options.h:57
static bool match(B &in, const char *str)
Definition: ParseUtils.h:99
void setUsageHelp(const char *str)
Definition: Options.cpp:58
int printUsageAndExit(int argc, char **argv, bool verbose=false)
Definition: Options.cpp:60