MongoType
MongoDB Collection Data Dump with BSON Types
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parameters.cpp
Go to the documentation of this file.
1 
32 #include <Parameters.hpp>
33 
34 namespace po = boost::program_options;
35 
36 namespace mongotype {
37 
38 static bool initMap = true;
41 
42 static void mapperInit() {
43  if (initMap) {
44  initMap = false;
45  styleMapper.insert("dotted", STYLE_DOTTED);
46  styleMapper.insert("tree", STYLE_TREE);
47  styleMapper.insert("json", STYLE_JSON);
48  styleMapper.insert("jsonpacked", STYLE_JSONPACKED);
49  typeMapper.insert("none", TYPE_NONE);
50  typeMapper.insert("name", TYPE_NAME);
51  typeMapper.insert("desc", TYPE_DESC);
52  typeMapper.insert("code", TYPE_CODE);
53  typeMapper.insert("all", TYPE_ALL);
54  }
55 }
56 
57 Parameters::Parameters() : valid(false), port(DEFAULT_PORT), scalarFirst(false), style(STYLE_DOTTED), typeMask(TYPE_ALL) {
58  mapperInit();
59 }
60 
62 }
63 
64 void validate(boost::any& v,
65  const std::vector<std::string>& values,
66  StyleParam*, int)
67 {
68  po::validators::check_first_occurrence(v);
69  const string& s = po::validators::get_single_string(values);
70  StyleParam st = styleMapper.find(const_cast<string&>(s), STYLE_UNDEF);
71  if (st != STYLE_UNDEF) {
72  v = boost::any(st);
73  } else {
74  throw po::validation_error(po::validation_error::invalid_option_value);
75  }
76 }
77 
78 void validate(boost::any& v,
79  const std::vector<std::string>& values,
80  TypeParamMask*, int)
81 {
82  po::validators::check_first_occurrence(v);
83  const string& s = po::validators::get_single_string(values);
84  TypeParamMask t = typeMapper.find(const_cast<string&>(s), TYPE_UNDEF);
85  if (t != TYPE_UNDEF) {
86  v = boost::any(t);
87  } else {
88  throw po::validation_error(po::validation_error::invalid_option_value);
89  }
90 }
91 
92 int Parameters::parse(int ac, char* av[])
93 {
94  int rv = 0;
95  try {
96  // Options that will only be allowed on the command line.
97  po::options_description general("General Options");
98  general.add_options()
99  ("help", "print help message")
100  ("version,v", "print version string")
101  ("debug,d", "print debugging info")
102  ("stack,q", "print stack debugging info")
103  ("config,c", po::value<string>(&config_file)->default_value(DEFAULT_CONFIGURATION_FILE),
104  "path of configuration file. Default configuration file: " DEFAULT_CONFIGURATION_FILE)
105  ;
106 
107  // Options that will be allowed both on command line and in the configuration file.
108  po::options_description server("MongoDB Server Options");
109  server.add_options()
110  ("host,h", po::value<string>(&host)->default_value(DEFAULT_HOST),
111  "MongoDB server host name: <host>[:port]")
112  ("port,p", po::value<int>(&port)->default_value(DEFAULT_PORT),
113  "MongoDB server port number.")
114  ;
115 
116  // Options that will be allowed both on command line and in the configuration file.
117  po::options_description oformat("Output Format Options");
118  oformat.add_options()
119  ("style,s", po::value<StyleParam>(&style)->default_value(STYLE_DOTTED),
120  "Output Style: {dotted,tree,json,jsonpacked}.")
121  ("type,t", po::value<TypeParamMask>(&typeMask)->default_value(TYPE_ALL),
122  "BSON Type: {none,name,desc,code,all}.")
123  ("scalarfirst,f", po::value<bool>(&scalarFirst)->default_value(false),
124  "Output scalar objects elements before any embedded objects or arrays.")
125  ;
126 
127  // Hidden options, will be allowed both on command line and in config file, but will not be shown to the user.
128  po::options_description hidden("Hidden options");
129  hidden.add_options()
130  ("dbcollection", po::value<string>(&dbCollection), //->required(),
131  "Database and collection names concatenated with a '.' between them, i.e., \"mydb.mycollection\".")
132 // ("query", po::value<string>(&query)->default_value(string("")),
133 // "Optional JSON query, i.e., the first parameter to find()")
134 // ("projection", po::value<string>(&projection)->default_value(string("")),
135 // "Optional JSON query projection, i.e., the second parameter to find().")
136  ;
137 
138  po::options_description cmdline_options;
139  cmdline_options.add(general).add(server).add(oformat).add(hidden);
140 
141  po::options_description config_file_options;
142  config_file_options.add(server).add(oformat).add(hidden);
143 
144  po::options_description visible("\n\nSyntax:\n\tmongotype [<options>] <db.collection> [<query>] [<projection>]\n\nOptions");
145  visible.add(general).add(server).add(oformat);
146 
147  po::positional_options_description p;
148  p.add("dbcollection", 1);
149 // p.add("query", 1);
150 // p.add("projection", 1);
151 
152  store(po::command_line_parser(ac, av).options(cmdline_options).positional(p).run(), vm);
153  notify(vm);
154 
155  if (isDebug()) {
156  cout << "Parameters After Command Line:\n" << *this << "\n";
157  }
158 
159  ifstream ifs(config_file.c_str());
160  if (ifs)
161  {
162  store(parse_config_file(ifs, config_file_options), vm);
163  notify(vm);
164  ifs.close();
165  }
166 
167  if (vm.count("version")) {
168  cout << mongotype::VERSION << "\n";
169  exit(0);
170  }
171 
172  if (vm.count("help")) {
173  cout << visible << "\n";
174  exit(0);
175  }
176 
177  if (vm.count("dbcollection") == 0) {
178  cout << po::invalid_syntax(po::invalid_syntax::missing_parameter, "<db.collection>").what() << visible << "\n";
179  exit(0);
180  }
181 
182  valid = true;
183 
184  if (isDebug()) {
185  cout << "Parameters After Config File:\n" << *this << "\n";
186  }
187  }
188  catch(exception& e)
189  {
190  cerr << e.what() << "\n";
191  exit(1);
192  }
193  return rv;
194 }
195 
196 ostream& operator <<(ostream& os, Parameters& p) {
197  os << "valid:" << p.valid << "\n";
198  os << "config_file:" << p.config_file << "\n";
199  os << "host:" << p.host << "\n";
200  os << "port:" << p.port << "\n";
201  os << "style:" << p.style << "\n";
202  os << "typeMask:" << p.typeMask << "\n";
203  os << "scalarFirst:" << p.scalarFirst << "\n";
204  os << "dbCollection:" << p.dbCollection << "\n";
205 // os << "query:" << p.query << "\n";
206 // os << "projection:" << p.projection << "\n";
207  return os;
208 }
209 
210 } /* namespace mongotype */