Charades
command_line.h
Go to the documentation of this file.
1 
5 #ifndef CLARG_H_
6 #define CLARG_H_
7 
8 #include <charm++.h>
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 // Should parse return a bool for success/failure? Errors maybe should abort
14 // but just print the error and return false?
15 class Parser {
16 public:
17  virtual bool parse(std::string argument) const = 0;
18  virtual void print() const = 0;
19 };
20 
21 class AggregateParser : public Parser {
22 protected:
23  std::vector<Parser*> parsers;
24 public:
25  AggregateParser() {}
26 
27  void add_parser(Parser* p) {
28  parsers.push_back(p);
29  }
30 
31  bool parse(std::string argument) const {
32  for (int i = 0; i < parsers.size(); i++) {
33  if (parsers[i]->parse(argument)) {
34  return true;
35  }
36  }
37  return false;
38  }
39 
40  void print() const {
41  for (int i = 0; i < parsers.size(); i++) {
42  parsers[i]->print();
43  }
44  CkPrintf("\n");
45  }
46 };
47 
48 template <typename ArgType>
49 class Argument : public Parser {
50 private:
51  std::string name, description;
52  ArgType& location;
53 
54 public:
55  Argument(std::string n, std::string d, ArgType& l)
56  : name(n), description(d), location(l) {}
57 
58  bool parse(std::string argument) const {
59  std::stringstream stream(argument);
60  std::string lhs, rhs;
61  std::getline(stream, lhs, '=');
62  std::getline(stream, rhs, '=');
63 
64  // TODO: Error checking on >>
65  if (lhs != name) {
66  return false;
67  } else if (rhs.length() == 0) {
68  return false;
69  } else {
70  std::stringstream rhs_stream(rhs);
71  rhs_stream >> location;
72  return true;
73  }
74  }
75 
76  void print() const {
77  CkPrintf("%24s: %s\n", name.c_str(), description.c_str());
78  }
79 };
80 
81 // Treating this as a parser might be weird or might be fine
82 // To merge multiple sets, we could just add this as a parser to a superset, but
83 // adding one entry for each argument in it's map
84 // Or we can just make a more proper composite set
85 // We want to preserve set names and order for printing
89 class ArgumentSet : public AggregateParser {
90 private:
91  std::string name;
92  std::map<std::string, uint8_t> indices;
93 
94 public:
98  ArgumentSet(std::string n) : name(n) {}
99 
106  template <typename ArgType>
107  void register_argument(std::string name, std::string desc, ArgType& loc) {
108  if (indices.find(name) != indices.end()) {
109  if (CkMyPe() == 0) {
110  CkPrintf("Duplicate argument registration: %s\n", name.c_str());
111  CkAbort("Argument parse error\n");
112  }
113  }
114  indices[name] = parsers.size();
115  add_parser(new Argument<ArgType>(name, desc, loc));
116  }
117 
118  void print() const {
119  CkPrintf("\n%s %s\n", std::string(25,'=').c_str(), name.c_str());
120  AggregateParser::print();
121  }
122 };
123 
125 void tw_add_arguments(ArgumentSet* args);
126 void parse_command_line(int argc, char** argv);
127 
128 #endif
Definition: command_line.h:21
void tw_add_arguments(ArgumentSet *args)
Add a new set of arguments for the simulator to parse.
Definition: command_line.C:8
Definition: command_line.h:15
void register_argument(std::string name, std::string desc, ArgType &loc)
Register a new command line argument to be parsed.
Definition: command_line.h:107
ArgumentSet(std::string n)
Definition: command_line.h:98
Class for a set of arguments that can be read from the command line.
Definition: command_line.h:89
Definition: command_line.h:49