I want to build an easy to use Command line framework that user can add his own functions

  Kiến thức lập trình

I want to write a Command Line Framework for fun! i have this idea for a long time now and i’m trying to impliment it but i can’t do it.

this is what i’ve thought :

What i want to do is to have an abstract class named CommandLine in an header file and the user inherits from my class and adds and stores his own functions using a pre-made function like this one:

//holds func pointers
std::vector<bool (* func)(PARAMETERS)> vFunc;
//holds command names
std::vector<std::string> Commands;

bool PushBackFunc(bool(* Func)(PARAMETERS), const std::string& CommandName)
{
    //Function pointer so i can later call the function
    vFunc.push_back(Func);

    //string with the name of the command so i can later compare it with input
    Commands.push_back(CommandName); 
}

(PushBackFunc() is called in a virtual function OnCreate() that user overrides and calls to intialize his functions)

//exaple:
class CommandLine
{
public:
     virtual bool OnCreate() = 0;
     //other stuff
};

class MyCommandLine : public CommandLine
{
public:
     bool OnCreate()
     {
          PushBackFunc(&ExapleFunc, "Exaple");
     }
     //other stuff
};

and after if the user types the CommandName and the PARAMETERS in run time we call the function, like this for exaple:

vFunc[0](PARAMETERS);

Now the problem here is that the “PARAMETERS” could be all different… thus i can not have a vector of function pointers with deferent parameters.

I thought about a struct that holds all the input but that can get very messy if you have lots of functions. The next “best” thing i thought is to make every parameter global so i dont have to deal with function inputs but that’s just bad…

i cant find a way that templates can help here for the same reason just function pointers didnt help. (PLEASE CORRECT ME IF I’M WRONG)

I also tried inheritance:

class In
{
public:
    virtual int Get() = 0;
};

class Input1 : public In
{
    int param = 1;
public:
    int Get()
    {
        return param;
    }
};

class Input2 : public In
{
   int param = 0;
public:
   int Get()
   {
        return param;
   }
};

In* PARAM = new Input;
In* PARAM2 = new Input2;

std::vector<bool (* func)(In* i)> vFunc;
std::vector<std::string> Commands;

bool PushBackFunc(bool(* Func)(In* i), const std::string& CommandName)
{
    //Function pointer so i can later call the function
    vFunc.push_back(Func);

    //string with the name of the command so i can later compare it with input
    Commands.push_back(CommandName); 
}

as you can see, i can now create pointers of defferent In classes and pass them in the functions and get access in the child veriables using Get(). BUT i’m limmited on the return type… and C++ don’t let you template virtual functions

//this is forbiten:
class In
{
public:
     template <typedef T>
     virtual T Get() = 0;
};

last thing i thought was lamda functions. Well i dont know lots about them but i dont think they can help here.

thats the problem and thats what i tried! Nothing above must be done the way i said. I just want an easy way for the user to add his functions without messing with the base class and just use the command line. IF what i’m asking can be done!

New contributor

Παυλής Οχιαλλακακαονοματα is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT