Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-15] Support for adjacent variable-value syntax #20

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM debian:stable-slim AS base
ARG BUILD_SHA
ARG BUILD_DATE
LABEL org.opencontainers.image.vendor="Dmytro Konstantinov" \
org.opencontainers.image.source="https://github.com/UrsaDK/getopts_long" \
org.opencontainers.image.revision="${BUILD_SHA}" \
Expand Down Expand Up @@ -103,6 +105,7 @@ RUN TZ=UTC git show --pretty="%H%+ad" | head -2 > ./VERSION \
&& rm -Rf \
./.git \
./dockerfs \
&& ./bin/bats \
&& ./bin/kcov
WORKDIR /mnt
VOLUME ["/mnt"]
Expand Down
3 changes: 1 addition & 2 deletions bin/bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ shellcheck --format="tty" --shell="bash" --check-sourced \

# Run bats without generating code coverage
echo "Running tests: ${*:-"${TEST_DIR}"}"
/usr/local/bin/bats ${*:+'--tap'} --recursive "${@:-"${TEST_DIR}"}"
[[ -z "${*}" ]] && "$(dirname "${0}")/kcov"
/usr/local/bin/bats --recursive "${*:-"${TEST_DIR}"}"
2 changes: 2 additions & 0 deletions bin/docker
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ case "${COMMAND}" in
;;
'r'|'run')
[[ -t 1 ]] && IT='-it' # Check if output is attached to a TTY
# shellcheck disable=SC2086
docker container run ${GITHUB_ACTIONS:+-u root} \
--rm --init ${IT} -v "${PWD}:/mnt" "${IMAGE}" "${@}"
;;
Expand All @@ -72,6 +73,7 @@ case "${COMMAND}" in
'e'|'exec')
[[ -t 1 ]] && IT='-it' # Check if output is attached to a TTY
CONTAINER_ID="$(docker container ls -qlf "ancestor=${IMAGE}")"
#shellcheck disable=SC2086
docker container exec ${IT} "${CONTAINER_ID}" "${@}"
;;
's'|'stop')
Expand Down
16 changes: 10 additions & 6 deletions bin/kcov
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ path_exists "${TEST_DIR}"
TEMP_DIR="$(mktemp -d -t kcov.XXXXXXXXX)"
trap 'rm -Rf ${TEMP_DIR}' EXIT

# Run kcov
echo "Generating coverage report: ${COVERAGE_DIR}/index.html"
KCOV_CMD="/usr/local/bin/kcov --clean --include-path=${LIB_DIR} ${@} \
# Run kcov + bats
echo "Generating coverage report: ${TEMP_DIR}/bats/index.html"
echo -n "Running the whole tests suite: "
KCOV_CMD="/usr/local/bin/kcov --clean --include-path=${LIB_DIR} ${*} \
${TEMP_DIR} /usr/local/bin/bats --recursive ${TEST_DIR}"
if [[ ${EUID} -eq 0 ]]; then
KCOV_USER="$(ls -l "${0}" | awk '{print $3}')"
echo
KCOV_USER="$(find "${0}" -maxdepth 0 -printf '%u')"
[[ "${KCOV_USER}" == "root" ]] && die_with_kcov_user_error

chown -R "${KCOV_USER}" "${TEMP_DIR}"
Expand All @@ -83,8 +85,10 @@ sed -i -E 's#/(home|mnt)/##g' \
"${TEMP_DIR}"/bats/*.{json,xml} "${TEMP_DIR}"/bats/metadata/*

# Present kcov report
rm -Rf "${COVERAGE_DIR}"
cp -LR "${TEMP_DIR}/bats" "${COVERAGE_DIR}"
if rm -Rf "${COVERAGE_DIR}" 2>/dev/null; then
echo "Publishing coverage report: ${COVERAGE_DIR}/index.html";
cp -LR "${TEMP_DIR}/bats" "${COVERAGE_DIR}"
fi

# Total code coverage
COVERAGE="$(jq -r .percent_covered "${COVERAGE_DIR}/coverage.json")"
Expand Down
18 changes: 14 additions & 4 deletions lib/getopts_long.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@ getopts_long() {
builtin getopts "${optspec_short}" "${optvar}" "${@}" || return 1
[[ "${!optvar}" == '-' ]] || return 0

printf -v "${optvar}" "%s" "${OPTARG%%=*}"
if [[ "${OPTARG}" == *=* ]]; then
printf -v "${optvar}" "%s" "${OPTARG%%=*}"
else
for optspec in $(echo "${optspec_long}" | tr ' ' '\n' | sort -ur); do
if [[ "${optspec}" == *: && "${OPTARG}" == "${optspec%?}"* ]]; then
printf -v "${optvar}" "%s" "${optspec%?}"
break
fi
done
[[ "${!optvar}" == '-' ]] && printf -v "${optvar}" "%s" "${OPTARG}"
fi

if [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}:([[:space:]]|$) ]]; then
OPTARG="${OPTARG#${!optvar}}"
if [[ " ${optspec_long} " == *" ${!optvar}: "* ]]; then
OPTARG="${OPTARG#"${!optvar}"}"
OPTARG="${OPTARG#=}"

# Missing argument
Expand All @@ -39,7 +49,7 @@ getopts_long() {
unset OPTARG && printf -v "${optvar}" '?'
fi
fi
elif [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}([[:space:]]|$) ]]; then
elif [[ " ${optspec_long} " == *" ${!optvar} "* ]]; then
unset OPTARG
else
# Invalid option
Expand Down
21 changes: 21 additions & 0 deletions test/bats/github_15.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bats

load ../test_helper

@test "${FEATURE}: short option, silent" {
compare '-o-- user_arg' \
'-o-- user_arg'
}
@test "${FEATURE}: short option, verbose" {
compare '-o-- user_arg' \
'-o-- user_arg'
}

@test "${FEATURE}: long option, silent" {
compare '-o-- user_arg' \
'--option-- user_arg'
}
@test "${FEATURE}: long option, verbose" {
compare '-o-- user_arg' \
'--option-- user_arg'
}
28 changes: 14 additions & 14 deletions test/bats/invalid_arguments.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ load ../test_helper
compare '-i' \
'--invalid' \
'/^INVALID OPTION -- /d'
expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i'
expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid'
expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i'
expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid'
}
@test "${FEATURE}: long option, verbose" {
compare '-i' \
Expand All @@ -41,8 +41,8 @@ load ../test_helper
compare '-i user_arg' \
'--invalid user_arg' \
'/^INVALID OPTION -- /d'
expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i'
expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid'
expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i'
expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid'
}
@test "${FEATURE}: long option, extra arguments, verbose" {
compare '-i user_arg' \
Expand All @@ -55,53 +55,53 @@ load ../test_helper
@test "${FEATURE}: short option, terminator, extra arguments, silent" {
compare '-i -- user_arg' \
'-i -- user_arg'
expect "${actual_lines[5]}" == '$@: user_arg'
expect "${getopts_long_lines[5]}" == '$@: user_arg'
}
@test "${FEATURE}: short option, terminator, extra arguments, verbose" {
compare '-i -- user_arg' \
'-i -- user_arg' \
's/getopts_long-verbose/getopts-verbose/g'
expect "${actual_lines[6]}" == '$@: user_arg'
expect "${getopts_long_lines[6]}" == '$@: user_arg'
}

@test "${FEATURE}: long option, terminator, extra arguments, silent" {
compare '-i -- user_arg' \
'--invalid -- user_arg' \
'/^INVALID OPTION -- /d'
expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i'
expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid'
expect "${actual_lines[5]}" == '$@: user_arg'
expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i'
expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid'
expect "${getopts_long_lines[5]}" == '$@: user_arg'
}
@test "${FEATURE}: long option, terminator, extra arguments, verbose" {
compare '-i -- user_arg' \
'--invalid -- user_arg' \
's/getopts_long-verbose: (.*) invalid$/getopts-verbose: \1 i/g'
expect "${actual_lines[6]}" == '$@: user_arg'
expect "${getopts_long_lines[6]}" == '$@: user_arg'
}

# terminator followed by options

@test "${FEATURE}: terminator, short option, extra arguments, silent" {
compare '-- -i user_arg' \
'-- -i user_arg'
expect "${actual_lines[4]}" == '$@: -i user_arg'
expect "${getopts_long_lines[4]}" == '$@: -i user_arg'
}
@test "${FEATURE}: terminator, short option, extra arguments, verbose" {
compare '-- -i user_arg' \
'-- -i user_arg' \
's/getopts_long-verbose/getopts-verbose/g'
expect "${actual_lines[4]}" == '$@: -i user_arg'
expect "${getopts_long_lines[4]}" == '$@: -i user_arg'
}

@test "${FEATURE}: terminator, long option, extra arguments, silent" {
compare '-- -i user_arg' \
'-- --invalid user_arg' \
'/^\$@: /d'
expect "${actual_lines[4]}" == '$@: --invalid user_arg'
expect "${getopts_long_lines[4]}" == '$@: --invalid user_arg'
}
@test "${FEATURE}: terminator, long option, extra arguments, verbose" {
compare '-- -i user_arg' \
'-- --invalid user_arg' \
'/^\$@: /d'
expect "${actual_lines[4]}" == '$@: --invalid user_arg'
expect "${getopts_long_lines[4]}" == '$@: --invalid user_arg'
}
4 changes: 2 additions & 2 deletions test/bats/no_arguments.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ load ../test_helper
@test "${FEATURE}: terminator, extra arguments, silent" {
compare '-- user_arg' \
'-- user_arg'
expect "${actual_lines[4]}" == '$@: user_arg'
expect "${getopts_long_lines[4]}" == '$@: user_arg'
}
@test "${FEATURE}: terminator, extra arguments, verbose" {
compare '-- user_arg' \
'-- user_arg'
expect "${actual_lines[4]}" == '$@: user_arg'
expect "${getopts_long_lines[4]}" == '$@: user_arg'
}
57 changes: 39 additions & 18 deletions test/bats/option_supplied.bats
Original file line number Diff line number Diff line change
Expand Up @@ -45,77 +45,77 @@ load ../test_helper
@test "${FEATURE}: short option, terminator, extra arguments, silent" {
compare '-o user_val -- user_arg' \
'-o user_val -- user_arg'
expect "${actual_lines[5]}" == '$@: user_arg'
expect "${getopts_long_lines[5]}" == '$@: user_arg'
}
@test "${FEATURE}: short option, terminator, extra arguments, verbose" {
compare '-o user_val -- user_arg' \
'-o user_val -- user_arg'
expect "${actual_lines[5]}" == '$@: user_arg'
expect "${getopts_long_lines[5]}" == '$@: user_arg'
}

@test "${FEATURE}: long option, terminator, extra arguments, silent" {
compare '-o user_val -- user_arg' \
'--option user_val -- user_arg'
expect "${actual_lines[5]}" == '$@: user_arg'
expect "${getopts_long_lines[5]}" == '$@: user_arg'
}
@test "${FEATURE}: long option, terminator, extra arguments, verbose" {
compare '-o user_val -- user_arg' \
'--option user_val -- user_arg'
expect "${actual_lines[5]}" == '$@: user_arg'
expect "${getopts_long_lines[5]}" == '$@: user_arg'
}

# multiple same arguments

@test "${FEATURE}: short option, multiple same arguments, silent" {
compare '-o user_val1 -o user_val2' \
'-o user_val1 -o user_val2'
expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2'
expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2'
}
@test "${FEATURE}: short option, multiple same arguments, verbose" {
compare '-o user_val1 -o user_val2' \
'-o user_val1 -o user_val2'
expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2'
expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2'
}

@test "${FEATURE}: long option, multiple same arguments, silent" {
compare '-o user_val1 -o user_val2' \
'--option user_val1 --option user_val2'
expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2'
expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2'
}
@test "${FEATURE}: long option, multiple same arguments, verbose" {
compare '-o user_val1 -o user_val2' \
'--option user_val1 --option user_val2'
expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2'
expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1'
expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2'
}

# terminator followed by options

@test "${FEATURE}: terminator, short option, extra arguments, silent" {
compare '-- -o user_val user_arg' \
'-- -o user_val user_arg'
expect "${actual_lines[4]}" == '$@: -o user_val user_arg'
expect "${getopts_long_lines[4]}" == '$@: -o user_val user_arg'
}
@test "${FEATURE}: terminator, short option, extra arguments, verbose" {
compare '-- -o user_val user_arg' \
'-- -o user_val user_arg'
expect "${actual_lines[4]}" == '$@: -o user_val user_arg'
expect "${getopts_long_lines[4]}" == '$@: -o user_val user_arg'
}

@test "${FEATURE}: terminator, long option, extra arguments, silent" {
compare '-- -o user_val user_arg' \
'-- --option user_val user_arg' \
'/^\$@: /d'
expect "${actual_lines[4]}" == '$@: --option user_val user_arg'
expect "${getopts_long_lines[4]}" == '$@: --option user_val user_arg'
}
@test "${FEATURE}: terminator, long option, extra arguments, verbose" {
compare '-- -o user_val user_arg' \
'-- --option user_val user_arg' \
'/^\$@: /d'
expect "${actual_lines[4]}" == '$@: --option user_val user_arg'
expect "${getopts_long_lines[4]}" == '$@: --option user_val user_arg'
}

# option without an argument
Expand All @@ -134,8 +134,8 @@ load ../test_helper
compare '-o' \
'--option' \
'/^MISSING ARGUMENT -- /d'
expect "${expected_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=o'
expect "${actual_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=option'
expect "${bash_getopts_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=o'
expect "${getopts_long_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=option'
}
@test "${FEATURE}: long option, missing value, verbose" {
compare '-o' \
Expand Down Expand Up @@ -182,3 +182,24 @@ load ../test_helper
compare '-o =user_val' \
'--option =user_val'
}

# option with an adjoined value

@test "${FEATURE}: short option, adjoined value, silent" {
compare '-ouser_val' \
'-ouser_val'
}
@test "${FEATURE}: short option, adjoined value, verbose" {
compare '-ouser_val' \
'-ouser_val'
}

@test "${FEATURE}: long option, adjoined value, silent" {
compare '-ouser_val' \
'--optionuser_val'
}

@test "${FEATURE}: long option, adjoined value, verbose" {
compare '-ouser_val' \
'--optionuser_val'
}
Loading
Loading