From f26df8a237f7a394fe92518fc35e37c192e67aad Mon Sep 17 00:00:00 2001 From: mnq78 Date: Tue, 29 Oct 2024 13:36:42 -0300 Subject: [PATCH 1/5] Update easyoptions.sh --- .../scripts/easy-options/easyoptions.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh index 772c0a6..100ec6d 100644 --- a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh +++ b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh @@ -109,7 +109,7 @@ parse_options() { option_name=${option%=$option_var} if [[ "${#option_name}" = "1" ]]; then short_options="${short_options}${option_name}" - if [[ "${#option_var}" > "1" ]]; then + if [[ "${#option_var}" -gt "1" ]]; then short_option_vars+=("$option_var") fi fi From 0c97d6ef7b9364b2dfdba4696e1bc23220c42d4f Mon Sep 17 00:00:00 2001 From: Nahuel Quiroga Date: Tue, 29 Oct 2024 14:02:33 -0300 Subject: [PATCH 2/5] fix(lint): fixing spaces --- .../scripts/easy-options/easyoptions.sh | 278 +++++++++--------- 1 file changed, 139 insertions(+), 139 deletions(-) diff --git a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh index 100ec6d..b8b2822 100644 --- a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh +++ b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh @@ -56,157 +56,157 @@ ## show_error() { - echo "Error: $1." >&2 - echo "See --help for usage and options." >&2 + echo "Error: $1." >&2 + echo "See --help for usage and options." >&2 } parse_documentation() { - documentation="$(grep "^##" "$(which "$0")")(no-trim)" - documentation=$(echo "$documentation" | sed -r "s/## ?//" | sed -r "s/@script.name/$(basename "$0")/g" | sed "s/@#/@/g") - documentation=${documentation%(no-trim)} + documentation="$(grep "^##" "$(which "$0")")(no-trim)" + documentation=$(echo "$documentation" | sed -r "s/## ?//" | sed -r "s/@script.name/$(basename "$0")/g" | sed "s/@#/@/g") + documentation=${documentation%(no-trim)} } parse_options() { - local short_option_vars - local short_options - local documentation - local next_is_value - local argument - - local option - local option_name - local option_value - local option_var - - local known_option - local known_option_name - local known_option_var - - # Parse known options from documentation - if [[ -z ${options+defined} ]]; then - parse_documentation - while read -r line; do - case "$line" in - "-h, --help"*) continue ;; - "--help, -h"*) continue ;; - -*," "--*) option=$(echo "$line" | awk -F'(^-|, --| )' '{ print $2"="$3 }') ;; - --*," "-*) option=$(echo "$line" | awk -F'(--|, -| )' '{ print $3"="$2 }') ;; - --*=*) option=$(echo "$line" | awk -F'(--|=| )' '{ print $2"=?" }') ;; - --*" "*) option=$(echo "$line" | awk -F'(--| )' '{ print $2 }') ;; - *) continue ;; - esac - options+=("$option") - done <<< "$documentation" + local short_option_vars + local short_options + local documentation + local next_is_value + local argument + + local option + local option_name + local option_value + local option_var + + local known_option + local known_option_name + local known_option_var + + # Parse known options from documentation + if [[ -z ${options+defined} ]]; then + parse_documentation + while read -r line; do + case "$line" in + "-h, --help"*) continue ;; + "--help, -h"*) continue ;; + -*," "--*) option=$(echo "$line" | awk -F'(^-|, --| )' '{ print $2"="$3 }') ;; + --*," "-*) option=$(echo "$line" | awk -F'(--|, -| )' '{ print $3"="$2 }') ;; + --*=*) option=$(echo "$line" | awk -F'(--|=| )' '{ print $2"=?" }') ;; + --*" "*) option=$(echo "$line" | awk -F'(--| )' '{ print $2 }') ;; + *) continue ;; + esac + options+=("$option") + done <<<"$documentation" + fi + + options+=(h=help) + arguments=() + + # Prepare known options + for option in "${options[@]}"; do + option_var=${option#*=} + option_name=${option%=$option_var} + if [[ "${#option_name}" = "1" ]]; then + short_options="${short_options}${option_name}" + if [[ "${#option_var}" -gt "1" ]]; then + short_option_vars+=("$option_var") + fi fi - - options+=(h=help) - arguments=() - - # Prepare known options - for option in "${options[@]}"; do - option_var=${option#*=} - option_name=${option%=$option_var} - if [[ "${#option_name}" = "1" ]]; then - short_options="${short_options}${option_name}" - if [[ "${#option_var}" -gt "1" ]]; then - short_option_vars+=("$option_var") - fi + done + + # Extract regular arguments + index=1 + parameters=() + for argument in "$@"; do + if [[ "$argument" = -* ]]; then + parameters+=("$argument") + for known_option in "${options[@]}"; do + known_option_var=${known_option#*=} + known_option_name=${known_option%=$known_option_var} + if [[ "$known_option_var" = "?" && "$argument" = --$known_option_name ]]; then + next_is_value="yes" + break fi - done - - # Extract regular arguments - index=1 - parameters=() - for argument in "$@"; do - if [[ "$argument" = -* ]]; then - parameters+=("$argument") - for known_option in "${options[@]}"; do - known_option_var=${known_option#*=} - known_option_name=${known_option%=$known_option_var} - if [[ "$known_option_var" = "?" && "$argument" = --$known_option_name ]]; then - next_is_value="yes" - break - fi - done - else - if [[ -z "$next_is_value" ]]; then - arguments+=("${!index}") - else - parameters+=("$argument") - fi - next_is_value="" + done + else + if [[ -z "$next_is_value" ]]; then + arguments+=("${!index}") + else + parameters+=("$argument") + fi + next_is_value="" + fi + index=$((index + 1)) + done + set -- "${parameters[@]}" + + # Parse the provided options + while getopts ":${short_options}-:" option; do + option="${option}${OPTARG}" + option_value="" + + # Set the corresponding variable for known options + for known_option in "${options[@]}" "${short_option_vars[@]}"; do + known_option_var=${known_option#*=} + known_option_name=${known_option%=$known_option_var} + + # Short option + if [[ "$option" = "$known_option_name" ]]; then + option_value="yes" + known_option_var=$(echo "$known_option_var" | tr "-" "_") + eval "$known_option_var=\"$option_value\"" + break + + # Long option + elif [[ "$option" = -$known_option_name && "$known_option_var" != "?" ]]; then + option_value="yes" + known_option_var=$(echo "$known_option_var" | tr "-" "_") + eval "$known_option_var=\"$option_value\"" + break + + # Long option with value in next parameter + elif [[ "$option" = -$known_option_name && "$known_option_var" = "?" ]]; then + eval option_value="\$$OPTIND" + if [[ -z "$option_value" || "$option_value" = -* ]]; then + show_error "you must specify a value for --$known_option_name" + exit 1 fi - index=$((index + 1)) + OPTIND=$((OPTIND + 1)) + known_option_var=$(echo "$known_option_name" | tr "-" "_") + eval "$known_option_var=\"$option_value\"" + break + + # Long option with value after equal sign + elif [[ "$option" = -$known_option_name=* && "$known_option_var" = "?" ]]; then + option_value=${option#*=} + known_option_var=$(echo "$known_option_name" | tr "-" "_") + eval "$known_option_var=\"$option_value\"" + break + + # Long option with unnecessary value + elif [[ "$option" = -$known_option_name=* && "$known_option_var" != "?" ]]; then + option_value=${option#*=} + show_error "--$known_option_name does not accept a value, you specified \"$option_value\"" + exit 1 + fi done - set -- "${parameters[@]}" - - # Parse the provided options - while getopts ":${short_options}-:" option; do - option="${option}${OPTARG}" - option_value="" - - # Set the corresponding variable for known options - for known_option in "${options[@]}" "${short_option_vars[@]}"; do - known_option_var=${known_option#*=} - known_option_name=${known_option%=$known_option_var} - - # Short option - if [[ "$option" = "$known_option_name" ]]; then - option_value="yes" - known_option_var=$(echo "$known_option_var" | tr "-" "_") - eval "$known_option_var=\"$option_value\"" - break - - # Long option - elif [[ "$option" = -$known_option_name && "$known_option_var" != "?" ]]; then - option_value="yes" - known_option_var=$(echo "$known_option_var" | tr "-" "_") - eval "$known_option_var=\"$option_value\"" - break - - # Long option with value in next parameter - elif [[ "$option" = -$known_option_name && "$known_option_var" = "?" ]]; then - eval option_value="\$$OPTIND" - if [[ -z "$option_value" || "$option_value" = -* ]]; then - show_error "you must specify a value for --$known_option_name" - exit 1 - fi - OPTIND=$((OPTIND + 1)) - known_option_var=$(echo "$known_option_name" | tr "-" "_") - eval "$known_option_var=\"$option_value\"" - break - - # Long option with value after equal sign - elif [[ "$option" = -$known_option_name=* && "$known_option_var" = "?" ]]; then - option_value=${option#*=} - known_option_var=$(echo "$known_option_name" | tr "-" "_") - eval "$known_option_var=\"$option_value\"" - break - - # Long option with unnecessary value - elif [[ "$option" = -$known_option_name=* && "$known_option_var" != "?" ]]; then - option_value=${option#*=} - show_error "--$known_option_name does not accept a value, you specified \"$option_value\"" - exit 1 - fi - done - - # Unknown option - if [[ -z "$option_value" ]]; then - option=${option%%=*} - [[ "$option" = \?* ]] && option=${option#*\?} - show_error "unrecognized option -$option" - exit 1 - fi - # Help option - if [[ -n "$help" ]]; then - [[ -z "$documentation" ]] && parse_documentation - echo "$documentation" - exit - fi - done + # Unknown option + if [[ -z "$option_value" ]]; then + option=${option%%=*} + [[ "$option" = \?* ]] && option=${option#*\?} + show_error "unrecognized option -$option" + exit 1 + fi + + # Help option + if [[ -n "$help" ]]; then + [[ -z "$documentation" ]] && parse_documentation + echo "$documentation" + exit + fi + done } parse_options "$@" From a1f5dd36d148be1e08e3b66a01cf88dd217bead1 Mon Sep 17 00:00:00 2001 From: Nahuel Quiroga Date: Tue, 29 Oct 2024 14:09:50 -0300 Subject: [PATCH 3/5] fix(lint): fixing variables --- .../scripts/easy-options/easyoptions.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh index b8b2822..d774a8b 100644 --- a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh +++ b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh @@ -106,7 +106,7 @@ parse_options() { # Prepare known options for option in "${options[@]}"; do option_var=${option#*=} - option_name=${option%=$option_var} + option_name=${option%="$option_var"} if [[ "${#option_name}" = "1" ]]; then short_options="${short_options}${option_name}" if [[ "${#option_var}" -gt "1" ]]; then @@ -123,7 +123,7 @@ parse_options() { parameters+=("$argument") for known_option in "${options[@]}"; do known_option_var=${known_option#*=} - known_option_name=${known_option%=$known_option_var} + known_option_name=${known_option%="$known_option_var"} if [[ "$known_option_var" = "?" && "$argument" = --$known_option_name ]]; then next_is_value="yes" break @@ -149,7 +149,7 @@ parse_options() { # Set the corresponding variable for known options for known_option in "${options[@]}" "${short_option_vars[@]}"; do known_option_var=${known_option#*=} - known_option_name=${known_option%=$known_option_var} + known_option_name=${known_option%="$known_option_var"} # Short option if [[ "$option" = "$known_option_name" ]]; then From 8557845e5a6212e9f7f0048ab28d3346cf1becdf Mon Sep 17 00:00:00 2001 From: Nahuel Quiroga Date: Wed, 30 Oct 2024 15:00:33 -0300 Subject: [PATCH 4/5] fix(bash): fixes for testing --- .../scripts/easy-options/easyoptions.sh | 2 +- .../fastapi-simple-docker-pip/scripts/easy-options/example.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh index d774a8b..a4fed37 100644 --- a/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh +++ b/examples/fastapi-simple-docker-pip/scripts/easy-options/easyoptions.sh @@ -61,7 +61,7 @@ show_error() { } parse_documentation() { - documentation="$(grep "^##" "$(which "$0")")(no-trim)" + documentation="$(grep "^##" "$0")" documentation=$(echo "$documentation" | sed -r "s/## ?//" | sed -r "s/@script.name/$(basename "$0")/g" | sed "s/@#/@/g") documentation=${documentation%(no-trim)} } diff --git a/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh b/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh index 336adf9..53f9255 100644 --- a/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh +++ b/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh @@ -21,7 +21,7 @@ ## format. ROOT="$(realpath "$(dirname "$0")")" -. "${ROOT}/opts.sh" || exit # Bash implementation, slower +. "${ROOT}/easyoptions.sh" || exit # Bash implementation, slower # Boolean and parameter options [[ -n "$some_option" ]] && echo "Option specified: --some-option" From 432a3a6c1ed48735f9468532bf90d3b84b1ef0d6 Mon Sep 17 00:00:00 2001 From: Nahuel Quiroga Date: Wed, 30 Oct 2024 16:17:01 -0300 Subject: [PATCH 5/5] fix(lint): fixing lint issues --- .../scripts/easy-options/example.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh b/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh index 53f9255..9c9d5a4 100644 --- a/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh +++ b/examples/fastapi-simple-docker-pip/scripts/easy-options/example.sh @@ -24,11 +24,11 @@ ROOT="$(realpath "$(dirname "$0")")" . "${ROOT}/easyoptions.sh" || exit # Bash implementation, slower # Boolean and parameter options -[[ -n "$some_option" ]] && echo "Option specified: --some-option" +[[ -n "$some_option" ]] && echo "Option specified: --some-option" [[ -n "$some_boolean" ]] && echo "Option specified: --some-boolean" -[[ -n "$some_value" ]] && echo "Option specified: --some-value is $some_value" +[[ -n "$some_value" ]] && echo "Option specified: --some-value is $some_value" # Arguments for argument in "${arguments[@]}"; do - echo "Argument specified: $argument" + echo "Argument specified: $argument" done