parsing main

giving the function main

1
2
3
int main(int argc, char* argv[])
{
}


can someone show me how to parse commands passed through main. also how do i test it ? do run the command line in windows then the command along with the exe name?
If the name of your executable is "foo" and the command-line you typed was
 foo 1 2 3 Hello 4 3.14 World
then

argv[0] is "foo"
argv[1] is "1"
argv[2] is "2"
argv[3] is "3"
argv[4] is "Hello"
argv[5] is "3.14"
argv[6] is "World"

and

argc is 7 (number of elements in the above array).
wow thats easy enough. thanks :)
maybe not so easy. its saying there is commands even when there isnt any. is there a default command also sent to main. everything seems like its one off. its also not recognizing string literals.
Is the command that always seems to be there (I presume it's the string at argv[0]) the name of the executable?
ok its saying there is 1 command and then arg[0] is the string path up to main.
Last edited on
this doesnt work to well. at all. if i play with some options in the command prompt it does seem to pause to some commands but nothing happens. here is the code.

 
//removed long bad code 
Last edited on
ok its saying there is 1 command and then arg[0] is the string path up to main.


By convention, that is always at least one command; the command used to run that actual executable.
Last edited on
yes. still wasnt able to get any commands working. any at all. can anyone provide a working sample of a parsed main with options. just something with a cout << printout you choosed option x.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main(int argc, char** argv)
{
    if(argc < 2) {
        std::cerr << "Program requires one argument." << std::endl;
        return 1
    }
    else if(argc > 2) {
        std::cerr << "Too many arguments passed." << std::endl;
        return 2;
    }
    else {
        std::cout << argv[1] << std::endl;
    }
    return 0;
}

Try this.
Last edited on
thank flipe i see a few different things going on that was messing everything up. i thought you were suppose to pass the number of arguments along with the arguments themselves in the command line. this screws up what i wrote up above in a major way. thanks i can work on it from here.
your example worked with just echoing but when i try and actually parse it the command is there but the parsing isnt happening. like below.

1
2
3
4
5
6
7
if (argv[1] == "compress")
			std::cout << "compressing with default option";
		else
		{
			std::cout << argv[1]; // shows up as compress then why didnt the first if fire
			return 1;
		}


argv in the else is called and shows up as compress then why didnt the compress in the first half of the if statement fire.
Last edited on
Don't compare C-strings with ==, use strcmp().
argv[1] is a character pointer. It has a value which is a numerical address; thus, you are testing a numerical address (i.e. a number) for equality to a string literal ("compress"). They are not going to be equal.

I suggest you use a comparison function, like strcmp.
Last edited on
ok that was the problem thanks guys. i learned tons with this exercise as aggravating as it was.

in the command line
typing main = program must require at least one argument
typing main compress = compressing with default option
typing main compress 1 = compressing with option 1
typing main compress 2 = compressing with option 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

int main(int argc, char** argv)
{
    if(argc < 2) {
        std::cerr << "Program requires one argument." << std::endl;
        return 1;
    }
    else if(argc == 2)
	{
		if ((std::strcmp(argv[1],"compress") == 0 ))
			std::cout << "compressing with default option";
		else
		{
			std::cout <<"unknown command";
			return 1;
		}
	}
    else if (argc == 3)
	{
		if (((std::strcmp(argv[1],"compress") == 0) && (std::strcmp(argv[2], "1") ==0 )))
                            std::cout << "compressing with option 1";
		else if (((std::strcmp(argv[1],"compress") == 0) && (std::strcmp(argv[2], "2") ==0 )))
                            std::cout << "compressing with option 2";
		else
		{
			std::cout << "unknown command sequence";
			return 1;
		}
    }

    return 0;
}



Topic archived. No new replies allowed.