Computer Scientist

Tuesday, 7 December 2010

Parsing Long Options

Find this topic in DOCUMENT of GNU C library: libc


Here I conclude some useful tips:


== return values of getopt():
  • successful
    • a character (the option name without argument)
    • a character (the option name), a pointer to char (char *optarg: argument)
  • failed
    • '?' (not included in options OR missing argument) (int optopt keeps the character)
  • -1 complete

== return values of getopt_long ():

  • successful
    • short_options
      • (same with getopt())
    • long_options
      • content of val (flag = NULL) (Tips, put corresponding short option char in val)
      • 0 (flag != NULL, put content of val into *flag)
      • (same with above two) (with argument are stored in optarg)
  • failed
    • (same with getopt())
  • -1 complete
    PS: indexptr record the index of the options in array of struct option.

No comments:

Post a Comment