mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-10 13:11:32 +00:00
Parse option specs, allow negating with --no-, add more tests.
This commit is contained in:
@@ -46,17 +46,12 @@ usage() {
|
||||
local file=$1
|
||||
|
||||
local usage=$(grep '^Usage: ' $file)
|
||||
local opts=$(grep -A 2 '^=item --' $file | sed -e 's/^=item //' -e 's/^\([A-Z]\)/ \1/' -e 's/^--$//' > $TMPDIR/help)
|
||||
|
||||
if [ "$OPT_ERR" ]; then
|
||||
echo "Error: ${OPT_ERR}" >&2
|
||||
fi
|
||||
echo $usage >&2
|
||||
echo >&2
|
||||
echo "Options:" >&2
|
||||
echo >&2
|
||||
cat $TMPDIR/help >&2
|
||||
echo >&2
|
||||
echo "For more information, 'man $TOOL' or 'perldoc $file'." >&2
|
||||
}
|
||||
|
||||
@@ -77,57 +72,100 @@ parse_options() {
|
||||
local file=$1
|
||||
shift
|
||||
|
||||
local opt=""
|
||||
local val=""
|
||||
local default=""
|
||||
local version=""
|
||||
local i=0
|
||||
|
||||
awk '
|
||||
# Parse the program options (po) from the POD. Each option has
|
||||
# a spec file like:
|
||||
# $ cat po/string-opt2
|
||||
# long=string-opt2
|
||||
# type=string
|
||||
# default=foo
|
||||
# That's the spec for --string-opt2. Each line is a key:value pair
|
||||
# from the option's POD line like "type: string; default: foo".
|
||||
mkdir $TMPDIR/po/ 2>/dev/null
|
||||
rm -rf $TMPDIR/po/*
|
||||
awk -v "po_dir"="$TMPDIR/po" '
|
||||
/^=head1 OPTIONS/ {
|
||||
getline
|
||||
while ($0 !~ /^=head1/) {
|
||||
if ($0 ~ /^=item --.*/) {
|
||||
long_opt=substr($2, 3, length($2) - 2)
|
||||
short_opt=""
|
||||
required_arg=""
|
||||
|
||||
if ($3) {
|
||||
if ($3 ~ /-[a-z]/)
|
||||
short_opt=substr($3, 3, length($3) - 3)
|
||||
else
|
||||
required_arg=$3
|
||||
}
|
||||
|
||||
if ($4 ~ /[A-Z]/)
|
||||
required_arg=$4
|
||||
long_opt = substr($2, 3, length($2) - 2)
|
||||
spec_file = po_dir "/" long_opt
|
||||
trf = "sed -e \"s/[ ]//g\" | tr \";\" \"\n\" > " spec_file
|
||||
|
||||
getline # blank line
|
||||
getline # short description line
|
||||
getline # specs or description
|
||||
|
||||
if ($0 ~ /default: /) {
|
||||
i=index($0, "default: ")
|
||||
default=substr($0, i + 9, length($0) - (i + 9))
|
||||
if ($0 ~ /^[a-z]/ ) {
|
||||
# spec line like "type: int; default: 100"
|
||||
print "long:" long_opt "; " $0 | trf
|
||||
}
|
||||
else {
|
||||
# no specs, should be description of option
|
||||
print "long:" long_opt > spec_file
|
||||
}
|
||||
else
|
||||
default=""
|
||||
|
||||
print long_opt "," short_opt "," required_arg "," default
|
||||
}
|
||||
getline
|
||||
}
|
||||
exit
|
||||
}' $file > $TMPDIR/options
|
||||
}' $file
|
||||
|
||||
while read spec; do
|
||||
opt=$(echo $spec | cut -d',' -f1 | sed 's/-/_/g' | tr [:lower:] [:upper:])
|
||||
default=$(echo $spec | cut -d',' -f4)
|
||||
eval "OPT_${opt}"="$default"
|
||||
done < <(cat $TMPDIR/options)
|
||||
# Evaluate the program options into existence as global variables
|
||||
# transformed like --my-op == $OPT_MY_OP. If an option has a default
|
||||
# value, it's assigned that value. Else, it's value is an empty string.
|
||||
for opt_spec in $(ls $TMPDIR/po/); do
|
||||
local opt=""
|
||||
local default_val=""
|
||||
local neg=0
|
||||
while read line; do
|
||||
local key=`echo $line | cut -d ':' -f 1`
|
||||
local val=`echo $line | cut -d ':' -f 2`
|
||||
case "$key" in
|
||||
long)
|
||||
opt=$(echo $val | sed 's/-/_/g' | tr [:lower:] [:upper:])
|
||||
;;
|
||||
default)
|
||||
default_val="$val"
|
||||
;;
|
||||
shortform)
|
||||
;;
|
||||
type)
|
||||
;;
|
||||
negatable)
|
||||
if [ "$val" = "yes" ]; then
|
||||
neg=1
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
die "Invalid attribute in $TMPDIR/po/$opt_spec: $line"
|
||||
esac
|
||||
done < $TMPDIR/po/$opt_spec
|
||||
|
||||
if [ -z "$opt" ]; then
|
||||
die "No long attribute in option spec $TMPDIR/po/$opt_spec"
|
||||
fi
|
||||
|
||||
if [ $neg -eq 1 ]; then
|
||||
if [ -z "$default_val" ] || [ "$default_val" != "yes" ]; then
|
||||
die "Option $opt_spec is negatable but not default: yes"
|
||||
fi
|
||||
fi
|
||||
|
||||
eval "OPT_${opt}"="$default_val"
|
||||
done
|
||||
|
||||
# Parse the command line options. Anything after -- is put into
|
||||
# EXT_ARGV. Options must begin with one or two hyphens (--help or -h),
|
||||
# else the item is put into ARGV (it's probably a filename, directory,
|
||||
# etc.) The program option specs parsed above are used to valid the
|
||||
# command line options. All options have already been eval'd into
|
||||
# existence, but we re-eval opts specified on the command line to update
|
||||
# the corresponding global variable's value. For example, if --foo has
|
||||
# a default value 100, then $OPT_FOO=100 already, but if --foo=500 is
|
||||
# specified on the command line, then we re-eval $OPT_FOO=500 to update
|
||||
# $OPT_FOO.
|
||||
local i=0 # ARGV index
|
||||
for opt; do
|
||||
if [ $# -eq 0 ]; then
|
||||
break
|
||||
break # no more opts
|
||||
fi
|
||||
opt=$1
|
||||
if [ "$opt" = "--" ]; then
|
||||
@@ -144,30 +182,62 @@ parse_options() {
|
||||
usage $file
|
||||
exit 0
|
||||
fi
|
||||
shift
|
||||
shift
|
||||
if [ $(expr "$opt" : "-") -eq 0 ]; then
|
||||
# Option does not begin with a hyphen (-), so treat it as
|
||||
# a filename, directory, etc.
|
||||
ARGV[i]="$opt"
|
||||
i=$((i+1))
|
||||
continue
|
||||
fi
|
||||
opt=$(echo $opt | sed 's/^-*//')
|
||||
spec=$(grep -E "^$opt,|,$opt," "$TMPDIR/options")
|
||||
if [ -z "$spec" ]; then
|
||||
die "Unknown option: $opt"
|
||||
|
||||
# Save real opt from cmd line for error messages.
|
||||
local real_opt="$opt"
|
||||
|
||||
# Strip leading -- or --no- from option.
|
||||
if $(echo $opt | grep -q '^--no-'); then
|
||||
neg=1
|
||||
opt=$(echo $opt | sed 's/^--no-//')
|
||||
else
|
||||
neg=0
|
||||
opt=$(echo $opt | sed 's/^-*//')
|
||||
fi
|
||||
opt=$(echo $spec | cut -d',' -f1)
|
||||
required_arg=$(echo $spec | cut -d',' -f3)
|
||||
val="yes"
|
||||
|
||||
# Find the option's spec file.
|
||||
if [ -f "$TMPDIR/po/$opt" ]; then
|
||||
spec="$TMPDIR/po/$opt"
|
||||
else
|
||||
spec=$(grep "^shortform:-$opt\$" $TMPDIR/po/* | cut -d ':' -f 1)
|
||||
if [ -z "$spec" ]; then
|
||||
die "Unknown option: $opt"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Get the value specified for the option, if any. If the opt's spec
|
||||
# says it has a type, then it requires a value and that value should
|
||||
# be the next item ($1). Else, typeless options (like --version) are
|
||||
# either "yes" if specified, else "no" if negatable and --no-opt.
|
||||
required_arg=$(cat $spec | grep '^type:' | cut -d':' -f2)
|
||||
if [ -n "$required_arg" ]; then
|
||||
if [ $# -eq 0 ]; then
|
||||
die "--$opt requires a $required_arg argument"
|
||||
die "$real_opt requires a $required_arg argument"
|
||||
else
|
||||
val="$1"
|
||||
shift
|
||||
fi
|
||||
else
|
||||
if [ $neg -eq 0 ]; then
|
||||
val="yes"
|
||||
else
|
||||
val="no"
|
||||
fi
|
||||
fi
|
||||
opt=$(echo $opt | sed 's/-/_/g' | tr [:lower:] [:upper:])
|
||||
eval "OPT_${opt}"="$val"
|
||||
|
||||
# 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:])
|
||||
|
||||
# Re-eval the option to update its global variable value.
|
||||
eval "OPT_$opt"="$val"
|
||||
done
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user