Search notes:

libc: getopt

//
//  Some calls:
//    ./getopt
//    ./getopt -h
//    ./getopt -rhello foo
//    ./getopt -rhello -o
//    ./getopt -rhello -oworld
//    ./getopt -rhello -o foo bar baz
//
//
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // Needed for getopt

void usage() {

  printf("\n\n getopt [-h] [-r R] [-o [O] ]\n\n");
  exit (0);

}

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

  char *R = NULL;
  int   o = 0;
  char *O = "default O value";
  int index;
  int opt;

  opterr = 0;

  while ((opt = getopt (argc, argv, 
            "h"       // no colon:   just a flag
            "r:"      // one colon:  required value
            "o::"     // two colons: optional value
          )) != -1) { // -1: No more options present, terminate loop

    switch (opt) {

      case 'h':
        usage();

      case 'r':
        R = optarg;
        break;

      case 'o':
        O = optarg;
        o = 1;
        break;

      case '?': // ? indicates an error
                // optopt now contains the option that caused
                // the error

        if (optopt == 'r') {
          fprintf (stderr, "-r without value.\n");
        }
        else if (isprint (optopt)) {
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        }
        else {
          fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
        }

        return 1;
        
      default:
        abort ();
      }
  }

  printf ("R = %s, O = %s, o = %d\n", R, O, o);

  for (index = optind; index < argc; index++) {
    printf ("Non-option argument %s\n", argv[index]);
  }

  return 0;
}

Github repository about-libc, path: /getopt.c

See also

The Standard C Library

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758201008, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/C-C-plus-plus/C/libc/getopt(119): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78