| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| function getopt(argc, argv, options, longopts, thisopt, i, j) |
| { |
| if (length(options) == 0 && length(longopts) == 0) |
| return -1 |
|
|
| if (argv[Optind] == "--") { |
| Optind++ |
| _opti = 0 |
| return -1 |
| } else if (argv[Optind] !~ /^-[^:[:space:]]/) { |
| _opti = 0 |
| return -1 |
| } |
| if (argv[Optind] !~ /^--/) { |
| if (_opti == 0) |
| _opti = 2 |
| thisopt = substr(argv[Optind], _opti, 1) |
| Optopt = thisopt |
| i = index(options, thisopt) |
| if (i == 0) { |
| if (Opterr) |
| printf("%c -- invalid option\n", thisopt) > "/dev/stderr" |
| if (_opti >= length(argv[Optind])) { |
| Optind++ |
| _opti = 0 |
| } else |
| _opti++ |
| return "?" |
| } |
| if (substr(options, i + 1, 1) == ":") { |
| |
| if (length(substr(argv[Optind], _opti + 1)) > 0) |
| Optarg = substr(argv[Optind], _opti + 1) |
| else |
| Optarg = argv[++Optind] |
| _opti = 0 |
| } else |
| Optarg = "" |
| if (_opti == 0 || _opti >= length(argv[Optind])) { |
| Optind++ |
| _opti = 0 |
| } else |
| _opti++ |
| return thisopt |
| } else { |
| j = index(argv[Optind], "=") |
| if (j > 0) |
| thisopt = substr(argv[Optind], 3, j - 3) |
| else |
| thisopt = substr(argv[Optind], 3) |
| Optopt = thisopt |
| i = match(longopts, "(^|,)" thisopt "($|[,:])") |
| if (i == 0) { |
| if (Opterr) |
| printf("%s -- invalid option\n", thisopt) > "/dev/stderr" |
| Optind++ |
| return "?" |
| } |
| if (substr(longopts, i-1+RLENGTH, 1) == ":") { |
| if (j > 0) |
| Optarg = substr(argv[Optind], j + 1) |
| else |
| Optarg = argv[++Optind] |
| } else |
| Optarg = "" |
| Optind++ |
| return thisopt |
| } |
| } |
| BEGIN { |
| Opterr = 1 |
| Optind = 1 |
|
|
| |
| if (_getopt_test) { |
| _myshortopts = "ab:cd" |
| _mylongopts = "longa,longb:,otherc,otherd" |
|
|
| while ((_go_c = getopt(ARGC, ARGV, _myshortopts, _mylongopts)) != -1) |
| printf("c = <%s>, Optarg = <%s>\n", _go_c, Optarg) |
| printf("non-option arguments:\n") |
| for (; Optind < ARGC; Optind++) |
| printf("\tARGV[%d] = <%s>\n", Optind, ARGV[Optind]) |
| } |
| } |
|
|