Convert size \d+[KMGT] opts in parse_options.sh.

This commit is contained in:
Daniel Nichter
2012-01-26 10:44:55 -07:00
parent 73bc7cdcf8
commit 81caf6addc
3 changed files with 85 additions and 1 deletions

View File

@@ -236,6 +236,7 @@ _eval_po() {
local opt=""
local default_val=""
local neg=0
local size=0
while read key val; do
case "$key" in
long)
@@ -247,6 +248,7 @@ _eval_po() {
"short form")
;;
type)
[ "$val" = "size" ] && size=1
;;
desc)
;;
@@ -273,6 +275,11 @@ _eval_po() {
fi
fi
# Convert sizes.
if [ $size -eq 1 -a -n "$default_val" ]; then
default_val=$(size_to_bytes $default_val)
fi
# Eval the option into existence as a global variable.
eval "OPT_${opt}"="$default_val"
done
@@ -339,6 +346,7 @@ _parse_command_line() {
local opt_is_negated=""
local real_opt=""
local required_arg=""
local spec=""
for opt in "$@"; do
if [ "$opt" = "--" -o "$opt" = "----" ]; then
@@ -438,6 +446,11 @@ _parse_command_line() {
# Get and transform the opt's long form. E.g.: -q == --quiet == QUIET.
opt=$(cat "$spec" | grep '^long:' | cut -d':' -f2 | sed 's/-/_/g' | tr [:lower:] [:upper:])
# Convert sizes.
if grep "^type:size" "$spec" >/dev/null; then
val=$(size_to_bytes $val)
fi
# Re-eval the option to update its global variable value.
eval "OPT_$opt"="'$val'"
@@ -448,10 +461,16 @@ _parse_command_line() {
opt_is_negated=""
real_opt=""
required_arg=""
spec=""
fi
done
}
size_to_bytes() {
local size="$1"
echo $size | perl -ne '%f=(B=>1, K=>1_024, M=>1_048_576, G=>1_073_741_824, T=>1_099_511_627_776); m/^(\d+)([kMGT])?/i; print $1 * $f{uc($2 || "B")};'
}
# ###########################################################################
# End parse_options package
# ###########################################################################