From a76a55daf7a250fed4c35434cf453c19b93d6c8b Mon Sep 17 00:00:00 2001 From: Dan Bornstein Date: Wed, 4 Oct 2023 12:56:02 -0700 Subject: [PATCH] Tighten up validity check. --- scripts/lib/bashy-core/arg-processor.sh | 5 +++++ tests/02-core/03-arg-processor/90-init-required/expect.md | 5 ++++- tests/02-core/03-arg-processor/90-init-required/the-cmd | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/scripts/lib/bashy-core/arg-processor.sh b/scripts/lib/bashy-core/arg-processor.sh index ebeb6e5..06667e8 100644 --- a/scripts/lib/bashy-core/arg-processor.sh +++ b/scripts/lib/bashy-core/arg-processor.sh @@ -744,6 +744,11 @@ function _argproc_janky-args { error-msg --file-line=2 'Cannot use both --required and --init.' _argproc_declarationError=1 return 1 + elif (( gotInit )) && [[ ${optVar} == '' ]]; then + # Special case: `--init` is meaningless without `--var`. + error-msg --file-line=2 'Must use --var when --init is used.' + _argproc_declarationError=1 + return 1 elif [[ ${argSpecs} =~ ' call '|' var ' ]]; then # Special case for `--call` and `--var` (which always go together). if [[ (${optCall} == '') && (${optVar} == '') ]]; then diff --git a/tests/02-core/03-arg-processor/90-init-required/expect.md b/tests/02-core/03-arg-processor/90-init-required/expect.md index f967286..e701db7 100644 --- a/tests/02-core/03-arg-processor/90-init-required/expect.md +++ b/tests/02-core/03-arg-processor/90-init-required/expect.md @@ -3,9 +3,12 @@ ### stderr ``` the-cmd: -the-cmd:18: Cannot use both --required and --init. the-cmd:19: Cannot use both --required and --init. the-cmd:20: Cannot use both --required and --init. +the-cmd:21: Cannot use both --required and --init. +the-cmd:24: Must use --var when --init is used. +the-cmd:25: Must use --var when --init is used. +the-cmd:26: Must use --var when --init is used. Cannot process arguments, due to declaration errors. the-cmd -- test command diff --git a/tests/02-core/03-arg-processor/90-init-required/the-cmd b/tests/02-core/03-arg-processor/90-init-required/the-cmd index 893a431..8de6d48 100755 --- a/tests/02-core/03-arg-processor/90-init-required/the-cmd +++ b/tests/02-core/03-arg-processor/90-init-required/the-cmd @@ -15,10 +15,16 @@ define-usage $' This is a test command. ' +# Can't use both `--init` and `--required`. opt-value --required --var=var1 --init=florp some-value opt-choice --init=maybe --required --var=var3 yes no positional-arg --init=x --var=var4 --required some-position +# Can't use `--init` without `--var`. +opt-value --init=x --call=func1 beep +opt-choice --init=x --call=func2 bomp blump +positional-arg --init=x --call=func3 plomp + process-args "$@" || usage --short echo 'Nopers.'