Skip to content

Commit

Permalink
Don't attempt to process arguments if there were declaration errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Oct 4, 2023
1 parent 2696d82 commit 28599e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes:

* `bashy-core`:
* `stderr-msg`: New option `--file-line`.
* `arg-processor`: Tightened up error checking.

### v2.5 -- 2023-10-04

Expand Down
24 changes: 24 additions & 0 deletions scripts/lib/bashy-core/arg-processor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
# name. Generated functions use the convention `_argproc:<name>`, to make the
# origin clear and to avoid collisions.

# Was there an error during argument and option declaration?
_argproc_declarationError=0

# List of statements to run just before parsing. This includes:
#
# * global variable assignment statements
Expand Down Expand Up @@ -269,6 +272,11 @@ function process-args {
local _argproc_error=0
local _argproc_s

if (( _argproc_declarationError )); then
error-msg 'Cannot process arguments, due to declaration errors.'
return 1
fi

# Run all the pre-parse statements.
for _argproc_s in "${_argproc_initStatements[@]}"; do
eval "${_argproc_s}"
Expand Down Expand Up @@ -635,6 +643,7 @@ function _argproc_janky-args {
if [[ ${a} =~ ^--. ]]; then
if ! [[ ${a} =~ ^--([a-z]+)(=.*)?$ ]]; then
error-msg --file-line=2 "Invalid option syntax: ${a}"
_argproc_declarationError=1
return 1
fi

Expand All @@ -643,6 +652,7 @@ function _argproc_janky-args {

if ! [[ ${argSpecs} =~ " ${name} " ]]; then
error-msg --file-line=2 "Unknown option: --${name}"
_argproc_declarationError=1
return 1
fi

Expand Down Expand Up @@ -692,6 +702,7 @@ function _argproc_janky-args {
;;
*)
error-msg --file-line=2 "Unknown arg-processing option: --${name}"
_argproc_declarationError=1
return 1
;;
esac
Expand All @@ -702,6 +713,7 @@ function _argproc_janky-args {
else
error-msg --file-line=2 "Value required for option --${name}."
fi
_argproc_declarationError=1
return 1
fi
elif [[ ${a} == '--' ]]; then
Expand All @@ -714,24 +726,29 @@ function _argproc_janky-args {
optsDone=1
else
error-msg --file-line=2 "Invalid option syntax: ${a}"
_argproc_declarationError=1
return 1
fi
done

if (( !optsDone || (${#args[@]} == 0) )); then
error-msg --file-line=2 'Missing argument specification.'
_argproc_declarationError=1
return 1
elif (( ${#args[@]} > 1 && !multiArg )); then
error-msg --file-line=2 'Too many arguments.'
_argproc_declarationError=1
return 1
elif (( gotInit && optRequired )); then
# Special case: `--init` is meaningless if `--required` was passed.
error-msg --file-line=2 'Cannot use both --required and --init.'
_argproc_declarationError=1
return 1
elif [[ ${argSpecs} =~ ' call '|' var ' ]]; then
# Special case for `--call` and `--var` (which always go together).
if [[ (${optCall} == '') && (${optVar} == '') ]]; then
error-msg --file-line=2 'Must use at least one of --call or --var.'
_argproc_declarationError=1
return 1
fi
fi
Expand All @@ -752,6 +769,7 @@ function _argproc_parse-spec {
--value-eq) valueOk=1; valueWithEq=1 ;;
*)
error-msg --file-line=1 "Unrecognized option: $1"
_argproc_declarationError=1
return 1
;;
esac
Expand All @@ -762,6 +780,7 @@ function _argproc_parse-spec {

if ! [[ ${spec} =~ ^([a-zA-Z0-9][-a-zA-Z0-9]*)(/[a-zA-Z])?(=.*)?$ ]]; then
error-msg --file-line=1 "Invalid spec: ${spec}"
_argproc_declarationError=1
return 1
fi

Expand All @@ -773,6 +792,7 @@ function _argproc_parse-spec {
specAbbrev="${abbrev:1}" # `:1` to drop the slash.
elif [[ ${abbrev} != '' ]]; then
error-msg --file-line=1 "Abbrev not allowed in spec: ${spec}"
_argproc_declarationError=1
return 1
fi

Expand All @@ -789,6 +809,7 @@ function _argproc_parse-spec {
fi
elif [[ ${value} != '' ]]; then
error-msg --file-line=1 "Value not allowed in spec: ${spec}"
_argproc_declarationError=1
return 1
fi
}
Expand All @@ -804,6 +825,7 @@ function _argproc_regex-filter-check {
for arg in "$@"; do
if [[ ! (${arg} =~ ${regex}) ]]; then
error-msg --file-line=1 "Invalid value for ${desc}: ${arg}"
_argproc_declarationError=1
return 1
fi
done
Expand All @@ -819,6 +841,7 @@ function _argproc_set-arg-description {

if declare -F "${funcName}" >/dev/null; then
error-msg --file-line=1 "Duplicate argument: ${longName}"
_argproc_declarationError=1
return 1
fi

Expand All @@ -835,6 +858,7 @@ function _argproc_set-arg-description {
;;
*)
error-msg --file-line=1 "Unknown type: ${typeName}"
_argproc_declarationError=1
return 1
;;
esac
Expand Down

0 comments on commit 28599e7

Please sign in to comment.