Parsing command line arguments after execution

Hello,

How would I parse command line arguments inside of a program.

So the normal way to do it is with argc and argv which then requires you to type something like prog.exe arg1 arg2.

How would I go about making it run with only the program prog.exe
Then inside of prog.exe it waits for input in the form "insert x" where insert is the function and x is the value to insert. Also insert isnt the name of the function so I would need to be able to have insert call an insert function not called insert.

Is it possible to parse arguments int he same form as argc and argv inside of the program instead of when main is called?
As soon as main has started, any further input you get from the user can be evaluated in your program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char *argv[])
{
    std::istringstream iss;
    std::string method;
    int param;
    while(getline(cin, args))
    {
        iss.str(args);
        iss >> method >> param;

        if (method == "insert")
        { /*Do something*/ }
    }
}
<shellapi.h> defines the function WCHAR * CALLBACK CommandLineToArgvW(WCHAR * szCmdLine, int * pcArgs) which you can use to turn a null terminated wide character string into argc and argv like outputs. For example:
1
2
3
4
5
WCHAR * szCmdLine = L"insert x";
WCHAR ** argv;
int argc;

argv = CommandLineToArgvW(szCmdLine, &argc);


will set argc = 2 and argv[0] = "insert" argv[1] = "x"
Topic archived. No new replies allowed.