howto: test.exe -command

Hello! Sorry if the title dosen't explain what i mean. But how do I make a program that i can run like:
test -command1
test -command2

things like that. And i don't know the name of the function so i can't google it =/.

Thanks for any help!
use these arguments for main int main (int argc, char** argv) argc holds the number of arguments passed to the program
argv holds an array containing the arguments passed to your program as character sequences
argv[0] contains the executable path and name so all the commands start from argv[1]

eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main (int argc, char** argv)
{
   if (argc == 2)//You have a command
   {
       if ( !strcmp(argv[1],"command1") ) //the argument is "command1"
          //do something
      else if ( !strcmp(argv[1],"command2") )
         //do something else
      else
      {
           cout << "Invalid command";
           return 2; //Not OK
      }
   }
   else
   {
      cout << "You should give a command";
      return 1;//Not OK
   }
   return 0;//OK
}
Last edited on
Topic archived. No new replies allowed.