1

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 do it. Thanks

2
  • 1
    Show us your code so far, and explain your difficulties. Otherwise, this is "send teh codez plz". Commented Apr 29, 2011 at 6:21
  • I think we can provide help without doing the actual parsing :) Commented Apr 29, 2011 at 6:23

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

can you please tell me what is the equivalent of getopt() under windows.Actually i am using VC++.
1

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.

2 Comments

@ Santiago Lezica hey hi i know about command line arguments but i am dealing with variable number of arguments, like i can provide any number of arguments at command line;this is the thing which is causing me the problem. Can any one of you provide help on variable number of arguments i know may be "getopt" can solve my problem but i dont know how to use it.
As long as you know the total number of arguments, you have no problem iterating through them and deciding which settings they affect. Just loop and analyze.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.