Loading CHANGELOG.md +3 −0 Original line number Diff line number Diff line # Changelog ## 2.1.0 * Improve the help display for big size values ## 2.0.0 * Add version and Changelog * Add enum type Loading VERSION +1 −1 Original line number Diff line number Diff line 2.0.0 2.2.0 bashopts.sh +47 −36 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ set -e BASHOPTS_VERSION=2.0.0 BASHOPTS_VERSION=2.1.0 bashopts_exit_handle() { local err=$? Loading Loading @@ -425,23 +425,27 @@ bashopts_dump_value() { } # display the formated help bashopts_diplay_help() { bashopts_display_help() { local elts optargs_max_len=8 val ncol line declare -A optargs ncol=$(tput cols || true) 2> /dev/null # compute the good arguments comumn size if tput cols &> /dev/null; then ncol=$(tput cols) else ncol=${COLUMNS:-160} fi local value_max_len=$(( $ncol / 4 )) # compute the good arguments column size for op in "${bashopts_optlist[@]}"; do elts=() unset val if ! [[ $op =~ ^__.*__$ ]] && [[ -v $op ]]; then val=" $(bashopts_dump_value $op)" val=" $(bashopts_dump_value $op | tr -d '\n')" fi if [[ -v bashopts_optprop_short_opt[$op] ]]; then elts+=("-${bashopts_optprop_short_opt[$op]}"); fi if [[ -v bashopts_optprop_long_opt[$op] ]]; then elts+=("--${bashopts_optprop_long_opt[$op]}"); fi optargs[$op]="$(bashopts_join_by , ${elts[@]})$val" optargs[$op]="$(bashopts_join_by , ${elts[@]})${val:0:${value_max_len}}" optargs_max_len=$(bashopts_math_max $optargs_max_len ${#optargs[$op]}) done ncol=${ncol:-160} optargs_max_len=$(bashopts_math_min $optargs_max_len $(( $ncol / 3 )) ) # display global info echo Loading @@ -454,18 +458,18 @@ bashopts_diplay_help() { echo "OPTIONS:" for op in "${bashopts_optlist[@]}"; do # display arguments, value if available, description, and additional info if available printf " %-${optargs_max_len}s %s\n" "${optargs[$op]}" "${bashopts_optprop_description[$op]} $( printf " %-${optargs_max_len}s ${bashopts_optprop_description[$op]}" "${optargs[$op]}" if ! [[ $op =~ ^__.*__$ ]]; then # display additional information the each properties # discarding special options like --help echo -n " - [\$$op] (type:${bashopts_optprop_type[$op]}" if [[ -v bashopts_optprop_expression[$op] ]]; then echo -n ", default:\"${bashopts_optprop_expression[$op]//\"/\\\"}\"" printf ", default: \"%.${value_max_len}s\"" "$(tr -d '\n' <<< "${bashopts_optprop_expression[$op]//\"/\\\"}")" elif [[ -v bashopts_optprop_default[$op] ]]; then if [[ "${bashopts_optprop_type[$op]}" =~ ^(string|enumeration)$ ]]; then echo -n ", default:\"${bashopts_optprop_default[$op]//\"/\\\"}\"" printf ", default: \"%.${value_max_len}s\"" "$(tr -d '\n' <<< "${bashopts_optprop_default[$op]//\"/\\\"}")" else echo -n ", default:${bashopts_optprop_default[$op]}" printf ", default: %.${value_max_len}s" "$(tr -d '\n' <<< "${bashopts_optprop_default[$op]}")" fi else elts=")" Loading @@ -477,28 +481,35 @@ bashopts_diplay_help() { done <<< "${bashopts_optprop_enum_values[$op]}" )" fi echo -n ")" echo ")" else echo "" fi )" done test "$1" != "-e" || exit $2 } # Enable help display on option process bashopts_diplay_help_delayed() { bashopts_display_help_delayed() { __BASHOPTS_DISPLAY_HELP__="true" } # display all otions values and properties bashopts_diplay_summary() { local elts desc_max_len=0 val dval bashopts_display_summary() { local elts desc_max_len=0 val dval ncol if tput cols &> /dev/null; then ncol=$(tput cols) else ncol=${COLUMNS:-160} fi local value_max_len=$(( $ncol / 4 )) declare -A optargs for op in "${bashopts_optlist[@]}"; do desc_max_len=$(bashopts_math_max $desc_max_len ${#bashopts_optprop_description[$op]}) done for op in "${bashopts_optlist[@]}"; do if ! [[ $op =~ ^__.*__$ ]]; then printf "* %-${desc_max_len}s : $(bashopts_dump_value $op)\n" "${bashopts_optprop_description[$op]}" printf "* %-${desc_max_len}.${value_max_len}s : $(bashopts_dump_value $op | tr -d '\n')\n" "${bashopts_optprop_description[$op]}" fi done } Loading Loading @@ -534,7 +545,7 @@ bashopts_parse_args() { # call the getopt if ! args=$(getopt -o $short_opts -l "$long_opts" -n "$bashopts_tool_name" -- "${args[@]}"); then >&2 bashopts_diplay_help >&2 bashopts_display_help exit 1 fi eval set -- "$args"; Loading Loading @@ -810,7 +821,7 @@ bashopts_process_opts() { bashopts_process_option -n $op done if [ "$__BASHOPTS_DISPLAY_HELP__" == "true" ]; then bashopts_diplay_help bashopts_display_help exit 0 fi } Loading test.sh +1 −1 Original line number Diff line number Diff line Loading @@ -89,7 +89,7 @@ _test_case_4() { req_test_eq "${array_value[1]}" "value 2" req_test_eq "${array_value[2]}" "value '\"' \$3" req_test_eq "$(cat /tmp/bashopts_testrc)" "array_value=([0]=\"value1\" [1]=\"value 2\" [2]=\"value '\\\"' \\\$3\")" req_test_eq "$(bashopts_diplay_help | grep -E -o "^\\s+\-\-value \[\"value1\", \"value 2\", \"value '\\\\\"' \\\$3\"]\s+")" " --value [\"value1\", \"value 2\", \"value '\\\"' \$3\"] " req_test_eq "$(bashopts_display_help | grep -E -o "^\\s+\-\-value \[\"value1\", \"value 2\", \"value '\\\\\"' \\\$3\"]\s+")" " --value [\"value1\", \"value 2\", \"value '\\\"' \$3\"] " } _test_case_5() { Loading Loading
CHANGELOG.md +3 −0 Original line number Diff line number Diff line # Changelog ## 2.1.0 * Improve the help display for big size values ## 2.0.0 * Add version and Changelog * Add enum type Loading
bashopts.sh +47 −36 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ set -e BASHOPTS_VERSION=2.0.0 BASHOPTS_VERSION=2.1.0 bashopts_exit_handle() { local err=$? Loading Loading @@ -425,23 +425,27 @@ bashopts_dump_value() { } # display the formated help bashopts_diplay_help() { bashopts_display_help() { local elts optargs_max_len=8 val ncol line declare -A optargs ncol=$(tput cols || true) 2> /dev/null # compute the good arguments comumn size if tput cols &> /dev/null; then ncol=$(tput cols) else ncol=${COLUMNS:-160} fi local value_max_len=$(( $ncol / 4 )) # compute the good arguments column size for op in "${bashopts_optlist[@]}"; do elts=() unset val if ! [[ $op =~ ^__.*__$ ]] && [[ -v $op ]]; then val=" $(bashopts_dump_value $op)" val=" $(bashopts_dump_value $op | tr -d '\n')" fi if [[ -v bashopts_optprop_short_opt[$op] ]]; then elts+=("-${bashopts_optprop_short_opt[$op]}"); fi if [[ -v bashopts_optprop_long_opt[$op] ]]; then elts+=("--${bashopts_optprop_long_opt[$op]}"); fi optargs[$op]="$(bashopts_join_by , ${elts[@]})$val" optargs[$op]="$(bashopts_join_by , ${elts[@]})${val:0:${value_max_len}}" optargs_max_len=$(bashopts_math_max $optargs_max_len ${#optargs[$op]}) done ncol=${ncol:-160} optargs_max_len=$(bashopts_math_min $optargs_max_len $(( $ncol / 3 )) ) # display global info echo Loading @@ -454,18 +458,18 @@ bashopts_diplay_help() { echo "OPTIONS:" for op in "${bashopts_optlist[@]}"; do # display arguments, value if available, description, and additional info if available printf " %-${optargs_max_len}s %s\n" "${optargs[$op]}" "${bashopts_optprop_description[$op]} $( printf " %-${optargs_max_len}s ${bashopts_optprop_description[$op]}" "${optargs[$op]}" if ! [[ $op =~ ^__.*__$ ]]; then # display additional information the each properties # discarding special options like --help echo -n " - [\$$op] (type:${bashopts_optprop_type[$op]}" if [[ -v bashopts_optprop_expression[$op] ]]; then echo -n ", default:\"${bashopts_optprop_expression[$op]//\"/\\\"}\"" printf ", default: \"%.${value_max_len}s\"" "$(tr -d '\n' <<< "${bashopts_optprop_expression[$op]//\"/\\\"}")" elif [[ -v bashopts_optprop_default[$op] ]]; then if [[ "${bashopts_optprop_type[$op]}" =~ ^(string|enumeration)$ ]]; then echo -n ", default:\"${bashopts_optprop_default[$op]//\"/\\\"}\"" printf ", default: \"%.${value_max_len}s\"" "$(tr -d '\n' <<< "${bashopts_optprop_default[$op]//\"/\\\"}")" else echo -n ", default:${bashopts_optprop_default[$op]}" printf ", default: %.${value_max_len}s" "$(tr -d '\n' <<< "${bashopts_optprop_default[$op]}")" fi else elts=")" Loading @@ -477,28 +481,35 @@ bashopts_diplay_help() { done <<< "${bashopts_optprop_enum_values[$op]}" )" fi echo -n ")" echo ")" else echo "" fi )" done test "$1" != "-e" || exit $2 } # Enable help display on option process bashopts_diplay_help_delayed() { bashopts_display_help_delayed() { __BASHOPTS_DISPLAY_HELP__="true" } # display all otions values and properties bashopts_diplay_summary() { local elts desc_max_len=0 val dval bashopts_display_summary() { local elts desc_max_len=0 val dval ncol if tput cols &> /dev/null; then ncol=$(tput cols) else ncol=${COLUMNS:-160} fi local value_max_len=$(( $ncol / 4 )) declare -A optargs for op in "${bashopts_optlist[@]}"; do desc_max_len=$(bashopts_math_max $desc_max_len ${#bashopts_optprop_description[$op]}) done for op in "${bashopts_optlist[@]}"; do if ! [[ $op =~ ^__.*__$ ]]; then printf "* %-${desc_max_len}s : $(bashopts_dump_value $op)\n" "${bashopts_optprop_description[$op]}" printf "* %-${desc_max_len}.${value_max_len}s : $(bashopts_dump_value $op | tr -d '\n')\n" "${bashopts_optprop_description[$op]}" fi done } Loading Loading @@ -534,7 +545,7 @@ bashopts_parse_args() { # call the getopt if ! args=$(getopt -o $short_opts -l "$long_opts" -n "$bashopts_tool_name" -- "${args[@]}"); then >&2 bashopts_diplay_help >&2 bashopts_display_help exit 1 fi eval set -- "$args"; Loading Loading @@ -810,7 +821,7 @@ bashopts_process_opts() { bashopts_process_option -n $op done if [ "$__BASHOPTS_DISPLAY_HELP__" == "true" ]; then bashopts_diplay_help bashopts_display_help exit 0 fi } Loading
test.sh +1 −1 Original line number Diff line number Diff line Loading @@ -89,7 +89,7 @@ _test_case_4() { req_test_eq "${array_value[1]}" "value 2" req_test_eq "${array_value[2]}" "value '\"' \$3" req_test_eq "$(cat /tmp/bashopts_testrc)" "array_value=([0]=\"value1\" [1]=\"value 2\" [2]=\"value '\\\"' \\\$3\")" req_test_eq "$(bashopts_diplay_help | grep -E -o "^\\s+\-\-value \[\"value1\", \"value 2\", \"value '\\\\\"' \\\$3\"]\s+")" " --value [\"value1\", \"value 2\", \"value '\\\"' \$3\"] " req_test_eq "$(bashopts_display_help | grep -E -o "^\\s+\-\-value \[\"value1\", \"value 2\", \"value '\\\\\"' \\\$3\"]\s+")" " --value [\"value1\", \"value 2\", \"value '\\\"' \$3\"] " } _test_case_5() { Loading