3

I want to parse the command line options having specific format for getting the 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*)"";
}

The arguments passed are getCmdOption(argv, argv+argc, "--optionName") The code works fine for all the options and gives proper output but if I want to give values such as Application.exe --optionName surname="O Kief" the code should return me surname="O Kief" but instead it returns "surname=O Keif"

The format for input is Application.exe --optionName optionValue and expected output is "optionValue"

What is possibly wrong with the logic ? And how can i handle the case I have mentioned ?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
CMouse
  • 130
  • 3
  • 19
  • return (char*)" " Should not be returned, NULL is the correct return field but " " is used as per the requirement. – CMouse Mar 30 '17 at 06:20
  • A bit offtopic, but perhaps using something ready could be an option for you - `man getopt`. – dmi Mar 30 '17 at 06:22
  • 1
    That happens because the `""` at the command line are handled by the shell, not your program. – StoryTeller - Unslander Monica Mar 30 '17 at 06:22
  • On Unix shells at least, if you pass `foo"bar"` on the command line, this is concatenated into a single string containing `foobar` before it is passed to your program. Dunno about Windows. (I assume you are using Windows since your program has an `.exe` suffix). – BoBTFish Mar 30 '17 at 06:22
  • I am targeting both the platform so a platform independent solution is needed, but developing on windows thus mentioned exe here, the other part of code is portable on linux. – CMouse Mar 30 '17 at 06:23
  • So you mean to say the logic is correct but the shell handles the situation bu concating the string before it is passed to the program? So what could be a possible workaround for this ? – CMouse Mar 30 '17 at 06:27
  • 3
    Nothing you can do in your program. This is a question about how to use the shell. If you want to pass an actual `"` character, you need to *escape* it, by passing `\"` (or, wrap the whole thing in single quotes: `'arg"with"quotes'` - again, don't know if it works on Windows). – BoBTFish Mar 30 '17 at 06:29
  • Yeah wrapping in single quote works but that reduces the user friendliness of the command – CMouse Mar 30 '17 at 06:32
  • Nothing you can do about that. This is all happening entirely outside of your program. Or you rethink the interface to your program entirely, so as not to use shell input. Maybe read input from a file, or using `std::cin` (preferably with [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline))? – BoBTFish Mar 30 '17 at 06:36

1 Answers1

2

""is the only way to get the command interpreter (cmd.exe) to treat the whole double-quoted string as a single argument. Sadly, however, not only are the enclosing double-quotes retained (as usual) but so are the doubled escaped ones, so obtaining the intended string is a two-step process; e.g., assuming that the double-quoted string is passed as the 1st argument.

Application.exe --optionName option="name" removes the enclosing double-quotes then converts the doubled double-quotes to single ones. Be sure to use the enclosing double-quotes around the assignment parts to prevent unwanted interpretation of the values.

Although a precise solution is given below but you can visit this for in-depth solution of the problem (Not exactly a problem)

If the optionValue does not contain white spaces keep the format as

Application.exe --optionName "optionValue"

If the option Value contains white space wrap the thing in quote as

 Application.exe --optionName '"option Value"'
Community
  • 1
  • 1
CocoCrisp
  • 807
  • 1
  • 9
  • 26