Passing command line argument with spaces

2019-09-07 20:40发布

问题:

I have an application which reads specific commands from the command line and processes the command. For Example:

I have a command called "setparamval" which is used to set the parameter value. The syntax of this command while passing from the command line would look something like this:

setparamval <variable_name>,<value>

Supposing if I have a variable called "DESC" to which I would want to set the value as "ab cd ef gh" I am passing the command line argument as:

setparamval DESC,"ab cd ef gh" 

(i am passing the value inside "", otherwise the command line treats this as seperate arguments..)

Till here everything is fine...

Next, in my code, I am trying to extract the parameter name and the value from the command passed from command line using:

char inputCmd[TAGNAME_LEN];
strcpy(inputCmd,argv[1]);

When i see the value in "inputCmd", i am getting: "DESC,"ab cd ef gh". Now i want to seperate the parameter from the parameter value (where the delimiter is ",") using:

char *savePtr = NULL;
char *src = inputCmd;  //argv[1];
ch = strtok_r(src, ",", &savePtr);

When i see the value inside savePtr, i get: ""ab cd ef gh". There is an extra " getting added to the start of the string. I am not sure why is this getting added.

Could you please help me in figuring out this problem? Thanks for your patience and help.