Skip to content

Commit

Permalink
Add arguments with value to parse-generic
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Neff <andy@visionsystemsinc.com>
  • Loading branch information
andyneff committed May 9, 2024
1 parent dc58c86 commit 63a4d7a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 12 additions & 1 deletion linux/command_tools.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function get_array_from_environment()
# .. function:: parse-generic
#
# :Arguments: ``$1``.. - Arguments to be sent to some command
# :Parameters: [``arguments_with_value``] - An array of command arguments that must take one argument
# :Output: * command_args - Arguments to some command, before the subcommand (run, build, do_something, etc...)
# * subcommand - The subcommand specified
# * subcommand_args - Arguments for the specified subcommand
Expand All @@ -116,7 +117,17 @@ function parse-generic()
while (( ${#} )); do
case "${1}" in
-*)
# Everything takes zero arguments, so no need to enumerate -d --debug -h --help --nocolor -q --quiet -s --silent -v --verbose --version
# Anything that takes one arguments from the array arguments_with_value
for arg in ${arguments_with_value[@]+"${arguments_with_value[@]}"}; do
# No need to check "${arg}=", that will be handled as taking "zero argument" below
if [ "${1}" = "${arg}" ]; then
command_args+=("${1}" "${2}")
shift 2
# Break out of the case
continue 2
fi
done
# Everything takes zero arguments, so no need to enumerate examples like: -d --debug -h --help
command_args+=("${1}")
shift 1
;;
Expand Down
27 changes: 27 additions & 0 deletions tests/test-command_tools.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ begin_test "Generic command argument parse"
[ -z "${subcommand+set}" ]
unset command_args subcommand_args subcommand

arguments_with_value=(-a --append)

# Command and command_args
parse-generic do -something else
assert_array_values command_args
Expand All @@ -132,6 +134,31 @@ begin_test "Generic command argument parse"
[ "${subcommand}" = "do" ]
unset command_args subcommand_args subcommand

# Add args with a value into the mix
parse-generic -a ok do -a "not ok"
assert_array_values command_args -a ok
assert_array_values subcommand_args -a "not ok"
[ "${subcommand}" = "do" ]
unset command_args subcommand_args subcommand

parse-generic -try --to -a do -something else
assert_array_values command_args -try --to -a do -something
assert_array_values subcommand_args
[ "${subcommand}" = "else" ]
unset command_args subcommand_args subcommand

parse-generic -try --to -a= do -something else
assert_array_values command_args -try --to -a=
assert_array_values subcommand_args -something else
[ "${subcommand}" = "do" ]
unset command_args subcommand_args subcommand

parse-generic -try --to --append stuff do -something else
assert_array_values command_args -try --to --append stuff
assert_array_values subcommand_args -something else
[ "${subcommand}" = "do" ]
unset command_args subcommand_args subcommand

)
end_test

Expand Down

0 comments on commit 63a4d7a

Please sign in to comment.