From d7b153404e60cfbb99551ab15990546871d67af2 Mon Sep 17 00:00:00 2001 From: Emeric Verschuur Date: Wed, 30 May 2018 22:13:40 +0200 Subject: [PATCH] Improve the help display for big size values --- CHANGELOG.md | 3 +++ VERSION | 2 +- bashopts.sh | 61 +++++++++++++++++++++++++++------------------------- test.sh | 2 +- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acd66c5..0c70ecb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 2.0.1 +* Improve the help display for big size values + ## 2.0.0 * Add version and Changelog * Add enum type diff --git a/VERSION b/VERSION index 227cea2..38f77a6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.0 +2.0.1 diff --git a/bashopts.sh b/bashopts.sh index 10d8502..fbcb080 100644 --- a/bashopts.sh +++ b/bashopts.sh @@ -19,7 +19,7 @@ set -e -BASHOPTS_VERSION=2.0.0 +BASHOPTS_VERSION=2.0.1 bashopts_exit_handle() { local err=$? @@ -428,20 +428,21 @@ bashopts_dump_value() { bashopts_diplay_help() { local elts optargs_max_len=8 val ncol line declare -A optargs - ncol=$(tput cols || true) 2> /dev/null + ncol=$(tput cols 2> /dev/null || true) + ncol=${ncol:-160} + local value_max_len=$(( $ncol / 4 )) # compute the good arguments comumn 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 @@ -454,32 +455,33 @@ 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]} $( - 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]//\"/\\\"}\"" - elif [[ -v bashopts_optprop_default[$op] ]]; then - if [[ "${bashopts_optprop_type[$op]}" =~ ^(string|enumeration)$ ]]; then - echo -n ", default:\"${bashopts_optprop_default[$op]//\"/\\\"}\"" - else - echo -n ", default:${bashopts_optprop_default[$op]}" - fi + 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 + 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 + printf ", default: \"%.${value_max_len}s\"" "$(tr -d '\n' <<< "${bashopts_optprop_default[$op]//\"/\\\"}")" else - elts=")" + printf ", default: %.${value_max_len}s" "$(tr -d '\n' <<< "${bashopts_optprop_default[$op]}")" fi - if [ "${bashopts_optprop_type[$op]}" == "enumeration" ]; then - echo -n ", accepted values:$( - while read -r line; do - echo -n " '${line##*|}'" - done <<< "${bashopts_optprop_enum_values[$op]}" - )" - fi - echo -n ")" + else + elts=")" + fi + if [ "${bashopts_optprop_type[$op]}" == "enumeration" ]; then + echo -n ", accepted values:$( + while read -r line; do + echo -n " '${line##*|}'" + done <<< "${bashopts_optprop_enum_values[$op]}" + )" fi - )" + echo ")" + else + echo "" + fi done test "$1" != "-e" || exit $2 } @@ -492,13 +494,14 @@ bashopts_diplay_help_delayed() { # display all otions values and properties bashopts_diplay_summary() { local elts desc_max_len=0 val dval + 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 } diff --git a/test.sh b/test.sh index e9ac7b7..b02be4b 100755 --- a/test.sh +++ b/test.sh @@ -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_diplay_help | grep -E -o "^\\s+\-\-value \[\"value1\", \"value 2\", \"value '\\\\\"' \\\$3\"]\s+")" " --value [\"value1\", \"value 2\", \"value '\\\"' \$3\"] " } _test_case_5() { -- GitLab