Commit 20ed3866 authored by Emeric Verschuur's avatar Emeric Verschuur
Browse files

Add BASH version check, var name uppercase and add bashopts_diplay_help_delayed

parent 694dcdaf
Loading
Loading
Loading
Loading
Loading
+25 −14
Original line number Diff line number Diff line
@@ -50,6 +50,10 @@ bashopts_critical() {
    exit 1
}

if [ ! "${BASH_VERSINFO[0]}" -ge 4 ]; then
    bashopts_critical "bashopts require BASH version 4 or greater"
fi

# display a error (non fatal)
bashopts_error() {
    >&2 printf "[ERROR] %s\n" "$@"
@@ -185,14 +189,14 @@ bashopts_setup() {
    fi
    bashopts_tool_usage=${bashopts_tool_usage:-"$bashopts_tool_name [options and commands] [-- [extra args]]"}
    # add the default options
    bashopts_declare -n __bashopts_display_help__ -l help -o h -d "Display this help"
    bashopts_declare -n __BASHOPTS_DISPLAY_HELP__ -l help -o h -d "Display this help"
    if [ "$disable_interactive" == "true" ]; then
        bashopts_interactive="false"
        BASHOPTS_INTERACTIVE="false"
    else
        if [ "$non_interactive" == "true" ]; then
            bashopts_declare -n bashopts_interactive -l interactive -o i -d "Interactive mode"
            bashopts_declare -n BASHOPTS_INTERACTIVE -l interactive -o i -d "Interactive mode"
        else
            bashopts_declare -n bashopts_non_interactive -l non-interactive -o n -d "Non interactive mode"
            bashopts_declare -n BASHOPTS_NON_INTERACTIVE -l non-interactive -o n -d "Non interactive mode"
        fi
    fi
}
@@ -412,6 +416,11 @@ bashopts_diplay_help() {
    test "$1" != "-e" || exit $2
}

# Enable help display on option process
bashopts_diplay_help_delayed() {
    __BASHOPTS_DISPLAY_HELP__="true"
}

# display all otions values and properties
bashopts_diplay_summary() {
    local elts desc_max_len=0 val dval
@@ -529,6 +538,7 @@ bashopts_dump_array() {
    echo -n "]"
}

# Process a specified option
bashopts_process_option() {
    local dval tval ival op arg arglist check val_req edit_req
    if ! arglist=$(getopt -o "n:k:r" -n "bashopts_process_option " -- "$@"); then
@@ -583,7 +593,7 @@ bashopts_process_option() {
        # Check current value(s)
        for (( i=0; i<${#tval[@]}; i++)); do
            if ! $check "${tval[$i]}" > /dev/null; then
                if [ "$bashopts_interactive" != "true" ]; then
                if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then
                    bashopts_critical "Non interactive mode: Exit due to one or more error"
                fi
                # (re)enable edition
@@ -593,7 +603,7 @@ bashopts_process_option() {
        done
    elif [ "$val_req" == "true" ]; then
        bashopts_error "At least one value required"
        if [ "$bashopts_interactive" != "true" ]; then
        if [ "$BASHOPTS_INTERACTIVE" != "true" ]; then
            bashopts_critical "Non interactive mode: Exit due to one or more error"
        fi
        # (re)enable edition
@@ -605,7 +615,7 @@ bashopts_process_option() {
            eval "tval=$(bashopts_get_def dval)"
        fi
        if [ "$edit_req" == "true" ]; then
            if [ "$bashopts_interactive" == "true" ]; then
            if [ "$BASHOPTS_INTERACTIVE" == "true" ]; then
                # interactive edition
                while true; do
                    echo "* ${bashopts_optprop_description[$op]}"
@@ -677,11 +687,11 @@ bashopts_process_option() {
            bashopts_warning "No settings file specified"
        fi
    fi
    if [ "$op" == "bashopts_non_interactive" ] && ! [[ -v bashopts_interactive ]]; then
        if [ "$bashopts_non_interactive" == "true" ]; then
            bashopts_interactive="false"
    if [ "$op" == "BASHOPTS_NON_INTERACTIVE" ] && ! [[ -v BASHOPTS_INTERACTIVE ]]; then
        if [ "$BASHOPTS_NON_INTERACTIVE" == "true" ]; then
            BASHOPTS_INTERACTIVE="false"
        else
            bashopts_interactive="true"
            BASHOPTS_INTERACTIVE="true"
        fi
    fi
}
@@ -689,18 +699,19 @@ bashopts_process_option() {
# STEP 4: process arg
bashopts_process_opts() {
    local op
    if [ "$__bashopts_display_help__" == "true" ]; then
        bashopts_interactive="false"
    if [ "$__BASHOPTS_DISPLAY_HELP__" == "true" ]; then
        BASHOPTS_INTERACTIVE="false"
    fi
    for op in "${bashopts_optlist[@]}"; do
        bashopts_process_option -n $op
    done
    if [ "$__bashopts_display_help__" == "true" ]; then
    if [ "$__BASHOPTS_DISPLAY_HELP__" == "true" ]; then
        bashopts_diplay_help
        exit 0
    fi
}

# Export all option variables
bashopts_export_opts() {
    for op in "${bashopts_optlist[@]}"; do
        if [[ -v $op ]]; then