diff options
author | EuAndreh <eu@euandre.org> | 2021-06-22 18:04:57 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-06-22 18:05:04 -0300 |
commit | 298cfec38bd597b3c171f11d0d4451f6134243cf (patch) | |
tree | 27dded8f84ae5a43bc4cd18ed089778bfaea8da6 | |
parent | TODOs.md: Mark #task-ada9b39a-cc42-97b2-bd02-ad9c0adc475c as DONE (diff) | |
download | git-permalink-298cfec38bd597b3c171f11d0d4451f6134243cf.tar.gz git-permalink-298cfec38bd597b3c171f11d0d4451f6134243cf.tar.xz |
src/git-permalink.sh.in: Fix #task-bebbe847-f552-be4b-b886-70a621162b69
The issue wan't with getopts but with my usage of it (well, now that's
obvious that the problem wouldn't be in the implementation, but on how
I was using).
The problem was on:
1) my confusion on when and where to "shift";
2) make sure to look for the "--help" and "--version" long flags before
getopts, because it doesn't know about long flags.
Those fixed, the tests are now working again.
-rwxr-xr-x | src/git-permalink.sh.in | 28 | ||||
-rwxr-xr-x | tests/cli-opts.sh | 6 |
2 files changed, 16 insertions, 18 deletions
diff --git a/src/git-permalink.sh.in b/src/git-permalink.sh.in index e0367bc..9c26293 100755 --- a/src/git-permalink.sh.in +++ b/src/git-permalink.sh.in @@ -94,19 +94,18 @@ version() { printf 'git-permalink-@VERSION@ @DATE@\n' } -PRINTONLY=false -while getopts 'phV' flag; do +# shellcheck disable=2068 +for flag in $@; do case "$flag" in - p) - PRINTONLY=true - shift + --) + break ;; - h) + --help) usage help exit ;; - V) + --version) version exit ;; @@ -115,19 +114,18 @@ while getopts 'phV' flag; do esac done -# shellcheck disable=2068 -for flag in $@; do +PRINTONLY=false +while getopts 'phV' flag; do case "$flag" in - --) - shift - break + p) + PRINTONLY=true ;; - --help) + h) usage help exit ;; - --version) + V) version exit ;; @@ -136,6 +134,8 @@ for flag in $@; do esac done +shift $(($OPTIND - 1)) + if [ -z "${1:-}" ]; then printf '%s\n\n' "$MSG_MISSING_FILE" >&2 usage >&2 diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh index 26d2a58..dae3fbd 100755 --- a/tests/cli-opts.sh +++ b/tests/cli-opts.sh @@ -98,10 +98,9 @@ test_help_flags() { OUT="$(mktemp)" ERR="$(mktemp)" - LANG=en_US.UTF-8 sh git-permalink --something somethign -h 1>"$OUT" 2>"$ERR" + LANG=en_US.UTF-8 sh git-permalink --something something -h 1>"$OUT" 2>"$ERR" STATUS=$? assert_status 0 - assert_empty_stderr assert_usage "$OUT" assert_fgrep_stdout 'Usage' assert_fgrep_stdout 'Options' @@ -141,10 +140,9 @@ test_version_flags() { OUT="$(mktemp)" ERR="$(mktemp)" - sh git-permalink --abc xyz -V 1>"$OUT" 2>"$ERR" + sh git-permalink -V --abc xyz 1>"$OUT" 2>"$ERR" STATUS=$? assert_status 0 - assert_empty_stderr assert_grep_stdout "$REGEX" OUT="$(mktemp)" |