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

82 lines
2 KiB
C
Raw Normal View History

#ifndef COMMAND_H
#define COMMAND_H
#pragma once
#include <string>
#include <vector>
#include <unistd.h>
extern "C"{
#include <popt.h>
}
#include "commandexecutor.h"
using std::string;
using std::vector;
namespace mshconsole{
class Command
{
const string name;
void checkObj();
vector<poptOption> opts;
static const struct poptOption POPT_TERMINATOR;
size_t optionNum;
public:
class Data{
public:
union Arg{
int i;
char* s;
long l;
float f;
double d;
};
enum Types{
STRING,
INT,
LONG,
FLOAT,
DOUBLE
};
private:
union Arg d;
poptOption* of;
Data(poptOption* o) : of(o), d() {}
friend class mshconsole::Command;
public:
const union Arg& getArg() const;
enum Types getType() const;
int getInt() const;
char* getString() const;
long getLong() const;
float getFloat() const;
double getDouble() const;
};
class Datas : public vector<Data*>{
public:
Data* getOptData(const string& optionName) const;
Data* getOptData(char opt) const;
Data* operator[](const string& opt) const;
};
class DuplicatedOptionException{};
class CommandNameNotValidException{};
class InvalidOptionException{};
Command(const string& n, int (*funcptr)(CommandExecutor* whoExecuted,const Datas& data, const vector<const char*> argv));
Command(const Command&);
~Command();
const string& getName();
int execute(const struct Params&, CommandExecutor*);
void addOption(const poptOption& option);
private:
int (*funcCommand)(CommandExecutor* whoExecuted,const Datas& data, const vector<const char*> argv);
};
}
#endif // COMMAND_H