Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arguments with value to parse-generic #506

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
andyneff marked this conversation as resolved.
Show resolved Hide resolved
# :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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Anything that takes one arguments from the array arguments_with_value
# Anything that takes one argument 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# No need to check "${arg}=", that will be handled as taking "zero argument" below
# No need to check "${arg}=", that will be handled as taking "zero arguments" 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Everything takes zero arguments, so no need to enumerate examples like: -d --debug -h --help
# Everything else 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
Loading