yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Options.cc
Go to the documentation of this file.
1 #define __STDC_FORMAT_MACROS
2 #define __STDC_LIMIT_MACROS
3 /**************************************************************************************[Options.cc]
4 Copyright (c) 2008-2010, Niklas Sorensson
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 associated documentation files (the "Software"), to deal in the Software without restriction,
8 including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in all copies or
13 substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
19 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 **************************************************************************************************/
21 
22 #include "Sort.h"
23 #include "Options.h"
24 #include "ParseUtils.h"
25 
26 using namespace Minisat;
27 
28 void Minisat::parseOptions(int& argc, char** argv, bool strict)
29 {
30  int i, j;
31  for (i = j = 1; i < argc; i++){
32  const char* str = argv[i];
33  if (match(str, "--") && match(str, Option::getHelpPrefixString()) && match(str, "help")){
34  if (*str == '\0')
35  printUsageAndExit(argc, argv);
36  else if (match(str, "-verb"))
37  printUsageAndExit(argc, argv, true);
38  } else {
39  bool parsed_ok = false;
40 
41  for (int k = 0; !parsed_ok && k < Option::getOptionList().size(); k++){
42  parsed_ok = Option::getOptionList()[k]->parse(argv[i]);
43 
44  // fprintf(stderr, "checking %d: %s against flag <%s> (%s)\n", i, argv[i], Option::getOptionList()[k]->name, parsed_ok ? "ok" : "skip");
45  }
46 
47  if (!parsed_ok){
48  if (strict && match(argv[i], "-"))
49  fprintf(stderr, "ERROR! Unknown flag \"%s\". Use '--%shelp' for help.\n", argv[i], Option::getHelpPrefixString()), exit(1);
50  else
51  argv[j++] = argv[i];
52  }
53  }
54  }
55 
56  argc -= (i - j);
57 }
58 
59 
60 void Minisat::setUsageHelp (const char* str){ Option::getUsageString() = str; }
61 void Minisat::setHelpPrefixStr (const char* str){ Option::getHelpPrefixString() = str; }
62 void Minisat::printUsageAndExit (int /*argc*/, char** argv, bool verbose)
63 {
64  const char* usage = Option::getUsageString();
65  if (usage != NULL)
66  fprintf(stderr, usage, argv[0]);
67 
69 
70  const char* prev_cat = NULL;
71  const char* prev_type = NULL;
72 
73  for (int i = 0; i < Option::getOptionList().size(); i++){
74  const char* cat = Option::getOptionList()[i]->category;
75  const char* type = Option::getOptionList()[i]->type_name;
76 
77  if (cat != prev_cat)
78  fprintf(stderr, "\n%s OPTIONS:\n\n", cat);
79  else if (type != prev_type)
80  fprintf(stderr, "\n");
81 
82  Option::getOptionList()[i]->help(verbose);
83 
84  prev_cat = Option::getOptionList()[i]->category;
85  prev_type = Option::getOptionList()[i]->type_name;
86  }
87 
88  fprintf(stderr, "\nHELP OPTIONS:\n\n");
89  fprintf(stderr, " --%shelp Print help message.\n", Option::getHelpPrefixString());
90  fprintf(stderr, " --%shelp-verb Print verbose help message.\n", Option::getHelpPrefixString());
91  fprintf(stderr, "\n");
92  exit(0);
93 }
94 
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
void setHelpPrefixStr(const char *str)
Definition: Options.cc:61
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:96
void setUsageHelp(const char *str)
Definition: Options.cc:60
void printUsageAndExit(int argc, char **argv, bool verbose=false)
Definition: Options.cc:62
#define NULL
void parseOptions(int &argc, char **argv, bool strict=false)
Definition: Options.cc:28