-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargument-parser.awk
More file actions
executable file
·87 lines (83 loc) · 3.79 KB
/
argument-parser.awk
File metadata and controls
executable file
·87 lines (83 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/gawk -f
##
# Assigns parsed reference from ARGC matched from acceptable arguments reference
# @note - Dunder variables are function scoped, and should **not** be assigned by caller
# @note - Use `gawk -h` to list parameters that parser may conflict with argument parsing
# @parameter {ArrayReference} _acceptable_arguments - Array reference of acceptable arguments
# @parameter {ArrayReference} _parsed_arguments - Array reference that parsed arguments should be saved to
# @parameter {string} __key__ - Associative array key that points to `_acceptable_arguments[__key__]` value
# @parameter {string[]} __acceptable_parts__ - Array split by `:` from `_acceptable_arguments[__key__]` value
# @parameter {string} __pattern__ - Regexp pattern from `_acceptable_arguments[__key__]` value
# @parameter {string} __type__ - Default `"value"` from `_acceptable_arguments[__key__]` value
# @parameter {number} __index__ - Index that points to value within `ARGC`
# @parameter {string[]} __argument_parts__ - Array split by `=` from `ARGC[__index__]` value
# @parameter {string} __parameter__ - Value from `__acceptable_parts__[1]`
# @author S0AndS0
# @license AGPL-3.0
# @example
# #!/usr/bin/gawk -f
#
#
# @include "argument-parser"
#
#
# BEGIN {
# delete parsed_arguments
# delete acceptable_arguments
#
# acceptable_arguments["usage"] = "--usage:boolean"
# acceptable_arguments["key"] = "--key|-k:value"
# acceptable_arguments["increment"] = "-I:increment"
#
# parse_script_arguments(acceptable_arguments, parsed_arguments)
#
# for (k in parsed_arguments) {
# print "parsed_arguments[\"" k "\"] ->", parsed_arguments[k]
# }
# }
function argument_parser(_acceptable_arguments, _parsed_arguments,
__key__, __acceptable_parts__, __pattern__, __type__,
__index__, __argument_parts__, __parameter__)
{
for (__index__ = 1; __index__ < ARGC; __index__++) {
for (__key__ in _acceptable_arguments) {
split(_acceptable_arguments[__key__], __acceptable_parts__, ":")
__pattern__ = __acceptable_parts__[1]
__type__ = __acceptable_parts__[2]
if (ARGV[__index__] ~ "^(" __pattern__ ")=.*$") {
split(ARGV[__index__], __argument_parts__, "=")
__parameter__ = __argument_parts__[1]
_parsed_arguments[__key__] = substr(ARGV[__index__], (length(__parameter__) + 2))
delete ARGV[__index__]
if (__type__ !~ "^(array|list)") {
delete _acceptable_arguments[__key__]
}
break
} else if (ARGV[__index__] ~ "^(" __pattern__ ")$") {
if (__type__ ~ "^bool") {
_parsed_arguments[__key__] = 1
delete ARGV[__index__]
delete _acceptable_arguments[__key__]
break
} else if (__type__ ~ "^increment") {
_parsed_arguments[__key__]++
delete ARGV[__index__]
break
} else if (__type__ ~ "^(array|list)") {
_parsed_arguments[__key__][(length(_parsed_arguments[__key__]) + 1)] = ARGV[__index__ + 1]
delete ARGV[__index__]
__index__++
delete ARGV[__index__]
break
} else {
_parsed_arguments[__key__] = ARGV[__index__ + 1]
delete ARGV[__index__]
__index__++
delete ARGV[__index__]
delete _acceptable_arguments[__key__]
break
}
}
}
}
}