[C/C++] Using `getopt' in c/c++

Using `getopt' in c/c++

Could not get the real original version for the examples, list one of them first, usefully and well tested.

Examples

Example 1

// getopt_example.cpp

#include <unistd.h>

int main(int argc, char *argv[])
{
    int option = -1;
    char *addr, *port;

    while ((option = getopt (argc, argv, "i:p:")) != -1)
    {
         switch (option)
         {
         case 'i':
             addr = strdup(optarg);
             break;
         case 'p':
             port = strdup(optarg);
             break;
         default:
              /* unrecognised option ... add your error condition */
              break;
         }
    }

    /* rest of program */

    return 0;
}

 

See Also

  • Getopt - The GNU C Library - http://www.gnu.org/software/libc/manual/html_node/Getopt.html 
  • The GNU C Library - http://www.gnu.org/software/libc/manual/html_node/index.html#Top

 



 

你可能感兴趣的:([C/C++] Using `getopt' in c/c++)