vendredi 29 janvier 2016

Parsing command line arguments and use them to initialize application that has to be implemented using abstract factory pattern

I have written an application that parses a command line and initializes the application, but is in C language. But the required application is to be written in C++. The following code snippet extracts/parses the arguments and calls respective functions.

void (* const OptionFuncPtr[])(char *pstr) = {
                                (SetA),(GetB), (SetC), (SetD), (Erase), (FOptions), (GOperations),
                                (ShowHelp), (SetInput), (undefined), (undefined), (SetLBit), (undefined), (undefined),
                                (OutputData), (ProgramOptions), (ReadAll), (ReadData), (SOptions), (TestMode),
                                (undefined), (VerifyData), (WOptions), (FillUnspecific), (ReadLBits), (NoProgessIndicator)
                                };

int main(int argc, char *argv[])
{

...// some code
for( int i = 1; i < argc; i++ )
    {
        param = argv[i];

        /* Allow parameters to start with '-' */
        if( param[0] != '-' )
            throw new ErrorMsg( "All parameters must start with '-'!" );

        if( strlen( param ) <= 1 )
            throw new ErrorMsg( "Parameters cannot be just the minus without any characters!" );

        value = param[1] - 'a';
        if(value > 26)
            throw new ErrorMsg("You have not entered a valid command line parameter");

        (*OptionFuncPtr[value])(&param[2]);
        }

//some more code
return 0;
}

As it can be seen, the code is in C. and I need to write it in C++. I am a noob to C++ and working on a deadline. I have read a lot of articles, but have not been to able to concrete any thing yet. Dont know how to implement this using C++. The methods that i am calling may be from different class. I think this is achievable using functors. But not sure. It would be helpful if any one can just give a small example, from where i will carry things forward. Need to use abstract factory pattern also.

Ps: This may seem to be a very basic question, but i am newbie to C++. :)

Aucun commentaire:

Enregistrer un commentaire