1
# getopt --- do C library getopt(3) function in awk
3
# arnold@gnu.ai.mit.edu
6
# Initial version: March, 1991
10
# Optind -- index of ARGV for first non-option argument
11
# Optarg -- string value of argument to current option
12
# Opterr -- if non-zero, print our own diagnostic
13
# Optopt -- current option letter
16
# -1 at end of options
17
# ? for unrecognized option
18
# <c> a character representing the current option
21
# _opti index in multi-flag option, e.g., -abc
22
function getopt(argc, argv, options, optl, thisopt, i)
24
optl = length(options)
25
if (optl == 0) # no options given
28
if (argv[Optind] == "--") { # all done
32
} else if (argv[Optind] !~ /^-[^: \t\n\f\r\v\b]/) {
38
thisopt = substr(argv[Optind], _opti, 1)
40
i = index(options, thisopt)
43
printf("%c -- invalid option\n",
44
thisopt) > "/dev/stderr"
45
if (_opti >= length(argv[Optind])) {
52
if (substr(options, i + 1, 1) == ":") {
54
if (length(substr(argv[Optind], _opti + 1)) > 0)
55
Optarg = substr(argv[Optind], _opti + 1)
57
Optarg = argv[++Optind]
61
if (_opti == 0 || _opti >= length(argv[Optind])) {
69
Opterr = 1 # default is to diagnose
70
Optind = 1 # skip ARGV[0]
74
while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != -1)
75
printf("c = <%c>, optarg = <%s>\n",
77
printf("non-option arguments:\n")
78
for (; Optind < ARGC; Optind++)
79
printf("\tARGV[%d] = <%s>\n",