Skip to content

Commit

Permalink
Update getopts
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Aug 22, 2024
1 parent 275b1bc commit 1b40f41
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions src/exporter.awk
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
function getopts(optstring, args, opt, oarg, i, j) {
i = int(OPTIND)
function getopts(optstring, args, i, j) {
i = OPTIND ? int(OPTIND) : (OPTIND = 1)
j = int(substr(OPTIND, length(i) + 2))
if (! (i in args)) return ""
if (args[i] == "--") { OPTIND = i + 1; return "" }
if (args[i] !~ /^-./) return ""
opt = substr(args[i], j + 2, 1)
oarg = substr(args[i], j + 3)
if (index(optstring, opt ":")) {
if (oarg == "") {
if ( (i + 1) in args ) {
OPTARG = args[i + 1]
OPTIND = i + 2
} else {
OPTARG = "option requires an argument -- " opt
opt = "?"
OPTERR = OPTARG = ""
if (!(i in args)) return
if (args[i] == "--") { OPTIND = i + 1; return }
if (args[i] !~ /^-./) return
OPT = substr(args[i], j + 2, 1)
OPTARG = substr(args[i], j + 3)
if (index(optstring, OPT ":")) {
if (OPTARG == "") {
if (!(++i in args)) {
OPTERR = "option requires an argument -- " OPT
OPTARG = ":"
return
}
} else {
OPTARG = oarg
OPTIND = i + 1
OPTARG = args[i]
}
} else if (index(optstring, opt)) {
OPTARG = ""
OPTIND = (oarg == "") ? (i + 1) : (i "," (j + 1))
OPTIND = i + 1
} else if (OPT != ":" && index(optstring, OPT)) {
OPTIND = (OPTARG == "") ? (i + 1) : (i "," (j + 1))
} else {
OPTARG = "illegal option -- " opt
opt = "?"
OPTERR = "illegal option -- " OPT
OPTARG = "?"
return
}
return opt
return 1
}

function prinit(str) {
Expand Down Expand Up @@ -69,16 +67,16 @@ BEGIN {
}

OPTIND = i
while(OPT = getopts("pnvs0", ARGV)) {
while(getopts("pnvs0", ARGV)) {
if (OPT == "p") prefix = "export "
if (OPT == "n") mode = "name"
if (OPT == "v") mode = "value"
if (OPT == "0") newline = NUL
if (OPT == "s") silent = 1
if (OPT == "?") abort("export: " OPTARG)
}
i = OPTIND
if (OPTERR) abort("exprot: " OPTERR)

i = OPTIND
if (i < ARGC) {
envkeys_length = 0
for (; i < ARGC; i++) {
Expand Down

0 comments on commit 1b40f41

Please sign in to comment.