Tuesday 8 January 2019

What's an effective way to parse command line parameters in C++?

Is there a really effective way of dealing with command line parameters in C++?



What I'm doing below feels completely amateurish, and I can't imagine this is how command line parameters are really handled (atoi, hard-coded argc checks) in professional software.




// Command line usage: sum num1 num2

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

{
   if (argc < 3)
   {
      cout << "Usage: " << argv[0] << " num1 num2\n";
      exit(1);
   }
   int a = atoi(argv[1]);
   int b = atoi(argv[2]);
   int sum = a + b;
   cout << "Sum: " << sum << "\n";
   return 0;
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...