0

I just started with pset6 server.c and already I am stuck at trying to figure out what the "h" in the getopt function is for.

// parse command-line arguments
int opt;
while ((opt = getopt(argc, argv, "hp:")) != -1)
{
    switch (opt)
    {
        // -h
        case 'h':
            printf("%s\n", usage);
            return 0;

        // -p port
        case 'p':
            port = atoi(optarg);
            break;

I think I get how the function works and what it does when the "-p" option is used to specify a certain port to listen to. However I have no idea why the "-h" option is included. The following switch just returns the usage if "-h" was used.

Any hints appreciated. Thanks!

1

if you read the switch statement at the beginning of the program, you'll know that the -h option lets you know how the program should be used. 'h' stands for "help". this is also typically the case with many other Linux console applications.

2
  • Thanks! -h for help makes sense. So it is good practice to always output the usage when the -h option is entered?
    – kafe
    Nov 22 '15 at 12:33
  • @kafe indeed...
    – kzidane
    Nov 23 '15 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .