h-getopt
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| h-getopt [August 24, 2026 at 23:55] – Ivan Janevski | h-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, | + | - `getopt(argc, |
| - `optstring` format: `" | - `optstring` format: `" | ||
| - 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, | + | - `getopt_long(argc, |
| - `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` | + | - `no_argument`: option takes no argument |
| - | - `required_argument` | + | - `required_argument`: option requires argument |
| - | - `optional_argument` | + | - `optional_argument`: argument is optional |
| **Global variables** (set by getopt): | **Global variables** (set by getopt): | ||
| - | - `optarg` | + | - `optarg`: pointer to argument for current option (NULL if none) |
| - | - `optind` | + | - `optind`: index in argv of next element to process (initialize to 1) |
| - | - `opterr` | + | - `opterr`: if 1 (default), print error messages to stderr on unknown options |
| - | - `optopt` | + | - `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
