Newsgroup: it.comp.programmare
Titolo: getopt() questo sconusciuto

Sto preparando l'esame di Sistemi Operativi e per il laboratorio mi sono
trovato nella necessità di utilizzare la funzione getopt() per eseguire il
parsing dei paramentri di un programma.
Non mi è molto chiaro come utilizzarla.
Qualcuno mi puo' spiegare per bene cosa fa, come viene usata e fare vedere
qualche esempio?
Sul Kernigam - Richie non c'e'!!!
Grazie mille


Ecco la definizione che ho trovato usando: man getopt
Spero ti sia di aiuto
NAME
     getopt - get option letter from argument vector
SYNOPSIS
     #include <stdlib.h>
     int getopt(int argc, char * const *argv,  const  char  *opt-
     string);
     extern char *optarg;
     extern int optind, opterr, optopt;
DESCRIPTION
     The getopt() function returns the next option letter in argv
     that  matches  a  letter  in  optstring. It supports all the
     rules of the command syntax standard (see  intro(1)).  Since
     all  new commands are intended to adhere to the command syn-
     tax standard, they  should  use  getopts(1),  getopt(3C)  or
     getsubopt(3C)  to  parse positional parameters and check for
     options that are legal for that command.
     The optstring argument must contain the option  letters  the
     command  using  getopt() will recognize; if a letter is fol-
     lowed by a colon, the option is expected to  have  an  argu-
     ment,  or group of arguments, which may be separated from it
     by white space.  The optarg argument is set to point to  the
     start of the option argument on return from getopt().
     The getopt() function places in optind the argv index of the
     next  argument  to  be  processed. optind is external and is
     initialized to 1 before the first call to getopt(). When all
     options  have  been processed (that is, up to the first non-
     option argument), getopt() returns EOF. The  special  option
     "--"  (two  hyphens)  may  be used to delimit the end of the
     options; when it is encountered, EOF is returned  and  "--"'
     is  skipped.   This is useful in delimiting non-option argu-
     ments that begin with "-" (hyphen).
RETURN VALUES
     The getopt() function prints an error message on  the  stan-
     dard  error  and  returns  a  "?"  (question  mark)  when it
     encounters an option letter not included in optstring or  no
     argument  after  an option that expects one. This error mes-
     sage may be disabled by setting  opterr to 0. The  value  of
     the character that caused the error is in optopt.
USAGE
     If the application is  linked  with  -lintl,  then  messages
     printed from this function are in the native language speci-
     fied by the LC_MESSAGES locale category; see setlocale(3C).
     The getopt() function does not  fully  check  for  mandatory
     arguments; that is, given an option string a:b and the input
SunOS 5.8           Last change: 29 Dec 1996                    1
Standard C Library Functions                           getopt(3C)
     -a -b, getopt() assumes that -b is the mandatory argument to
     the  -a  option and not that -a is missing a mandatory argu-
     ment.
     It is a  violation  of  the  command  syntax  standard  (see
     intro(1))  for  options  with  arguments  to be grouped with
     other options, as in cmd -abo filename , where a and  b  are
     options,
      o is an option that requires an argument, and  filename  is
     the  argument to o. Although this syntax is permitted in the
     current implementation, it should not be used because it may
     not  be supported in future releases.  The correct syntax to
     use is:
          cmd -ab -o filename.
EXAMPLES
     Example 1: Example on how one might  process  the  arguments
     for a command.
     The following code fragment shows how one might process  the
     arguments for a command that can take the mutually exclusive
     options a and b, and the option o, which requires  an  argu-
     ment:
     #include <stdlib.h>
     #include <stdio.h>
     main (int argc, char **argv)
     {
        int c;
        extern char *optarg;
        extern int optind;
        int aflg = 0;
        int bflg = 0;
        int errflg = 0;
        char *ofile = NULL;
        while ((c = getopt(argc, argv, "abo:")) != EOF)
           switch (c) {
           case 'a':
              if (bflg)
                 errflg++;
              else
                 aflg++;
              break;
           case 'b':
              if (aflg)
                 errflg++;
              else
                 bflg++;
SunOS 5.8           Last change: 29 Dec 1996                    2
Standard C Library Functions                           getopt(3C)
              break;
           case 'o':
              ofile = optarg;
              (void)printf("ofile = %s\n", ofile);
              break;
           case '?':
              errflg++;
           }
        if (errflg) {
           (void)fprintf(stderr,
              "usage: cmd [-a|-b] [-o <filename>] files...\n");
           exit (2);
            }
            for ( ; optind < argc; optind++)
          (void)printf("%s\n", argv[optind]);
        return 0;
     }
--------------------
Fabio

 Sul Kernigam - Richie non c'e'!!!
Non c'e' in quanto non e' C standard.
-- 
Andrea Bergia - studente              Linux Registered User #281550

 sbaglio).
I 3.8M del pdf sono liberamente scaricabili da
http://www.advancedlinuxprogramming.com/downloads.html
C:\>iao Angelo
-- 
Posted via Mailgate.ORG Server - 
http://www.Mailgate.ORG

Il giorno Wed, 18 Jun 2003 15:42:52 +0000 lo sventurato Ciobin ebbe
l'ardire di pronunciare tali nefaste parole:

Se non sbaglio tempo fa uscì un articolo su DEV dove era spiegato l'uso di
getopt(), solo che purtroppo ora non ricordo il numero :((.
-- 
Slaine Mc Roth
Linux User# 312607
Linux Machine# 198324