how to parse several arguments using command line arguments
i am writing a command line utility in which i want to parse several arguments: right now i am not reading data from address(so please don't get confuse by -addr); my primary objective is to design the framework so that i can parse several arguments as follows.
filename read -addr value -addr2 value2 -addrN valueN -length value -range value -length2 value2 -lengthN valueN -width value -width2 value2 -widthN valueN
the N can have a maximum value of 64 and minimum value of 1.
Please give some valuable suggestions so that i can d开发者_JAVA百科o it. Thanks
consider using the Getopt library or some of its modifications, it can make yout life easier.
http://www.boost.org/doc/libs/1_41_0/doc/html/program_options.html
The typical main()
prototype goes like this:
int main(int argc, char** argv) {
// stuff
}
When your program is executed from a command-line, argc
will be the total count of your arguments, plus one for the name of the program itself; and you can think of argv as an array of strings containing the arguments.
Knowing argc
, parsing the argument list should be easy :)
Edit: a short example, just in case.
int main(int argc, char** argv) {
printf("%d\n", argc);
}
Then, on your cl:
./program asd asd asd
4
argv[0] is "program", argv[1] is "asd", etc.
精彩评论