Fail cleanly when an option is missing its value - #16
Open
SNO7E-G wants to merge 1 commit into
Open
Conversation
Value-taking options in the argument parser read "$2" without checking it exists. Under `set -u`, invoking one with no value (e.g. `ptc-cli.sh -s en -p`) aborted with a raw "$2: unbound variable" instead of a usable message. Guard the value-taking options at the top of the parse loop: a missing value now logs "Option '<opt>' requires a value." and exits 1. The "--opt=value" forms carry their value in $1 and are exempt.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary. A value-taking option invoked with no value now prints a usable error and exits 1, instead of a raw bash abort.
Problem. In
main()'s parser, value options doX="$2"; shift 2with no check. Underset -euo pipefail,ptc-cli.sh -s en -p(value omitted) reads an unbound$2and dies with"$2": unbound variable— opaque for anyone who mistypes a flag in a pipeline.Change. Add a guard at the top of the parse loop for the ten value-taking options (
-s -p -c -t -d --api-url --api-token --monitor-interval --monitor-max-attempts --action): when the option is the terminal argument ([[ $# -lt 2 ]]), logOption '<opt>' requires a value.andexit 1. The--opt=valueforms carry their value in$1and are exempt.Risk. Low — only the malformed-input error path changes. The guard never inspects
$2, so an empty value (-s "") or a flag-looking value (-s -p) behave exactly as before. Compatible withptc-action(always passes a value).Verified.
ptc-cli.sh -s en -pprints the clean error and exits 1;test-runner(12) andtest-exit-codes(22) green. Independent review confirmed no valid-input regression.