⚠️ This project is no longer maintained.
Tool to parse command-line options.
GNU Awk 5.0.1
No options are supported now.
Option specification is command-line argument descriptions bundled together in some code place. It has the following syntax:
option { [-t|--type=integer|float|bool|string] [-a|--alias=<comma-separated-list>] [-ia|--is-assignable=true|false] [-ab|--allow-bundle=true|false] [-ac|--assignment-char=<char>] }
where:
option
- main command-line option name to be described within curly braces-t
|--type
-option
value type one ofinteger
|float
|bool
|string
(required when-ia
|--is-assignable
equals totrue
)-a
|--alias
-option
aliases-ia
|--is-assignable
- specifies whetheroption
accepts value-ab
|--allow-bundle
- specifies whetheroption
acceptable value can be assigned via delimitingoption
and it via-ac
|--assignment-char
(required when-ia
|--is-assignable
equals totrue
)-ac
|--assignment-char
- assignment char used to specifyoption
value (required when-ia
|--is-assignable
and-ab
|--allow-bundle
equal totrue
)
Example:
-h { --alias=--help --is-assignable=false } -v { --alias=--version --is-assignable=false }
This option specification means that -h
|--help
and -v
|--version
options doesn't accept any values.
You can create option specifications via arrays:
options[0] = "-h"
options[1] = "{"
options[2] = "--alias=--help"
options[3] = "--is-assignable=false"
options[4] = "}"
options[5] = "-v"
options[6] = "{"
options[7] = "--alias=--version"
options[8] = "--is-assignable=false"
options[9] = "}"
Or you can use parseoptsrunner.awk to pass arguments and their specifications simplier:
awk -f parseoptsrunner.awk -- -h :: -h '{' --alias=--help --is-assignable=false '}' -v '{' --alias=--version --is-assignable=false '}'
Everything before double colon is argument and everything after is specification.
Checking whether user-written arguments (arguments
array) conforms described option specification (options
array):
@include "parseopts.awk"
@include "utils.awk"
@include "colors.awk"
function shiftItems(from, to, source, target) {
i = from
j = to
while (i <= from + length(source) - 1) {
target[j] = source[i]
i++
j++
}
}
BEGIN {
split("-f { -a=--first -ia=true -t=bool -ab=true -ac=: } -s { -a=--second -ia=true -t=bool -ab=true -ac=: }", __options, " ")
shiftItems(1, 0, __options, options)
utils::clearArray(__options)
printf "options = "
utils::printlnArray(options)
split("--first false -s true", __arguments, " ")
shiftItems(1, 0, __arguments, arguments)
utils::clearArray(__arguments)
printf "arguments = "
utils::printlnArray(arguments, ", ")
print parseopts::checkArguments(arguments, options)
}