#include "shell.h" namespace mshconsole { Command::Command(const string& n, int (*funcptr)(CommandExecutor*,const vector&,const vector&, const char** argv)) : name(n), funcCommand(funcptr){ checkObj(); } int Command::execute(const vector* args, CommandExecutor* c){ if(args==nullptr) return -1; vector data; for(size_t i=0; i sLongData; for(size_t i=0; i argv(args->size()); size_t i; for (i = 0; i < args->size(); ++i) { argv[i] = const_cast(&(args->operator[](i)[0])); } int argc=i; //TODO: quotes for long options try{ int c; opterr=0; optind=1; while ((c = getopt (argc, argv.data(), getoptarg.c_str())) != -1){ if((c>='a'&&c<='z')||(c>='A'&&c>='Z')){ for(size_t i=0; igetArgName()+"=")==0){ if(!(longOptions.at(i)->hasFlag(LongOption::STRING))){ throw NotRightArgException(); } data[i]->set(l.substr(longOptions[i]->getArgName().size()+1,l.size())); getout = true; break; } if(l==longOptions[i]->getArgName()){ data[i]->set(true); getout=true; break; } } } catch(LongData::MultipleDefinitionException){ getout=false; } if(!getout) throw OptionNotParsedCorrectlyException(static_cast("--") + optarg); } else if(c=='?'){ //if (optopt == 'c') // fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt); //return 1; } else //abort(); break; } //demo for debug /* for(size_t i=0; igetArgName() << ": "; LongData* now = data[i]; if(now->getState()==LongData::States::STRING) cout << now->getLongData(); else cout << now->getBoolState(); cout << "\n"; } for(size_t i=0; i(argv.data())); } string Command::getName(){ return this->name; } Command::Command(const Command& old) : name(old.name) , funcCommand(old.funcCommand){ checkObj(); } void Command::checkObj(){ for(unsigned int i=0; i'a'&&name[i]<'z')||(name[i]>'A'&&name[i]<'Z'))){ throw CommandNameNotValidException(); } } } /*Command::Options *Command::getCmdOpts() const { return cmdOpts; } void Command::setCmdOpts(Command::Options *value) { cmdOpts = value; } Command::~Command(){ numCom--; } unsigned int Command::numCom = 0; const Command::Options *(Command::defaultOpts) = new Command::Options(); string Command::Options::Option::getOShort() const { return oShort; } void Command::Options::Option::setOShort(const string &value) { oShort = value; } string Command::Options::Option::getOLong() const { return oLong; } void Command::Options::Option::setOLong(const string &value) { oLong = value; } void Command::Options::addOption(Command::Options::Option *a){ for(size_t i=0; ihowMany(); i++){ if(a->getOShort()==(this->operator [](i)->getOShort())||a->getOLong()==(this->operator [](i)->getOLong())){ throw OptionNameConflictException(); } } opts.push_back(a); }*/ const string Command::LongData::EMPTY(""); }