Skip to content

Commit 2e6dea6

Browse files
committed
feat: new module l.parse_args. And l.parse_params is deprecated.
User should use l.parse_args instead of l.parse_params. The l.parse_params has some limitations on use. So it is deprecated and will be removed in soon.
1 parent 2dd4d84 commit 2e6dea6

File tree

7 files changed

+697
-70
lines changed

7 files changed

+697
-70
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,19 @@ indent_style = tab
2020
[{Dockerfile,Dockerfile.*,*.dockerfile}]
2121
indent_style = space
2222
indent_size = 2
23+
24+
[{/bin/*,/tools/*,**/*.{bats,bash,sh}}]
25+
26+
################################################################################
27+
# Below are shfmt options. #
28+
# See https://github.com/mvdan/sh/blob/master/cmd/shfmt/shfmt.1.scd#examples #
29+
################################################################################
30+
# --language-variant
31+
shell_variant = auto
32+
binary_next_line = false
33+
# --case-indent
34+
switch_case_indent = true
35+
space_redirects = false
36+
keep_padding = false
37+
# --func-next-line
38+
function_next_line = false

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ bump-minor:
2020

2121
bump-patch:
2222
./tools/release patch
23+
24+
.PHONY: build
25+
build:
26+
./build -y ./dist/lobash.bash

build

Lines changed: 37 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,10 @@
11
#!/usr/bin/env bash
22
# This script support Bash: 4.0+
33

4-
set -o errexit
5-
set -o nounset
6-
set -o pipefail
7-
set -o errtrace
4+
set -o errexit -o nounset -o pipefail -o errtrace
85
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit
96

10-
readlinkf() { # Modified from https://github.com/ko1nksm/readlinkf
11-
[ "${1:-}" ] || return 1
12-
13-
CDPATH='' # to avoid changing to an unexpected directory
14-
local max_symlinks=40
15-
local link
16-
local target=$1
17-
18-
[ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes
19-
[ -d "${target:-/}" ] && target="$target/"
20-
21-
cd -P . 2>/dev/null || return 1
22-
while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do
23-
if [ ! "$target" = "${target%/*}" ]; then
24-
case $target in
25-
/*) cd -P "${target%/*}/" 2>/dev/null || break ;;
26-
*) cd -P "./${target%/*}" 2>/dev/null || break ;;
27-
esac
28-
target=${target##*/}
29-
fi
30-
31-
if [ ! -L "$target" ]; then
32-
target="${PWD%/}${target:+/}${target}"
33-
printf '%s\n' "${target:-/}"
34-
return 0
35-
fi
36-
37-
link=$(ls -dl -- "$target" 2>/dev/null) || break
38-
target=${link#*" $target -> "}
39-
done
40-
return 1
41-
}
42-
43-
SCRIPT_PATH="$(readlinkf "$0")"
7+
SCRIPT_PATH="$(realpath "$0")"
448
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
459
readonly SCRIPT_PATH SCRIPT_DIR
4610

@@ -49,9 +13,9 @@ source "$SCRIPT_DIR/tools/colors.bash"
4913
# shellcheck source=./src/load_internals.bash
5014
source "$SCRIPT_DIR/src/load_internals.bash"
5115
_lobash.import_internals basic_meta_types module_meta rm erase_line
52-
_lobash.imports ask choose parse_params array_include union_array is_tty_available is_gnu_sed relative
16+
_lobash.imports ask choose parse_args array_include union_array is_tty_available is_gnu_sed relative
5317

54-
SUPPORTED_BASH_VERISONS=( 4.0 4.1 4.2 4.3 4.4 )
18+
SUPPORTED_BASH_VERISONS=(4.0 4.1 4.2 4.3 4.4)
5519

5620
if l.is_gnu_sed; then
5721
sedi() { sed -i "$@"; }
@@ -125,7 +89,7 @@ init_module() {
12589
deps=$(_lobash.get_module_metadata "$module_name" "Dependent")
12690
if [[ -n "$deps" ]]; then
12791
# shellcheck disable=2206
128-
deps=( ${deps//,/ } )
92+
deps=(${deps//,/ })
12993
for dep in "${deps[@]}"; do
13094
init_module "$dep"
13195
done
@@ -148,15 +112,15 @@ init_with_config_file() {
148112
echo -e "${GREY}To import category ${category_name}${RESET_ALL}"
149113
while read -r module_name; do
150114
init_module "$module_name"
151-
done < "$SCRIPT_DIR/src/internals/categories/${category_name,,}"
115+
done <"$SCRIPT_DIR/src/internals/categories/${category_name,,}"
152116
done
153117

154118
while read -r module_name; do
155119
init_module "$module_name"
156120
done < <(grep -E '^ - \[x\]' "$CONFIG_PATH" | sed -E 's/^ - \[x\] +(.+)/\1/')
157121

158122
# shellcheck disable=2207
159-
module_names=( $(l.union_array module_names) )
123+
module_names=($(l.union_array module_names))
160124
}
161125

162126
# Supported functions: VERBOSE_1, VERBOSE_2, VERBOSE_3, VERBOSE_4
@@ -175,13 +139,13 @@ init_verbose() {
175139
fi
176140

177141
local i=1
178-
while (( i <= 4 )); do
179-
if (( i <= VERBOSE )); then
142+
while ((i <= 4)); do
143+
if ((i <= VERBOSE)); then
180144
eval "VERBOSE_$i() { printf '%b[v=$i][%s:%s] %s%b\n' \"\$GREY\" \"\${BASH_LINENO}\" \"\${FUNCNAME[1]}\" \"\$1\" \"\$RESET_ALL\"; }"
181145
else
182146
eval "VERBOSE_$i() { true; }"
183147
fi
184-
i=$(( i + 1 ))
148+
i=$((i + 1))
185149
done
186150
}
187151

@@ -191,7 +155,7 @@ Usage: $0 [OPTIONS] [<output>]
191155
192156
Options:
193157
-h, --help Show usage.
194-
-c, --config Config file for building. If set, option "-p" will not work.
158+
-c, --config <filepath> Config file for building. If set, option "-p" will not work.
195159
-p <prefix> Prefix of variable names. [Default: l.]
196160
-y, --yes Overwrite output.
197161
-v <level> Show verbose. 0 means off. [Value: 0~4] [Default: 0]
@@ -204,17 +168,17 @@ EOF
204168

205169
# Reuse the last UNIQ_KEY if found
206170
set_uniq_key() {
207-
UNIQ_KEY=$( grep -E '^# UNIQ_KEY: ' "$TARGET" 2>/dev/null | sed -E 's/^# UNIQ_KEY: (.+)/\1/' || true)
171+
UNIQ_KEY=$(grep -E '^# UNIQ_KEY: ' "$TARGET" 2>/dev/null | sed -E 's/^# UNIQ_KEY: (.+)/\1/' || true)
208172

209173
if [[ -n $UNIQ_KEY ]]; then return 0; fi
210174

211175
# Lobash built by Lobash 0.4 has not "# UNIQ_KEY" label. So try to get it from "readonly _LOBASH_[_0-9]+_PREFIX="
212-
UNIQ_KEY=$( grep -E '^readonly _LOBASH_[_0-9]+_PREFIX=' "$TARGET" 2>/dev/null | sed -E 's/^readonly _LOBASH_([-_0-9]+)_PREFIX=.+/\1/' || true )
176+
UNIQ_KEY=$(grep -E '^readonly _LOBASH_[_0-9]+_PREFIX=' "$TARGET" 2>/dev/null | sed -E 's/^readonly _LOBASH_([-_0-9]+)_PREFIX=.+/\1/' || true)
213177

214178
if [[ -n $UNIQ_KEY ]]; then return 0; fi
215179

216180
# If not found UNIQ_KEY, generate one based on current lobash version and time.
217-
UNIQ_KEY=${VERSION//[^[:alnum:]]/_}_$(( $(date '+%s') - LOBASH_POUCH_TIME ))_$RANDOM
181+
UNIQ_KEY=${VERSION//[^[:alnum:]]/_}_$(($(date '+%s') - LOBASH_POUCH_TIME))_$RANDOM
218182
}
219183

220184
init() {
@@ -225,7 +189,7 @@ init() {
225189

226190
init_verbose
227191

228-
if (( ${#args[@]} == 0 )); then
192+
if ((${#args[@]} == 0)); then
229193
TARGET=$PWD/lobash.bash
230194
else
231195
if [[ -d ${args[0]} ]]; then
@@ -300,12 +264,11 @@ clean() {
300264
answer=$(l.ask "Existed file: ${TARGET}. Overwrite it?" N)
301265
echo -e "${GREY}$answer${RESET_ALL}"
302266
case $answer in
303-
YES )
304-
;;
305-
* )
306-
echo "Not overwrite it. No new Lobash file generated."
307-
exit 0
308-
;;
267+
YES) ;;
268+
*)
269+
echo "Not overwrite it. No new Lobash file generated."
270+
exit 0
271+
;;
309272
esac
310273
fi
311274

@@ -316,7 +279,7 @@ clean() {
316279
}
317280

318281
write() {
319-
printf -- '%s\n' "$*" >> "$TARGET"
282+
printf -- '%s\n' "$*" >>"$TARGET"
320283
}
321284

322285
fwrite() {
@@ -328,18 +291,16 @@ fwrite() {
328291
VERBOSE_3 "sed -E \"s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_CONST_PREFIX}([_a-zA-Z0-9]+)/${PUBLIC_CONST_PREFIX}\\1/g\""
329292

330293
if [[ $PREFIX == 'l.' ]]; then
331-
<"$1" \
332-
sed -E "/^# /d;s/${WORD_BOUNDARY}($prefixes)([_a-zA-Z0-9]+)/\\1${UNIQ_KEY}_\\2/g" \
333-
| sed -E "s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_CONST_PREFIX}([_a-zA-Z0-9]+)/${PUBLIC_CONST_PREFIX}\\1/g" \
334-
>> "$TARGET"
294+
<"$1" sed -E "/^# /d;s/${WORD_BOUNDARY}($prefixes)([_a-zA-Z0-9]+)/\\1${UNIQ_KEY}_\\2/g" |
295+
sed -E "s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_CONST_PREFIX}([_a-zA-Z0-9]+)/${PUBLIC_CONST_PREFIX}\\1/g" \
296+
>>"$TARGET"
335297
else
336298
VERBOSE_3 "sed -E \"s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_FUNC_PREFIX//\./\\.}([_a-zA-Z0-9]+)/${PREFIX}\\1/g\""
337299

338-
<"$1" \
339-
sed -E "/^# /d;s/${WORD_BOUNDARY}($prefixes)([_a-zA-Z0-9]+)/\\1${UNIQ_KEY}_\\2/g" \
340-
| sed -E "s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_CONST_PREFIX}([_a-zA-Z0-9]+)/${PUBLIC_CONST_PREFIX}\\1/g" \
341-
| sed -E "s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_FUNC_PREFIX//\./\\.}([_a-zA-Z0-9]+)/${PREFIX}\\1/g" \
342-
>> "$TARGET"
300+
<"$1" sed -E "/^# /d;s/${WORD_BOUNDARY}($prefixes)([_a-zA-Z0-9]+)/\\1${UNIQ_KEY}_\\2/g" |
301+
sed -E "s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_CONST_PREFIX}([_a-zA-Z0-9]+)/${PUBLIC_CONST_PREFIX}\\1/g" |
302+
sed -E "s/${WORD_BOUNDARY}${_LOBASH_PUBLIC_FUNC_PREFIX//\./\\.}([_a-zA-Z0-9]+)/${PREFIX}\\1/g" \
303+
>>"$TARGET"
343304
fi
344305
}
345306

@@ -415,7 +376,7 @@ generate() {
415376
_lobash.scan_module_metadata "$module_name"
416377
bashver=$(_lobash.get_module_metadata "$module_name" "Bash")
417378
compare=$(_lobash.semver_compare "$BASH_MIN_VERSION" "$bashver")
418-
if (( compare < 0 )); then
379+
if ((compare < 0)); then
419380
skips[$module_name]=true
420381
fi
421382
done
@@ -442,16 +403,22 @@ generate() {
442403

443404
set +u
444405
local skip_count=${#skips[@]}
445-
echo "Imported $(( ${#module_names[@]} - skip_count )) modules. Skipped ${skip_count} modules."
406+
echo "Imported $((${#module_names[@]} - skip_count)) modules. Skipped ${skip_count} modules."
446407
set -u
447408
echo -e "Generated Lobash file: ${GREEN}$TARGET${RESET_ALL}"
448409
}
449410

450411
declare -A opts=()
451412
declare -a args=()
413+
declare -A opts_def=(
414+
[opts]=opts
415+
[args]=args
416+
[-y --yes]='boolean'
417+
[-h --help]='boolean'
418+
)
452419

453420
main() {
454-
l.parse_params opts args "$@"
421+
l.parse_args opts_def "$@"
455422
init
456423
clean
457424
generate "$@"

example/modules/parse_args

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit -o nounset -o pipefail -o errtrace
4+
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit
5+
6+
SCRIPT_DIR="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
7+
readonly SCRIPT_DIR
8+
# shellcheck source=./init.bash
9+
source "$SCRIPT_DIR"/init.bash
10+
11+
declare -A opts=()
12+
declare -a args=()
13+
declare -A params_def=(
14+
[opts]=opts
15+
[args]=args
16+
[-f --flag]='boolean default:1'
17+
[-b]='boolean'
18+
)
19+
20+
foo() {
21+
l.parse_args params_def "$@"
22+
}
23+
24+
foo -a 3 -b12 -c=5 foo -d= bar -e -3 -4 a -F=abc -gh w -ij=5 -km= -5n -xzy --beep=boop baz --no-wow
25+
26+
echo 'foo -a 3 -b12 -c=5 foo -d= bar -e -3 -4 a -F=abc -gh w -ij=5 -km= -5n -xzy --beep=boop baz --no-wow'
27+
echo "[${#opts[@]} Options]"
28+
for key in "${!opts[@]}"; do
29+
echo "$key: ${opts[$key]}"
30+
done
31+
32+
printf "\n[%s Arguments]\n" "${#args[@]}"
33+
declare i=0
34+
for key in "${args[@]}"; do
35+
echo "$i: $key"
36+
((i += 1))
37+
done
38+
39+
echo "------Use Options------"
40+
41+
# NOTE: Use ${opts[c]:-} instead of ${opts[c]}. If `set -o nounset`, ${opts[c]} will throw error when option omitted.
42+
if [[ -n ${opts[c]:-} ]]; then
43+
echo "c=${opts[c]}"
44+
fi
45+
46+
echo "------Use Arguments------"
47+
echo "${args[0]} ${args[1]} ${args[2]}"

0 commit comments

Comments
 (0)