journal/tool/lib/argsLib.jq
2024-11-10 08:24:51 +00:00

45 lines
1 KiB
Plaintext

def parseArgBool:
. as $value |
if ($value | type) == "boolean" then
$value
elif
$value == "true" or $value == "on" or $value == "yes"
then
true
elif
$value == "false" or $value == "off" or $value == "no"
then
false
else
error("invalid bool value")
end;
def parseArgs:
. as $args |
if
($args.named | has("argsJson"))
then $args.named.argsJson[0] else $args.positional end | . as $args |
reduce $args[] as $arg ({
shortArgs: [],
longArgs: {},
nonArgs: []
}; (
if ($arg | test("^-[\\-a-zA-Z]") | not) then
.nonArgs += [$arg]
else
if $arg | startswith("--") then
$arg[2:] as $arg |
($arg | contains("=")) as $containsValue |
if $containsValue then
($arg | split("=")) as $argSplit |
$argSplit[0] as $arg | $argSplit[1] as $value |
.longArgs[$arg] |= $value
else
.longArgs[$arg] |= null
end
else
.shortArgs += [$arg[1:]]
end
end
));