Use option names for positional arguments instead of ARG[1-4]

This commit is contained in:
oblique 2015-05-17 19:10:55 +03:00
parent ce8d4c9ee5
commit df727ff5fd

View File

@ -584,7 +584,8 @@ NO_HAVEGED=0
CONFIG_OPTS=(CHANNEL GATEWAY WPA_VERSION ETC_HOSTS HIDDEN SHARE_METHOD CONFIG_OPTS=(CHANNEL GATEWAY WPA_VERSION ETC_HOSTS HIDDEN SHARE_METHOD
IEEE80211N HT_CAPAB DRIVER NO_VIRT COUNTRY FREQ_BAND IEEE80211N HT_CAPAB DRIVER NO_VIRT COUNTRY FREQ_BAND
NEW_MACADDR DAEMONIZE NO_HAVEGED) NEW_MACADDR DAEMONIZE NO_HAVEGED WIFI_IFACE INTERNET_IFACE
SSID PASSPHRASE)
FIX_UNMANAGED=0 FIX_UNMANAGED=0
LIST_RUNNING=0 LIST_RUNNING=0
@ -593,8 +594,6 @@ STOP_ID=
STORE_CONFIG= STORE_CONFIG=
LOAD_CONFIG= LOAD_CONFIG=
declare -A LOADED_ARGS
CONFDIR= CONFDIR=
WIFI_IFACE= WIFI_IFACE=
VWIFI_IFACE= VWIFI_IFACE=
@ -827,17 +826,21 @@ write_config() {
exit 1 exit 1
fi fi
WIFI_IFACE=$1
if [[ "$SHARE_METHOD" == "none" ]]; then
SSID=$2
PASSPHRASE=$3
else
INTERNET_IFACE=$2
SSID=$3
PASSPHRASE=$4
fi
for config_opt in "${CONFIG_OPTS[@]}"; do for config_opt in "${CONFIG_OPTS[@]}"; do
eval echo $config_opt=\$$config_opt eval echo $config_opt=\$$config_opt
done >> "$STORE_CONFIG" done >> "$STORE_CONFIG"
while [[ $# -ne 0 ]]; do echo -e "Config options written to '$STORE_CONFIG'"
echo "ARG$i=$1"
shift
((i++))
done >> "$STORE_CONFIG"
echo -e "\nConfigs written to $STORE_CONFIG"
exit 0 exit 0
} }
@ -854,29 +857,18 @@ is_config_opt() {
# Load options from config file # Load options from config file
read_config() { read_config() {
local opt_name opt_val local opt_name opt_val line
local pos_max=0 pos_num=0 pos_idx
while read line; do while read line; do
# Read switches and their values # Read switches and their values
opt_name="${line%=*}" opt_name="${line%%=*}"
opt_val="${line#*=}" opt_val="${line#*=}"
if is_config_opt "$opt_name" ; then if is_config_opt "$opt_name" ; then
eval $opt_name="\$opt_val" eval $opt_name="\$opt_val"
elif [[ "$opt_name" =~ ^ARG([1-9][0-9]*)$ ]]; then
pos_idx="${BASH_REMATCH[1]}"
((pos_num++))
[[ $pos_idx > $pos_max ]] && pos_max=$pos_idx
LOADED_ARGS[$pos_idx]="$opt_val"
else else
echo "WARN: Unrecognized configuration entry $opt_name" >&2 echo "WARN: Unrecognized configuration entry $opt_name" >&2
fi fi
done < "$LOAD_CONFIG" done < "$LOAD_CONFIG"
if [[ $pos_num -ne $pos_max ]]; then
echo "ERROR: Positional arguments cannot be skipped" >&2
exit 1
fi
} }
@ -1014,10 +1006,15 @@ done
# Load positional args from config file, if needed # Load positional args from config file, if needed
if [[ -n "$LOAD_CONFIG" && $# -eq 0 ]]; then if [[ -n "$LOAD_CONFIG" && $# -eq 0 ]]; then
for ((i=$# + 1; i<=${#LOADED_ARGS[@]}; i++)); do i=0
((j=i-1)) # set arguments in order
((k=i+1)) for x in WIFI_IFACE INTERNET_IFACE SSID PASSPHRASE; do
set -- "${@:1:$j}" "${LOADED_ARGS[$i]}" "${@:$k}" if eval "[[ -n \"\$${x}\" ]]"; then
eval "set -- \"\${@:1:$i}\" \"\$${x}\""
((i++))
fi
# we unset the variable to avoid any problems later
eval "unset $x"
done done
fi fi