#include "shell.h" namespace mshconsole { Command::Command(const string& n, int (*funcptr)(CommandExecutor* whoExecuted,const Datas& data, const vector argv)) : name(n), funcCommand(funcptr){ checkObj(); } void Command::addOption(Option *a){ if(a->getOptionName().length()==1){ shortOpts.push_back(a); } else longOpts.push_back(a); } int Command::execute(const vector* args, CommandExecutor* c){ if(args==nullptr) return -1; //filling longData Datas longData; for(size_t i=0; igetOptionName()[0]); } getoptarg+=" -:"; //creating argv as char** for getopt compatibility vector 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; igetOptionName()[0]){ shortData[i]->set(true); break; } } } else if(c=='-'){ string l(optarg); bool getout = false; try{ for(size_t i=0; igetOptionName()+"=")==0){ if(!(longOpts[i]->hasFlag(Option::STRING))){ throw NotRightArgException(); } longData[i]->set(l.substr(longOpts[i]->getOptionName().size()+1,l.size())); getout = true; break; } if(l==longOpts[i]->getOptionName()){ longData[i]->set(true); getout=true; break; } } } catch(Data::MultipleDefinitionException){ getout=false; } if(!getout) throw OptionNotParsedCorrectlyException(static_cast("--") + optarg); } else if(c=='?'){ 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 of all options for debug (code to check) /* for(size_t i=0; igetOptionName() << ": "; LongData* now = data[i]; if(now->getState()==LongData::States::STRING) cout << now->getLongData(); else cout << now->getBoolState(); cout << "\n"; } for(size_t i=0; iname; } 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(); } } } const string Command::Data::EMPTY(""); }