msh-console/msh-console-library/command.h

117 lines
3.3 KiB
C
Raw Normal View History

#ifndef COMMAND_H
#define COMMAND_H
#include <string>
#include <vector>
#include "shell.h"
#include "commandexecutor.h"
using std::string;
using std::vector;
namespace mshconsole{
class Command
{
class OptionNotParsedCorrectlyException {
string optarg;
public:
OptionNotParsedCorrectlyException(const string& s) : optarg(s) {}
string getOptarg(){
return optarg;
}
};
class CommandNameNotValidException {};
class NotRightArgException {};
vector<char> shortOptions;
const string name;
void checkObj();
public:
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));
Command(const Command&);
~Command();
string getName();
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);
};
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));
}
}
#endif // COMMAND_H