Getting command line parameters

I want to parse command line parameters that have a specific format to get values. The function is as follows

char* getCmdOption(char** begin,
                    char** end,
                    const std::string& option)
{
   char** itr = std::find(begin, end, option);

  if (itr != end && ++itr != end)
  {
        return *itr;
  }
  return (char*)"";
}

      

Past arguments: getCmdOption(argv, argv+argc, "--optionName")

The code works fine for all parameters and gives correct output, but if I want to give values ​​like the Application.exe --optionName surname="O Kief"

code should return to me surname="O Kief"

, but instead it returns"surname=O Keif"

The input format is Application.exe --optionName optionValue

and the expected output is"optionValue"

What could be wrong with the logic? And how can I handle the case I have considered?

+3


source to share


1 answer


"" is the only way to get the command interpreter (cmd.exe) to process the entire double-quoted string as its only argument. Unfortunately, however, not only the closed double quotes are saved (as usual), but also the double escaped ones, so getting the intended string is a two-step process; for example, assuming a double-quoted string is passed as the first argument.

Application.exe --optionName option = "name" removes the enclosed double quotes, and then converts the double double quotes to single quotes. Be sure to use closed double quotes around destination parts to prevent unwanted interpretation of values.

Although the exact solution is given below, but you can visit this one for an in-depth solution to the problem (not really a problem)

If the Value option contains no spaces, then save the format as



Application.exe --optionName "optionValue"

      

If the Value parameter contains a space, wrap the thing in quotes like

 Application.exe --optionName '"option Value"'

      

+2


source







All Articles