2016-03-16 20:15:24 +00:00
|
|
|
#ifndef COMMAND_H
|
|
|
|
#define COMMAND_H
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include "shell.h"
|
|
|
|
#include "commandexecutor.h"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
2016-03-18 17:48:16 +00:00
|
|
|
namespace mshconsole{
|
|
|
|
class Command
|
|
|
|
{
|
2016-04-02 14:28:03 +00:00
|
|
|
class OptionNotParsedCorrectlyException {
|
|
|
|
string optarg;
|
|
|
|
public:
|
|
|
|
OptionNotParsedCorrectlyException(const string& s) : optarg(s) {}
|
|
|
|
string getOptarg(){
|
|
|
|
return optarg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CommandNameNotValidException {};
|
|
|
|
class NotRightArgException {};
|
|
|
|
|
|
|
|
vector<char> shortOptions;
|
2016-03-18 17:48:16 +00:00
|
|
|
const string name;
|
|
|
|
void checkObj();
|
2016-03-16 20:15:24 +00:00
|
|
|
|
2016-03-18 17:48:16 +00:00
|
|
|
public:
|
2016-04-02 14:28:03 +00:00
|
|
|
class LongData{
|
|
|
|
public:
|
|
|
|
enum States{
|
|
|
|
STRING=4,
|
|
|
|
TRUE=2,
|
|
|
|
FALSE=1
|
|
|
|
};
|
|
|
|
class MultipleDefinitionException{};
|
|
|
|
|
|
|
|
private:
|
|
|
|
string sLongData;
|
|
|
|
enum States state;
|
|
|
|
static const string EMPTY;
|
|
|
|
bool isSLongDataValorized;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LongData() : sLongData(), state(LongData::FALSE), isSLongDataValorized(false) {}
|
|
|
|
void set(bool b){
|
|
|
|
if(isSLongDataValorized) throw MultipleDefinitionException();
|
|
|
|
b ? state=LongData::TRUE : state=LongData::FALSE;
|
|
|
|
isSLongDataValorized=true;
|
|
|
|
}
|
|
|
|
void set(const string& s){
|
|
|
|
if(isSLongDataValorized) throw MultipleDefinitionException();
|
|
|
|
state=LongData::STRING;
|
|
|
|
sLongData=s;
|
|
|
|
isSLongDataValorized=true;
|
|
|
|
}
|
|
|
|
enum LongData::States getState() const{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
const string& getLongData() const{
|
|
|
|
if(state!=LongData::STRING) return EMPTY;
|
|
|
|
else return sLongData;
|
|
|
|
}
|
|
|
|
bool getBoolState() const{
|
|
|
|
return state!=LongData::FALSE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Command(const string& n, int (*funcptr)(CommandExecutor*, const vector<LongData*>&, const vector<bool>&, const char** argv));
|
2016-03-18 17:48:16 +00:00
|
|
|
Command(const Command&);
|
|
|
|
~Command();
|
|
|
|
string getName();
|
2016-04-02 14:28:03 +00:00
|
|
|
int execute(const vector<string>*, CommandExecutor*);
|
|
|
|
|
|
|
|
class LongOption{
|
|
|
|
string argName;
|
|
|
|
public:
|
|
|
|
enum States{
|
|
|
|
STRING=2,
|
|
|
|
BOOL=1
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
enum States want;
|
|
|
|
public:
|
|
|
|
LongOption(const string& s, enum States b) : argName(s), want(b) {}
|
|
|
|
enum States getWant() const{
|
|
|
|
return want;
|
|
|
|
}
|
|
|
|
const string& getArgName(){
|
|
|
|
return argName;
|
|
|
|
}
|
|
|
|
bool hasFlag(enum States s){
|
|
|
|
return want & s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void addLongOption(LongOption* a){
|
|
|
|
longOptions.push_back(a);
|
|
|
|
}
|
|
|
|
void addShortOption(char a){
|
|
|
|
shortOptions.push_back(a);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
vector<LongOption*> longOptions;
|
|
|
|
int (*funcCommand)(CommandExecutor*,const vector<LongData*>&, const vector<bool>&, const char** argv);
|
2016-03-18 17:48:16 +00:00
|
|
|
};
|
2016-04-02 14:28:03 +00:00
|
|
|
|
|
|
|
inline Command::LongOption::States operator|(Command::LongOption::States a, Command::LongOption::States b){
|
|
|
|
return static_cast<Command::LongOption::States>(static_cast<int>(a) | static_cast<int>(b));
|
|
|
|
}
|
2016-03-18 17:48:16 +00:00
|
|
|
}
|
2016-04-02 14:28:03 +00:00
|
|
|
|
|
|
|
|
2016-03-16 20:15:24 +00:00
|
|
|
|
|
|
|
#endif // COMMAND_H
|