parsing argc and argv in c++
Asked Answered
C

2

5

I want to learn more C++... Usually I make a for loop to parse argv, and I wind up with a bunch a C-style strings. I want to do something similar in C++, but preferably without reading from /proc/whatever. At first, I tried to convert the C-style string to a C++ style string without results... The frustrating bit is that everyone on SO seems to want to know how to go the other way, which is what c_str() is for. What's a good C++ way to do this (ie parse argv)?

Also, one note, I'm looking for a unix style answer, all the techniques for conversion I've seen have to do with Windows, which I'm completely uniterested in.

Caresse answered 29/11, 2009 at 5:39 Comment(2)
Just curious... did you get the idea that you need to read /proc from this article? blog.linuxgamepublishing.com/2009/10/12/…Shephard
no, I did not... I hate the 15 char rule hereCaresse
B
25

I'm not sure I fully understand the question.

The cleanest method I know to get all the arguments in an easy to use array is:

std::vector<std::string> v(argv, argv + argc);

But if you're looking for a way to really parse the data, check out Boost.ProgramOptions.

Blinni answered 29/11, 2009 at 6:7 Comment(1)
+1 Boost program options is awesome. Makes it so much easier to make a command line application.Tame
W
2

Are you having trouble converting argv's to strings? You can just do:

string s(argv[i]);

...where i is a valid index into argv.

Whichever answered 29/11, 2009 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.