Tuesday, January 8, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...