C++ argv[] switch

int main(int argc, char *argv[]) {

   char usage[] =
      "\n"
      "  %s  - (c) 2009-2010 Name Surname\n"
      "  http://your.homepage.com\n"
      "\n"
      "  usage: %s  [optional values]\n"
      "\n";

   if(argc <= 1) { // show usage if no parameter is specified
      printf(usage, "1.0.0", argv[0]);

      return 0;
   }

   for(int i=1; i<argc; i++) {
      if(argv[i][0] == '-') { // if value starts with "-" it should be the command
         switch(argv[i][1]) {
            case 'a':
               printf("you selected -a\n");
               printf("a was: %s", argv[i+1]);
               break;

            case 'b':
               printf("you selected -b");
               printf("b was: %s", argv[i+1]);
               break;

            default:
               printf(usage, "1.0.0", argv[0]);
         }
      }
   }

   return 0;
}

Protams visi ienākošie dati ir jāpārbauda vai viņi atbilst prasībām & stuff.

Advertisement


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.