Proposal: add a 'mandatory' 'param' attribute - #68
Open
esaporski wants to merge 6 commits into
Open
Conversation
esaporski
commented
Jan 9, 2026
esaporski
commented
Jan 9, 2026
esaporski
commented
Jan 9, 2026
esaporski
commented
Jan 9, 2026
esaporski
commented
Jan 9, 2026
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.
Hi,
This pull request addresses the problems cited in the issue #41.
I think it would be a good idea to have a way to flag a parameter as mandatory, so we don't need to implement checks for all necessary parameters after the argument parsing :)
Implementation
To track if the parameter was passed to the parser, I created a "control variable" to check if the flag is defined. For a parameter with a short option
-p, the variable is name_p_flag_declared. For example:For a parameter with a long option
--param, it would be__param_flag_declared. And for a parameter with both short and long options-p | --param, it uses the short option_p_flag_declared.If we want to know which parameter was not defined, we cannot set an error like the other errors using something like
set "mandatory" "$1" && breakbecause if the parameter was not defined, it will never reach thissetstatement inside the switch case.After the switch case is done, even if the list of arguments is still empty, we need to check for the control variables for the mandatory parameters:
Then, we parse the error after all the other errors:
If
$1is empty and the control variable is also empty, it means we are missing a mandatory parameter. Else, we have another validation error.Docs
docs/Reference.md:param:attributes:mandatory:BOOLEAN - Flag parameter as mandatoryexamples/advanced.sh:parser_definitionexamples/basic.sh:parser_definitionTests
getoptions_base_spec.shWhen a mandatory parameter is missing:
Other errors have priority over the mandatory parameter:
For multiple mandatory parameters, it will respect the order of the parser definition:
Custom error handling:
getoptions_abbr_spec.shabbrenabled and mandatory parameter defined with a long option:abbrenabled globally but disabled for mandatory parameter and parameter defined with long and short option: