-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.sh
More file actions
executable file
·162 lines (147 loc) · 3.33 KB
/
main.sh
File metadata and controls
executable file
·162 lines (147 loc) · 3.33 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Bootstrap
use header;
# Unset self Bashbox.meta hooks
unset -f bashbox::build::before bashbox::build::after bashbox::run::before bashbox::run::after;
#####################
### Public functions
#####################
use variables;
# use utils;
use std::print::log;
use std::print::helpgen;
use std::term::colors;
use std::io::file::check_newline;
use std::async::lockfile;
use std::native::sleep;
use argbash::common;
### Private functions
#####################
use subcommand;
use clap;
function print_help() {
println::helpgen "${_self_name^^}" \
--short-desc "\
Wannabe bash compiler\
" \
\
--usage "\
${_self_name} [OPTIONAL-OPTIONS] [SUBCOMMAND] <subcommand-arguments>\
" \
\
--options-desc "\
-V, --version<^>Print version info and exit
-v, --verbose<^>Use very verbose output
-q, --quiet<^>No output printed to stdout
--offline<^>Run without checking for update
-h, --help<^>Prints this help information\
" \
\
--subcommands "\
new<^>${SUBCOMMANDS_DESC[1]}
build<^>${SUBCOMMANDS_DESC[2]}
clean<^>${SUBCOMMANDS_DESC[3]}
install<^>${SUBCOMMANDS_DESC[4]}
selfinstall<^>${SUBCOMMANDS_DESC[5]}\
" \
\
--footer-msg "\
Try '${_self_name} <subcommand> --help' for more information on a specific command.
For bugreports: $___self_REPOSITORY\
";
}
function main() {
#####################
### Initialization
#####################
### Mutables
_self_name="${___self##*/}";
_arg_verbose=off;
_arg_quiet=off;
_arg_offline=off;
#####################
### Start of arg parse
#####################
# Assign optional parent arguments
# Drop/escape optional parent arguments
for _arg in "${@}"; do {
# Doesnt contain `--`` and is a whole word with leading `-`
if test "$_arg" != "--" && [[ "$_arg" == -* ]]; then {
case "$_arg" in
--verbose | -v)
_arg_verbose=on;
;;
--quiet | -q)
_arg_quiet=on;
;;
--offline)
_arg_offline=on;
;;
--version | -V)
echo "$___self_VERSION";
exit 0;
;;
--help | -h*)
print_help; exit 0;
;;
-C)
# _arg_path="$2";
cd "$2" || { log::error "$2 doesn't exist" || exit; };
shift;
;;
esac
shift;
} else {
break;
} fi
} done
unset _arg;
# for i in $(
# a=$#;
# until test $a -eq 0; do
# echo $a;
# ((a--));
# done
# ); do {
# echo "$i"
# eval "echo \$$i" | grep -E 'verbose|quiet|offline' 1>/dev/null && {
# set -- "${@:1:$i-1}" "${@:$i+1}";
# }
# } done
# unset i;
# TODO(LESSON): Dynamic argument parsing on bash is a nightmare. Well, at least for me on this script.
#####################
### Setup options
#####################
## Verbose
# test "$_arg_verbose" == on && test "$_arg_quiet" == off && {
# set -x;
# }
#####################
### Main execution
#####################
_subcommand_argv="${1:-}" && shift || true;
case "$_subcommand_argv" in
new | run | build | clean | install | selfinstall)
subcommand::$_subcommand_argv "$@";
;;
*)
## Check for Bashbox.sh custom user functions
clap "$@";
source "$_bashbox_meta";
if declare -F "$_subcommand_argv" &>/dev/null; then {
"$_subcommand_argv" "$@";
} else {
if test -n "$_subcommand_argv"; then {
log::warn "Unknown subcommand: $_subcommand_argv";
} fi
print_help;
if test -n "$_subcommand_argv"; then {
exit 1;
} else {
exit 0;
} fi
} fi
;;
esac
exit;
}