Site Tools


h-getopt

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
h-getopt [August 24, 2026 at 23:55] – Ivan Janevskih-getopt [August 26, 2026 at 16:02] (current) – external edit 127.0.0.1
Line 46: Line 46:
  
 **Short option parsing**: **Short option parsing**:
-- `getopt(argc, argv, optstring)` — parse options (GNU extension, POSIX)+- `getopt(argc, argv, optstring)`: parse options (GNU extension, POSIX)
   - `optstring` format: `"ab:c::"` means `-a` (no arg), `-b` (required arg), `-c` (optional arg)   - `optstring` format: `"ab:c::"` means `-a` (no arg), `-b` (required arg), `-c` (optional arg)
   - Returns option character, `?` on error, `:` on missing arg, or -1 when done   - Returns option character, `?` on error, `:` on missing arg, or -1 when done
Line 52: Line 52:
  
 **Long option parsing**: **Long option parsing**:
-- `getopt_long(argc, argv, optstring, longopts, &longindex)` — parse long options+- `getopt_long(argc, argv, optstring, longopts, &longindex)`: parse long options
 - `struct option { const char *name; int has_arg; int *flag; int val; };` - `struct option { const char *name; int has_arg; int *flag; int val; };`
   - Terminate array with `{0, 0, 0, 0}`   - Terminate array with `{0, 0, 0, 0}`
Line 58: Line 58:
  
 **Argument flags** (for struct option.has_arg): **Argument flags** (for struct option.has_arg):
-- `no_argument` — option takes no argument +- `no_argument`: option takes no argument 
-- `required_argument` — option requires argument +- `required_argument`: option requires argument 
-- `optional_argument` — argument is optional+- `optional_argument`: argument is optional
  
 **Global variables** (set by getopt): **Global variables** (set by getopt):
-- `optarg` — pointer to argument for current option (NULL if none) +- `optarg`: pointer to argument for current option (NULL if none) 
-- `optind` — index in argv of next element to process (initialize to 1) +- `optind`: index in argv of next element to process (initialize to 1) 
-- `opterr` — if 1 (default), print error messages to stderr on unknown options +- `opterr`: if 1 (default), print error messages to stderr on unknown options 
-- `optopt` — option character that caused error (unknown option or missing arg)+- `optopt`: option character that caused error (unknown option or missing arg)
  
 **Usage pattern**: **Usage pattern**:
h-getopt.1787615735.md.gz · Last modified: by Ivan Janevski