0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/tools/tool.h

94 lines
2.1 KiB
C
Raw Normal View History

2009-01-27 21:16:09 +01:00
// Tool.h
#pragma once
#include <string>
#include <boost/program_options.hpp>
2009-02-10 16:48:41 +01:00
#if defined(_WIN32)
#include <io.h>
#endif
2009-01-27 21:16:09 +01:00
#include "client/dbclient.h"
#include "db/instance.h"
2009-01-27 21:16:09 +01:00
using std::string;
namespace mongo {
2009-06-25 22:11:38 +02:00
2009-01-27 21:16:09 +01:00
class Tool {
public:
Tool( string name , string defaultDB="test" , string defaultCollection="");
virtual ~Tool();
int main( int argc , char ** argv );
2009-06-25 22:11:38 +02:00
2009-01-27 21:16:09 +01:00
boost::program_options::options_description_easy_init add_options(){
return _options->add_options();
}
boost::program_options::options_description_easy_init add_hidden_options(){
return _hidden_options->add_options();
}
2009-01-28 03:37:56 +01:00
void addPositionArg( const char * name , int pos ){
_positonalOptions.add( name , pos );
}
2009-06-25 22:11:38 +02:00
2009-01-27 21:16:09 +01:00
string getParam( string name , string def="" ){
if ( _params.count( name ) )
return _params[name.c_str()].as<string>();
return def;
}
2009-01-29 19:01:45 +01:00
bool hasParam( string name ){
return _params.count( name );
}
string getNS(){
if ( _coll.size() == 0 ){
cerr << "no collection specified!" << endl;
throw -1;
}
return _db + "." + _coll;
}
2009-01-27 21:16:09 +01:00
virtual int run() = 0;
2009-06-25 22:11:38 +02:00
virtual void printHelp(ostream &out);
virtual void printExtraHelp( ostream & out );
2009-09-01 21:53:31 +02:00
2009-01-27 21:16:09 +01:00
protected:
2009-08-12 22:31:22 +02:00
mongo::DBClientBase &conn( bool slaveIfPaired = false );
2009-08-12 22:31:22 +02:00
void auth( string db = "" );
2009-01-27 21:16:09 +01:00
string _name;
2009-09-01 21:53:31 +02:00
2009-01-27 21:16:09 +01:00
string _db;
string _coll;
2009-09-01 21:53:31 +02:00
2009-08-12 22:31:22 +02:00
string _username;
string _password;
2009-01-27 21:16:09 +01:00
2009-10-12 21:05:42 +02:00
void addFieldOptions();
void needFields();
2009-10-12 21:05:42 +02:00
vector<string> _fields;
BSONObj _fieldsObj;
2009-09-01 21:53:31 +02:00
2009-01-27 21:16:09 +01:00
private:
string _host;
mongo::DBClientBase * _conn;
bool _paired;
2009-09-01 21:53:31 +02:00
2009-01-27 21:16:09 +01:00
boost::program_options::options_description * _options;
boost::program_options::options_description * _hidden_options;
2009-01-28 03:37:56 +01:00
boost::program_options::positional_options_description _positonalOptions;
2009-01-27 21:16:09 +01:00
boost::program_options::variables_map _params;
2009-06-25 22:11:38 +02:00
2009-01-27 21:16:09 +01:00
};
2009-06-25 22:11:38 +02:00
2009-01-27 21:16:09 +01:00
}