Commit 062518cc authored by Emeric Verschuur's avatar Emeric Verschuur Committed by Emeric Verschuur
Browse files

Improve list property input

parent e380b399
Loading
Loading
Loading
Loading
+26 −4
Changes for bashopts.sh: 26 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -543,6 +543,15 @@ bashopts_dump_array() {
    echo -n "]"
}

bashopts_read_json_array() {
    local line
    while read -r line; do
        eval "$1+=($line)"
    done <<< "$(jq '.[]' <<< "$2")" && return 0 || \
    bashopts_log E "Invalid JSON array"
    return 1
}

# Process a specified option
bashopts_process_option() {
    local dval tval ival op arg arglist check val_req edit_req
@@ -624,16 +633,29 @@ bashopts_process_option() {
                # interactive edition
                while true; do
                    echo "* ${bashopts_optprop_description[$op]}"
                    if [ "${bashopts_optprop_method[$op]}" == "add" ]; then
                        echo " -> List property format: 'single val.' or BASH array '(v1 v2 v3)' or JSON array '[v1, v2, v3]'"
                    fi
                    echo -n "    $(bashopts_dump_array {bashopts_optprop_type[$op]} "${tval[@]}"): "
                    read ival || return 1
                    read ival || bashopts_log C "Unexpected error, aborting..."
                    if [ -n "$ival" ]; then
                        if [ "${bashopts_optprop_method[$op]}" == "add" ]; then
                            # array value
                            tval=()
                            case "${ival:0:1}" in
                                '[')
                                    bashopts_read_json_array tval "$ival" || continue
                                    ;;
                                '(')
                                    if ! eval "tval=$ival" 2>/dev/null; then
                                bashopts_log C "'$ival' must be written in BASH array format: '( \"val 1\" \"val2\" \"val3...\" )'"
                                unset tval
                                        bashopts_log E "Invalid BASH array"
                                        continue
                                    fi
                                    ;;
                                *)
                                    tval+=("$ival")
                                    ;;
                            esac
                        else
                            # non array/normal value
                            tval=$ival
@@ -659,7 +681,7 @@ bashopts_process_option() {
                            unset tval
                        fi
                    fi
                    if [[ -v tval ]]; then
                    if declare -p tval > /dev/null 2>&1; then
                        # edit OK, break
                        break
                    fi
+1 −1
Changes for example.sh: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ bashopts_declare -n first_name -l first -o f -d "First name" -t string -i -s -r
bashopts_declare -n last_name -l last -o l -d "Last name" -t string -i -s -r
bashopts_declare -n display_name -l display-name -t string -d "Display name" -e "\"\$first_name \$last_name\""
bashopts_declare -n age -l number -d "Age" -t number
bashopts_declare -n email_list -t string -m add -l email -d "Email adress"
bashopts_declare -n email_list -t string -m add -l email -d "Email adress" -i

# Parse arguments
bashopts_parse_args "$@"