#ifndef COMMAND_H #define COMMAND_H #include #include #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 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&, const vector&, const char** argv)); Command(const Command&); ~Command(); string getName(); int execute(const vector*, 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 longOptions; int (*funcCommand)(CommandExecutor*,const vector&, const vector&, const char** argv); }; inline Command::LongOption::States operator|(Command::LongOption::States a, Command::LongOption::States b){ return static_cast(static_cast(a) | static_cast(b)); } } #endif // COMMAND_H