Skip to content

Commit 6dd0d14

Browse files
committed
BREAKING CHANGE: Remove 'init souce' stage and call command directly
1 parent 79c9e75 commit 6dd0d14

File tree

12 files changed

+107
-40
lines changed

12 files changed

+107
-40
lines changed

bin/args-init

Lines changed: 0 additions & 12 deletions
This file was deleted.

bin/args.parse

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
# https://github.com/bashup/realpaths
4+
realpath.location(){ realpath.follow "$1"; realpath.absolute "$REPLY" ".."; }
5+
realpath.dirname() { REPLY=.; ! [[ $1 =~ /+[^/]+/*$|^//$ ]] || REPLY="${1%${BASH_REMATCH[0]}}"; REPLY=${REPLY:-/}; }
6+
realpath.follow() {
7+
local target
8+
while [[ -L "$1" ]] && target=$(readlink -- "$1"); do
9+
realpath.dirname "$1"
10+
# Resolve relative to symlink's directory
11+
[[ $REPLY != . && $target != /* ]] && REPLY=$REPLY/$target || REPLY=$target
12+
# Break out if we found a symlink loop
13+
for target; do [[ $REPLY == "$target" ]] && break 2; done
14+
# Add to the loop-detect list and tail-recurse
15+
set -- "$REPLY" "$@"
16+
done
17+
REPLY="$1"
18+
}
19+
realpath.absolute() {
20+
REPLY=$PWD; local eg=extglob; ! shopt -q $eg || eg=; ${eg:+shopt -s $eg}
21+
while (($#)); do case $1 in
22+
//|//[^/]*) REPLY=//; set -- "${1:2}" "${@:2}" ;;
23+
/*) REPLY=/; set -- "${1##+(/)}" "${@:2}" ;;
24+
*/*) set -- "${1%%/*}" "${1##${1%%/*}+(/)}" "${@:2}" ;;
25+
''|.) shift ;;
26+
..) realpath.dirname "$REPLY"; shift ;;
27+
*) REPLY="${REPLY%/}/$1"; shift ;;
28+
esac; done; ${eg:+shopt -u $eg}
29+
}
30+
31+
32+
33+
realpath.location "${BASH_SOURCE[0]}"
34+
if [ -d "$REPLY"/../lib/bash-args ]; then
35+
# distribution: linux distribution
36+
ARGS_LIB_DIR="$REPLY/../lib/bash-args"
37+
elif [ -d "$REPLY"/../lib ]; then
38+
# distribution channel: git / basher
39+
ARGS_LIB_DIR="$REPLY/../lib"
40+
else
41+
echo "Error: Could not determine \$ARGS_LIB_DIR"
42+
exit 1
43+
fi
44+
45+
source "$ARGS_LIB_DIR/util/util.sh"
46+
source "$ARGS_LIB_DIR/args-parse.sh"

bin/bash-args-init

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/usr/bin/env bash

docs/troubleshooting.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
If you are receiving opaque errors, the following may help
44

5+
## `bash: /usr/lib/jvm/default/bin: syntax error: operand expected (error token is "/usr/lib/jvm/default/bin")`
6+
7+
Ensure `args` exists as an associate array and NOT an index array. Create it _before_ calling out to `args.parse`
8+
9+
10+
```sh
11+
# Wrong
12+
declare -a args
13+
14+
# Correct
15+
declare -A args
16+
```
17+
518
## Not declaring variables
619

20+
TODO: test this
21+
722
If you wish to use a variable, please declare it before invoking `args.parse`. If your shell context has `set -u` enabled, you may have to declare it for variables that you do not use

glue.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
name = "args"
1+
name = "bash-args"
22
using = "Bash"

lib/args-parse.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ args.parse() {
331331
IFS="$oldIFS"
332332

333333
# shellcheck disable=SC2034
334-
argsHelpText="Usage:
334+
declare -g argsHelpText="Usage:
335335
$execName [flags] <arguments>
336336
${descriptionOutput}${argumentOutput}${flagOutput}"
337337
}
338+
339+
args.parse "$@"

package/aur/run.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ set -ex
44
# makerepropkg *.zst
55
# repro -f *.zst
66

7-
# makepkg -Cfsr
87
makepkg -Cfsrc
8+
# makepkg -Cfsrc
99

1010
namcap PKGBUILD
1111
namcap ./*.zst
1212

1313
pacman -Qlp ./*.zst
14+
15+
toast --shell

package/aur/toast.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
image: archlinux
2+
default: main
3+
tasks:
4+
init:
5+
command: |
6+
set -Eeuo pipefail
7+
pacman -Syu --noconfirm base-devel sudo man-db expect
8+
sed -ie 's/# %sudo/%sudo/g' /etc/sudoers
9+
groupadd sudo
10+
useradd -m op -s /bin/bash -G sudo
11+
chpasswd <<< 'op:password'
12+
13+
main:
14+
dependencies: [ init ]
15+
cache: false
16+
user: op
17+
mount_paths:
18+
- .
19+
command: |
20+
set -Eeuo pipefail
21+
./run2.sh

test/args.bats

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#!/usr/bin/env bats
22
set -Eeuo pipefail
33

4-
source ./bin/args-init
5-
64
@test "longOption with value" {
75
declare -A args=()
86

9-
args.parse "--port" "3005" <<-'EOF'
7+
source ./bin/args.parse --port 3005 <<-'EOF'
108
@flag [port] {3000} - The port to open on
119
EOF
1210

@@ -18,7 +16,7 @@ source ./bin/args-init
1816
@test "shortOption with value" {
1917
declare -A args=()
2018

21-
args.parse "-p" "3005" <<-'EOF'
19+
source ./bin/args.parse -p 3005 <<-'EOF'
2220
@flag [.p] {3000} - The port to open on
2321
EOF
2422

@@ -29,7 +27,7 @@ source ./bin/args-init
2927
@test "longOption and shortOption with longOption value" {
3028
declare -A args=()
3129

32-
args.parse "--port" "3005" <<-'EOF'
30+
source ./bin/args.parse --port 3005 <<-'EOF'
3331
@flag [port.p] {3000} - The port to open on
3432
EOF
3533

@@ -43,7 +41,7 @@ source ./bin/args-init
4341
@test "longOption and shortOption with shortOption value" {
4442
declare -A args=()
4543

46-
args.parse "-p" "3005" <<-'EOF'
44+
source ./bin/args.parse -p 3005 <<-'EOF'
4745
@flag [port.p] {3000} - The port to open on
4846
EOF
4947

@@ -59,7 +57,7 @@ source ./bin/args-init
5957
@test "longOption and default" {
6058
declare -A args=()
6159

62-
args.parse <<-'EOF'
60+
source ./bin/args.parse <<-'EOF'
6361
@flag [port] {3000} - The port to open on
6462
EOF
6563

@@ -69,7 +67,7 @@ source ./bin/args-init
6967
@test "shortOption and default" {
7068
declare -A args=()
7169

72-
args.parse <<-'EOF'
70+
source ./bin/args.parse <<-'EOF'
7371
@flag [.p] {3000} - The port to open on
7472
EOF
7573

@@ -79,7 +77,7 @@ source ./bin/args-init
7977
@test "longOption and shortOption" {
8078
declare -A args=()
8179

82-
args.parse <<-'EOF'
80+
source ./bin/args.parse <<-'EOF'
8381
@flag [port.p] {3000} - The port to open on
8482
EOF
8583

@@ -93,7 +91,7 @@ source ./bin/args-init
9391
declare -A args=()
9492

9593
! (
96-
args.parse --port --something nother <<-'EOF'
94+
source ./bin/args.parse --port --something nother <<-'EOF'
9795
@flag <port> {} - The port to open on
9896
EOF
9997
)
@@ -103,7 +101,7 @@ source ./bin/args-init
103101
declare -A args=()
104102

105103
(
106-
args.parse --port --something nother <<-'EOF'
104+
source ./bin/args.parse --port --something nother <<-'EOF'
107105
@flag <port> - The port to open on
108106
@flag <something> - something
109107
EOF
@@ -114,7 +112,7 @@ source ./bin/args-init
114112
declare -A args=()
115113

116114
! (
117-
args.parse --port - <<-'EOF'
115+
source ./bin/args.parse --port - <<-'EOF'
118116
@flag [port] {} - The port to open on
119117
EOF
120118
)
@@ -124,7 +122,7 @@ source ./bin/args-init
124122
declare -A args=()
125123

126124
(
127-
args.parse --port - <<-'EOF'
125+
source ./bin/args.parse --port - <<-'EOF'
128126
@flag [port] - The port to open on
129127
EOF
130128
)
@@ -134,7 +132,7 @@ source ./bin/args-init
134132
declare -A args=()
135133

136134
! (
137-
args.parse <<-'EOF'
135+
source ./bin/args.parse <<-'EOF'
138136
@flag <port> The port to open on
139137
EOF
140138
)

test/argsCommands.bats

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#!/usr/bin/env bats
22
set -Eeuo pipefail
33

4-
source ./bin/args-init
5-
64
@test "argsCommands is correct basic" {
75
declare -A args
86
declare -a argsCommands=()
97

10-
args.parse alfa bravo <<-'EOF'
8+
source ./bin/args.parse alfa bravo <<-'EOF'
119
@flag [port.p] {3000} - The port to open on
1210
EOF
1311

@@ -21,7 +19,7 @@ source ./bin/args-init
2119
declare -A args
2220
declare -a argsCommands=()
2321

24-
args.parse --port 3005 serve --user admin --enable-security now <<-'EOF'
22+
source ./bin/args.parse --port 3005 serve --user admin --enable-security now <<-'EOF'
2523
@flag [port.p] {3000} - The port to open on
2624
@flag [user] {} - User
2725
@flag [enable-security] - Enable security
@@ -36,7 +34,7 @@ source ./bin/args-init
3634
declare -A args
3735
declare -a argsCommands=()
3836

39-
args.parse --port 3005 serve now --enable-security last <<-'EOF'
37+
source ./bin/args.parse --port 3005 serve now --enable-security last <<-'EOF'
4038
@flag [port.p] {3000} - The port to open on
4139
@flag [enable-security] - Whether to enable security
4240
EOF

0 commit comments

Comments
 (0)