blob: 90e4998a4eb16fc6ca6d77b329b249d9b7af6772 (
plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
option_value() {
local -r option="$1"; shift
local -r argument="${CLI_OPTION_ARGUMENT[$option]:-value}"
if (( $# == 0 )) || [ -z "$1" ]; then
printf 'Error: %s requires a %s\n' "$option" "$argument" >&2
usage
exit 1
fi
printf '%s\n' "$1"
}
apply_cli_override() {
local -r config_target="$1"; shift
if [[ -v "SHURIKEN_CLI_OVERRIDES[$config_target]" ]]; then
printf -v "$config_target" '%s' \
"${SHURIKEN_CLI_OVERRIDES[$config_target]}"
fi
}
apply_cli_overrides() {
local config_target
for config_target in "${CLI_CONFIG_OVERRIDE_TARGETS[@]}"; do
apply_cli_override "$config_target"
done
if (( ${#SHURIKEN_CLI_SYNC_DESTINATIONS[@]} > 0 )); then
SYNC_DESTINATIONS=("${SHURIKEN_CLI_SYNC_DESTINATIONS[@]}")
fi
}
set_cli_option_value() {
local -r option="$1"; shift
local -r value="$1"; shift
local config_target
if [ "$option" = --sync-destination ]; then
SHURIKEN_CLI_SYNC_DESTINATIONS+=("$value")
SHURIKEN_CLI_HAS_CONFIG_OVERRIDES='yes'
return
fi
config_target="${CLI_OPTION_CONFIG_TARGET[$option]:-}"
if [ -n "$config_target" ]; then
SHURIKEN_CLI_OVERRIDES["$config_target"]="$value"
SHURIKEN_CLI_HAS_CONFIG_OVERRIDES='yes'
return
fi
printf -v "${CLI_OPTION_TARGET[$option]}" '%s' "$value"
}
set_cli_constant_option() {
local -r option="$1"; shift
local -r value="${CLI_OPTION_VALUE[$option]}"
local config_target
config_target="${CLI_OPTION_CONFIG_TARGET[$option]:-}"
if [ -n "$config_target" ]; then
SHURIKEN_CLI_OVERRIDES["$config_target"]="$value"
SHURIKEN_CLI_HAS_CONFIG_OVERRIDES='yes'
return
fi
printf -v "${CLI_OPTION_TARGET[$option]}" '%s' "$value"
}
set_cli_action() {
local -r selected_action="$1"; shift
if [ -n "$SHURIKEN_CLI_ACTION" ]; then
usage
exit 1
fi
SHURIKEN_CLI_ACTION="$selected_action"
}
parse_cli_arguments() {
local option_arg
local option
local option_kind
while (( $# > 0 )); do
option="$1"
shift
option_kind="${CLI_OPTION_KIND[$option]:-}"
case "$option_kind" in
value)
option_arg=$(option_value "$option" "$@")
set_cli_option_value "$option" "$option_arg"
shift
;;
flag|output)
set_cli_constant_option "$option"
;;
action)
set_cli_action "$option"
;;
*)
usage
exit 1
;;
esac
done
}
|