Skip to content

Fail cleanly when an option is missing its value - #16

Open
SNO7E-G wants to merge 1 commit into
OnTheGoSystems:mainfrom
SNO7E-G:fix-arg-validation
Open

Fail cleanly when an option is missing its value#16
SNO7E-G wants to merge 1 commit into
OnTheGoSystems:mainfrom
SNO7E-G:fix-arg-validation

Conversation

@SNO7E-G

@SNO7E-G SNO7E-G commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

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 do X="$2"; shift 2 with no check. Under set -euo pipefail, ptc-cli.sh -s en -p (value omitted) reads an unbound $2 and 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 ]]), log Option '<opt>' requires a value. and exit 1. The --opt=value forms carry their value in $1 and 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 with ptc-action (always passes a value).

Verified. ptc-cli.sh -s en -p prints the clean error and exits 1; test-runner (12) and test-exit-codes (22) green. Independent review confirmed no valid-input regression.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant