-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.sh
80 lines (61 loc) · 1.72 KB
/
cli.sh
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
[public:assoc] CLI
Cli::router(){
[private] route="$1"
if Type::function::exist "${CLI["$route":'main']}"; then
Cli::args "$route" "${argsArr[@]}"
${CLI["$route":'main']}
else
Cli::error "No command line found with $router"
fi
}
Cli::args(){
[private] route="$1"
if Type::function::exist ${CLI["$route":"args"]}; then
${CLI["$route":'args']} "${argsArr[@]}"
fi
}
Cli::help(){
[private] route="$1"
if Type::function::exist ${CLI["$route":'help']};then
${CLI["$route":'help']} ${*:2}
exit
else
printf '%s\n' "$HELP"
exit
fi
}
Cli::colorize(){
[private:assoc] COLORS=(
[BLACK]='\033[0;30m'
[RED]='\033[0;31m'
[GREEN]='\033[0;32m'
[YELLOW]='\033[0;33m'
[BLUE]='\033[0;34m'
[MAGENTA]='\033[0;35m'
[CYAN]='\033[0;36m'
[WHITE]='\033[0;37m'
)
printf '%s' "${COLORS[${1^^}]:-${COLORS[BLACK]}}"
}
Cli::list(){
printf "$(Cli::colorize green)$(Type::array::get::key ".*" CLI)$(Cli::colorize white)\n"
}
Cli::error(){
[private] msg="$*"
printf "$(Cli::colorize red)$msg$(Cli::colorize white)\n" >&2
}
Cli::error::stacktrace(){
[private:int] err=$?
set +o xtrace
[private] code="${1:-1}"
Cli::error "Error in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}' exited with status $err"
# Print out the stack trace described by $function_stack
if (( ${#FUNCNAME[@]} >> 2 )); then
Cli::error "Call tree:"
for ((i=1;i<${#FUNCNAME[@]}-1;i++)); do
Cli::error " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
done
fi
Cli::error "Exiting with status ${code}"
exit $err
}