Skip to content

Commit da86278

Browse files
committed
chore: parse arguments flexibly install.sh
1 parent cebe482 commit da86278

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

docs/installation.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ https://github.com/TypedDevs/bashunit/releases/download/{{ pkg.version }}/checks
3131

3232
#### Define custom tag and folder
3333

34-
The installation script can receive arguments in two forms:
34+
The installation script can receive arguments (in any order):
3535

3636
```bash
3737
curl -s https://bashunit.typeddevs.com/install.sh | bash -s [dir] [version]
38-
curl -s https://bashunit.typeddevs.com/install.sh | bash -s [version]
3938
```
4039
- `[dir]`: the destiny directory to save the executable bashunit; `lib` by default
41-
- `[version]`: the [release](https://github.com/TypedDevs/bashunit/releases) to download, for instance `{{ pkg.version }}`; `latest` by default. When only `[version]` is provided and follows the `N.N.N` pattern, it will be installed in the `lib` directory.
40+
- `[version]`: the [release](https://github.com/TypedDevs/bashunit/releases) to download, for instance `{{ pkg.version }}`; `latest` by default.
4241

4342
::: tip
4443
You can use `beta` as `[version]` to get the next non-stable preview release.

install.sh

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,32 @@ function install() {
6363
######### MAIN ##########
6464
#########################
6565

66-
DIR=${1-lib}
67-
VERSION=${2-latest}
68-
69-
# When only one argument is provided, treat it as the version if it matches
70-
# the pattern N.N.N (e.g. 0.20.0)
71-
if [[ $# -eq 1 && $DIR =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
72-
VERSION=$DIR
73-
DIR="lib"
66+
# Defaults
67+
DIR="lib"
68+
VERSION="latest"
69+
70+
function is_version() {
71+
[[ "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ || "$1" == "latest" || "$1" == "beta" ]]
72+
}
73+
74+
# Parse arguments flexibly
75+
if [[ $# -eq 1 ]]; then
76+
if is_version "$1"; then
77+
VERSION="$1"
78+
else
79+
DIR="$1"
80+
fi
81+
elif [[ $# -eq 2 ]]; then
82+
if is_version "$1"; then
83+
VERSION="$1"
84+
DIR="$2"
85+
elif is_version "$2"; then
86+
DIR="$1"
87+
VERSION="$2"
88+
else
89+
echo "Invalid arguments. Expected version or directory." >&2
90+
exit 1
91+
fi
7492
fi
7593

7694
BASHUNIT_GIT_REPO="https://github.com/TypedDevs/bashunit"

0 commit comments

Comments
 (0)