Skip to content

Commit

Permalink
Tighten up validity check.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Oct 4, 2023
1 parent 1052879 commit a76a55d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions scripts/lib/bashy-core/arg-processor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/02-core/03-arg-processor/90-init-required/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/02-core/03-arg-processor/90-init-required/the-cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.'

0 comments on commit a76a55d

Please sign in to comment.