simplified command-line argument matching

This commit is contained in:
Jack
2020-05-02 12:43:09 +03:00
parent 9dfc77f2fd
commit 99b4c73fd8
+27 -27
View File
@@ -2,7 +2,7 @@
# Push-button installer of macOS on VirtualBox # Push-button installer of macOS on VirtualBox
# (c) myspaghetti, licensed under GPL2.0 or higher # (c) myspaghetti, licensed under GPL2.0 or higher
# url: https://github.com/myspaghetti/macos-guest-virtualbox # url: https://github.com/myspaghetti/macos-guest-virtualbox
# version 0.90.5 # version 0.90.6
# Dependencies: bash coreutils gzip unzip wget xxd dmg2img # Dependencies: bash coreutils gzip unzip wget xxd dmg2img
# Supported versions: # Supported versions:
@@ -973,22 +973,23 @@ press enter when prompted, less than ten times, to complete the installation.
${highlight_color}USAGE${default_color} ${highlight_color}USAGE${default_color}
${low_contrast_color}${0} [STAGE]... ${default_color} ${low_contrast_color}${0} [STAGE]... ${default_color}
The script is divided into stages. Stage titles may be given as command-line The installation is divided into stages. Stage titles may be given as command-
arguments for the script. When the script is run with no command-line line arguments for the script. When the script is run with no command-line
arguments, each of the available stages except \"${low_contrast_color}documentation${default_color}\" and arguments, each of the stages is performed in succession in the order listed:
\"${low_contrast_color}troubleshoot${default_color}\" is performed in succession in the order listed:
${low_contrast_stages} ${low_contrast_stages}
Either of the stages \"${low_contrast_color}documentation${default_color}\" and \"${low_contrast_color}troubleshoot${default_color}\" is only performed when Other than the stages above, the command-line arguments \"${low_contrast_color}documentation${default_color}\" and
it is specified as the first command-line argument, with all subsequent \"${low_contrast_color}troubleshoot${default_color}\" are available. \"${low_contrast_color}troubleshoot${default_color}\" outputs system information,
arguments ignored. When specified after any argument, either of \"${low_contrast_color}documentation${default_color}\" VirtualBox logs, and checksums for some installation files. \"${low_contrast_color}documentation${default_color}\"
and \"${low_contrast_color}troubleshoot${default_color}\" is ignored. outputs the script's documentation. If \"${low_contrast_color}documentation${default_color}\" is the first argument,
no other arguments are parsed.
The stage \"${low_contrast_color}check_shell${default_color}\" is always performed when the script loads. The stage \"${low_contrast_color}check_shell${default_color}\" is always performed when the script loads.
The stages \"${low_contrast_color}check_gnu_coreutils_prefix${default_color}\", \"${low_contrast_color}set_variables${default_color}\", and The stages \"${low_contrast_color}check_gnu_coreutils_prefix${default_color}\", \"${low_contrast_color}set_variables${default_color}\", and
\"${low_contrast_color}check_dependencies${default_color}\" are always performed when any stage title other than \"${low_contrast_color}check_dependencies${default_color}\" are always performed when any stage title other than
\"${low_contrast_color}documentation${default_color}\" or \"${low_contrast_color}troubleshoot${default_color}\" is specified as the first \"${low_contrast_color}documentation${default_color}\" is specified as the first argument, and the rest of the
argument, and the rest of the stages are parsed only after the checks pass. specified stages are performed only after the checks pass.
${highlight_color}EXAMPLES${default_color} ${highlight_color}EXAMPLES${default_color}
${low_contrast_color}${0} configure_vm${default_color} ${low_contrast_color}${0} configure_vm${default_color}
@@ -1126,6 +1127,7 @@ Further information is available at the following URL:
} }
function troubleshoot() { function troubleshoot() {
echo '################################################################################'
head -n 5 "${0}" head -n 5 "${0}"
echo '################################################################################' echo '################################################################################'
echo 'BASH_VERSION '"${BASH_VERSION}" echo 'BASH_VERSION '"${BASH_VERSION}"
@@ -1421,25 +1423,23 @@ stages='
populate_virtual_disks populate_virtual_disks
populate_macos_target populate_macos_target
delete_temporary_files delete_temporary_files
documentation
troubleshoot
' '
[[ -z "${1}" ]] && for stage in ${stages}; do ${stage}; done && exit
check_shell check_shell
[[ "${1}" = "documentation" ]] && documentation && exit [[ "${1}" == "documentation" ]] && documentation && exit
if [[ "${1}" = "troubleshoot" ]]; then set_variables; check_dependencies >/dev/null; troubleshoot; exit; fi valid_arguments=(${stages//$'[\r\n]'/ } troubleshoot documentation)
stages_without_newlines="${stages//[$'\r\n']/ }" # replace newline with space character for specified_arg in "$@"; do
stages_without_newlines="${stages_without_newlines//documentation/}" # strip all occurrences of "documentation" there_is_a_match=""
stages_without_newlines="${stages_without_newlines//troubleshoot/}" # strip all occurrences of "troubleshoot" # doing matching the long way to prevent delimiter confusion
[[ -z "${1}" ]] && for stage in ${stages_without_newlines}; do ${stage}; done && exit for valid_arg in "${valid_arguments[@]}"; do
# every stage name must be preceded and followed by a space character [[ "${valid_arg}" = "${specified_arg}" ]] && there_is_a_match="true" && break
# for the command-line argument checking below to work done
for argument in $@; do if [[ -z "${there_is_a_match}" ]]; then
if [[ "${stages_without_newlines}" != *" ${argument} "* ]]; then echo $'\n'"One or more specified arguments is not recognized."
echo $'\n'"Can't parse one or more specified arguments. See documentation" echo $'\nRecognized stages:\n'"${stages}"
echo "by entering the following command:" echo $'Other recognized arguments:\n\n documentation\n troubleshoot'
echo $'\n'"View documentation by entering the following command:"
would_you_like_to_know_less would_you_like_to_know_less
echo $'\n'"Available stages: ${stages}"
exit exit
fi fi
done done