Commit 0733fdfe authored by Emeric Verschuur's avatar Emeric Verschuur

add bashopts_diplay_summary

parent 34bf38df
......@@ -85,6 +85,12 @@ bashopts_process_args usage:
```bash
bashopts_process_args
```
### Option exploitation
- All the options are stored on bash variable (e.g.: option name first_name on the following example give $first_name bash variable)
- All other command (other than argument beginning by '-' or '--') are stored into the ```bashopts_commands``` bash array. You can iterate by ```for command in ${bashopts_commands[@]}; do ...; done```
- All extra arguments (```mytool --arg1 --arg2 cmd1 cmd2 -- --extra-arg1 --extra-arg2 ...```) are stored into the ```bashopts_extra_args``` bash array.
## Example
```bash
......
......@@ -100,6 +100,7 @@ done
declare -x -A bashopts_opt2name
bashopts_optlist=()
bashopts_commands=()
bashopts_extra_args=()
bashopts_tool_name=$0
bashopts_tool_settings_path="/dev/null"
......@@ -287,6 +288,20 @@ bashopts_diplay_help() {
fi
printf " %-${optargs_max_len}s %s\n" ${optargs[$op]} "${bashopts_optprop_description[$op]} $elts"
done
test "$1" != "-e" || exit $2
}
bashopts_diplay_summary() {
local elts desc_max_len=0 val dval
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 : ${!op}\n" "${bashopts_optprop_description[$op]}"
fi
done
}
# STEP 3: parse arg
......@@ -394,13 +409,13 @@ bashopts_process_args() {
tval=$ival
fi
fi
eval "$op=$(declare -p tval | sed -E 's/^declare\s[^=]*=//g')"
eval "$op=$(declare -p tval | sed -E "s/^declare\\s[^=]*=('(.*)'|(.*))\$/\\2\\3/g")"
fi
if [ "${bashopts_optprop_setting[$op]}" == "true" ]; then
# vrite the value to the setting file
sed -i "/^$op=/d" $bashopts_tool_settings_path
if [ "${!op}" != "$dval" ]; then
declare -p $op | sed -E "s/^declare\\s-[-afFrxip ]*\\s([^=]+=)('(.*)'|(.*))\$/\\1\\3\\4/g" >> $bashopts_tool_settings_path
echo "$op=$(declare -p $op | sed -E "s/^declare\\s[^=]*=('(.*)'|(.*))\$/\\2\\3/g")" >> $bashopts_tool_settings_path
fi
fi
done
......@@ -408,4 +423,5 @@ bashopts_process_args() {
bashopts_diplay_help
exit 0
fi
bashopts_commands=("$@")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment