mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-10 21:19:59 +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 file=$1
|
||||||
|
|
||||||
local usage=$(grep '^Usage: ' $file)
|
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
|
if [ "$OPT_ERR" ]; then
|
||||||
echo "Error: ${OPT_ERR}" >&2
|
echo "Error: ${OPT_ERR}" >&2
|
||||||
fi
|
fi
|
||||||
echo $usage >&2
|
echo $usage >&2
|
||||||
echo >&2
|
echo >&2
|
||||||
echo "Options:" >&2
|
|
||||||
echo >&2
|
|
||||||
cat $TMPDIR/help >&2
|
|
||||||
echo >&2
|
|
||||||
echo "For more information, 'man $TOOL' or 'perldoc $file'." >&2
|
echo "For more information, 'man $TOOL' or 'perldoc $file'." >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,57 +72,100 @@ parse_options() {
|
|||||||
local file=$1
|
local file=$1
|
||||||
shift
|
shift
|
||||||
|
|
||||||
local opt=""
|
# Parse the program options (po) from the POD. Each option has
|
||||||
local val=""
|
# a spec file like:
|
||||||
local default=""
|
# $ cat po/string-opt2
|
||||||
local version=""
|
# long=string-opt2
|
||||||
local i=0
|
# type=string
|
||||||
|
# default=foo
|
||||||
awk '
|
# 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/ {
|
/^=head1 OPTIONS/ {
|
||||||
getline
|
getline
|
||||||
while ($0 !~ /^=head1/) {
|
while ($0 !~ /^=head1/) {
|
||||||
if ($0 ~ /^=item --.*/) {
|
if ($0 ~ /^=item --.*/) {
|
||||||
long_opt=substr($2, 3, length($2) - 2)
|
long_opt = substr($2, 3, length($2) - 2)
|
||||||
short_opt=""
|
spec_file = po_dir "/" long_opt
|
||||||
required_arg=""
|
trf = "sed -e \"s/[ ]//g\" | tr \";\" \"\n\" > " spec_file
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
getline # blank line
|
getline # blank line
|
||||||
getline # short description line
|
getline # specs or description
|
||||||
|
|
||||||
if ($0 ~ /default: /) {
|
if ($0 ~ /^[a-z]/ ) {
|
||||||
i=index($0, "default: ")
|
# spec line like "type: int; default: 100"
|
||||||
default=substr($0, i + 9, length($0) - (i + 9))
|
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
|
getline
|
||||||
}
|
}
|
||||||
exit
|
exit
|
||||||
}' $file > $TMPDIR/options
|
}' $file
|
||||||
|
|
||||||
while read spec; do
|
# Evaluate the program options into existence as global variables
|
||||||
opt=$(echo $spec | cut -d',' -f1 | sed 's/-/_/g' | tr [:lower:] [:upper:])
|
# transformed like --my-op == $OPT_MY_OP. If an option has a default
|
||||||
default=$(echo $spec | cut -d',' -f4)
|
# value, it's assigned that value. Else, it's value is an empty string.
|
||||||
eval "OPT_${opt}"="$default"
|
for opt_spec in $(ls $TMPDIR/po/); do
|
||||||
done < <(cat $TMPDIR/options)
|
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
|
for opt; do
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
break
|
break # no more opts
|
||||||
fi
|
fi
|
||||||
opt=$1
|
opt=$1
|
||||||
if [ "$opt" = "--" ]; then
|
if [ "$opt" = "--" ]; then
|
||||||
@@ -144,30 +182,62 @@ parse_options() {
|
|||||||
usage $file
|
usage $file
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
shift
|
shift
|
||||||
if [ $(expr "$opt" : "-") -eq 0 ]; then
|
if [ $(expr "$opt" : "-") -eq 0 ]; then
|
||||||
|
# Option does not begin with a hyphen (-), so treat it as
|
||||||
|
# a filename, directory, etc.
|
||||||
ARGV[i]="$opt"
|
ARGV[i]="$opt"
|
||||||
i=$((i+1))
|
i=$((i+1))
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
opt=$(echo $opt | sed 's/^-*//')
|
|
||||||
spec=$(grep -E "^$opt,|,$opt," "$TMPDIR/options")
|
# Save real opt from cmd line for error messages.
|
||||||
if [ -z "$spec" ]; then
|
local real_opt="$opt"
|
||||||
die "Unknown option: $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
|
fi
|
||||||
opt=$(echo $spec | cut -d',' -f1)
|
|
||||||
required_arg=$(echo $spec | cut -d',' -f3)
|
# Find the option's spec file.
|
||||||
val="yes"
|
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 [ -n "$required_arg" ]; then
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
die "--$opt requires a $required_arg argument"
|
die "$real_opt requires a $required_arg argument"
|
||||||
else
|
else
|
||||||
val="$1"
|
val="$1"
|
||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
if [ $neg -eq 0 ]; then
|
||||||
|
val="yes"
|
||||||
|
else
|
||||||
|
val="no"
|
||||||
|
fi
|
||||||
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
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
TESTS=37
|
TESTS=24
|
||||||
|
|
||||||
TMPFILE="$TEST_TMPDIR/parse-opts-output"
|
TMPFILE="$TEST_TMPDIR/parse-opts-output"
|
||||||
|
|
||||||
@@ -11,54 +11,59 @@ source "$LIB_DIR/parse_options.sh"
|
|||||||
# Parse options from POD using all default values.
|
# Parse options from POD using all default values.
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
|
|
||||||
|
TMPDIR="$TEST_TMPDIR"
|
||||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE
|
||||||
|
|
||||||
TEST_NAME="No warnings or errors"
|
TEST_NAME="No warnings or errors"
|
||||||
is "`cat $TMPFILE`" ""
|
is "`cat $TMPFILE`" ""
|
||||||
|
|
||||||
TEST_NAME="Default opts"
|
TEST_NAME="Default opts"
|
||||||
is "$OPT_THRESHOLD" "100"
|
is "$OPT_STRING_OPT" ""
|
||||||
is "$OPT_VARIABLE" "Threads_connected"
|
is "$OPT_STRING_OPT2" "foo"
|
||||||
is "$OPT_CYCLES" "1"
|
is "$OPT_TYPELESS_OPTION" ""
|
||||||
is "$OPT_GDB" "no"
|
is "$OPT_NOPTION" "yes"
|
||||||
is "$OPT_OPROFILE" "yes"
|
is "$OPT_INT_OPT" ""
|
||||||
is "$OPT_STRACE" "no"
|
is "$OPT_INT_OPT2" "42"
|
||||||
is "$OPT_TCPDUMP" "yes"
|
is "$OPT_VERSION" ""
|
||||||
is "$OPT_EMAIL" ""
|
|
||||||
is "$OPT_INTERVAL" "30"
|
|
||||||
is "$OPT_MAYBE_EMPTY" "no"
|
|
||||||
is "$OPT_COLLECT" "${HOME}/bin/pt-collect"
|
|
||||||
is "$OPT_DEST" "${HOME}/collected/"
|
|
||||||
is "$OPT_DURATION" "30"
|
|
||||||
is "$OPT_SLEEP" "300"
|
|
||||||
is "$OPT_PCT_THRESHOLD" "95"
|
|
||||||
is "$OPT_MB_THRESHOLD" "100"
|
|
||||||
is "$OPT_PURGE" "30"
|
|
||||||
|
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
# Specify some opts, but use default values for the rest.
|
# Specify some opts, but use default values for the rest.
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
|
|
||||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --threshold 50 --gdb yes --email user@example.com
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --int-opt 50 --typeless-option --string-opt bar
|
||||||
|
|
||||||
TEST_NAME="User-specified opts with defaults"
|
TEST_NAME="User-specified opts with defaults"
|
||||||
is "$OPT_THRESHOLD" "50" # specified
|
is "$OPT_STRING_OPT" "bar" # specified
|
||||||
is "$OPT_VARIABLE" "Threads_connected"
|
is "$OPT_STRING_OPT2" "foo"
|
||||||
is "$OPT_CYCLES" "1"
|
is "$OPT_TYPELESS_OPTION" "yes" # specified
|
||||||
is "$OPT_GDB" "yes" # specified
|
is "$OPT_NOPTION" "yes"
|
||||||
is "$OPT_OPROFILE" "yes"
|
is "$OPT_INT_OPT" "50" # specified
|
||||||
is "$OPT_STRACE" "no"
|
is "$OPT_INT_OPT2" "42"
|
||||||
is "$OPT_TCPDUMP" "yes"
|
is "$OPT_VERSION" ""
|
||||||
is "$OPT_EMAIL" "user@example.com" # specified
|
|
||||||
is "$OPT_INTERVAL" "30"
|
# ############################################################################
|
||||||
is "$OPT_MAYBE_EMPTY" "no"
|
# Negate an option like --no-option.
|
||||||
is "$OPT_COLLECT" "${HOME}/bin/pt-collect"
|
# ############################################################################
|
||||||
is "$OPT_DEST" "${HOME}/collected/"
|
|
||||||
is "$OPT_DURATION" "30"
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --no-noption
|
||||||
is "$OPT_SLEEP" "300"
|
|
||||||
is "$OPT_PCT_THRESHOLD" "95"
|
TEST_NAME="Negated option"
|
||||||
is "$OPT_MB_THRESHOLD" "100"
|
is "$OPT_STRING_OPT" ""
|
||||||
is "$OPT_PURGE" "30"
|
is "$OPT_STRING_OPT2" "foo"
|
||||||
|
is "$OPT_TYPELESS_OPTION" ""
|
||||||
|
is "$OPT_NOPTION" "no" # negated
|
||||||
|
is "$OPT_INT_OPT" ""
|
||||||
|
is "$OPT_INT_OPT2" "42"
|
||||||
|
is "$OPT_VERSION" ""
|
||||||
|
|
||||||
|
# ############################################################################
|
||||||
|
# Short form.
|
||||||
|
# ############################################################################
|
||||||
|
|
||||||
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" -v
|
||||||
|
|
||||||
|
TEST_NAME="Short form"
|
||||||
|
is "$OPT_VERSION" "yes"
|
||||||
|
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
# An unknown option should produce an error.
|
# An unknown option should produce an error.
|
||||||
|
@@ -90,80 +90,42 @@ See L<"ENVIRONMENT">.
|
|||||||
|
|
||||||
=over
|
=over
|
||||||
|
|
||||||
=item --threshold N
|
=item --string-opt
|
||||||
|
|
||||||
Max number of C<N> to tolerate. (default: 100)
|
type: string
|
||||||
|
|
||||||
=item --variable NAME
|
String option without a default.
|
||||||
|
|
||||||
This is the thing to check for. (default: Threads_connected)
|
=item --string-opt2
|
||||||
|
|
||||||
=item --cycles N
|
type: string; default: foo
|
||||||
|
|
||||||
How many times must the condition be met before the script will fire? (default: 1)
|
String option with a default.
|
||||||
|
|
||||||
=item --gdb
|
=item --typeless-option
|
||||||
|
|
||||||
Collect GDB stacktraces? (default: no)
|
Just an option.
|
||||||
|
|
||||||
=item --oprofile
|
=item --noption
|
||||||
|
|
||||||
Collect oprofile data? (default: yes)
|
default: yes; negatable: yes
|
||||||
|
|
||||||
=item --strace
|
=item --int-opt
|
||||||
|
|
||||||
Collect strace data? (default: no)
|
type: int
|
||||||
|
|
||||||
=item --tcpdump
|
Int option without a default
|
||||||
|
|
||||||
Collect tcpdump data? (default: yes)
|
=item --int-opt2
|
||||||
|
|
||||||
=item --email ADDRESS
|
type: int; default: 42
|
||||||
|
|
||||||
Send mail to this list of addresses when the script triggers.
|
Int option with a default.
|
||||||
|
|
||||||
=item --interval SECONDS
|
|
||||||
|
|
||||||
This is the interval between checks. (default: 30)
|
|
||||||
|
|
||||||
=item --maybe-empty
|
|
||||||
|
|
||||||
Result of checks may be empty. (default: no)
|
|
||||||
If the command you're running to detect the condition is allowed to return
|
|
||||||
nothing (e.g. a grep line that might not even exist if there's no problem),
|
|
||||||
then set this to "yes".
|
|
||||||
|
|
||||||
=item --collect DIRECTORY
|
|
||||||
|
|
||||||
Location of the C<collect> tool. (default: ${HOME}/bin/pt-collect)
|
|
||||||
|
|
||||||
=item --dest DIRECTORY
|
|
||||||
|
|
||||||
Where to store collected data. (default: ${HOME}/collected/)
|
|
||||||
|
|
||||||
=item --duration SECONDS
|
|
||||||
|
|
||||||
How long to collect statistics data for? (default: 30)
|
|
||||||
Make sure that this isn't longer than SLEEP.
|
|
||||||
|
|
||||||
=item --sleep SECONDS
|
|
||||||
|
|
||||||
How long to sleep after collecting? (default: 300)
|
|
||||||
|
|
||||||
=item --pct-threshold PCT
|
|
||||||
|
|
||||||
Bail out if the disk is more than this %full. (default: 95)
|
|
||||||
|
|
||||||
=item --mb-threshold MEGABYTES
|
|
||||||
|
|
||||||
Bail out if the disk has less than this many MB free. (default: 100)
|
|
||||||
|
|
||||||
=item --purge DAYS
|
|
||||||
|
|
||||||
Remove samples after this many days. (default: 30)
|
|
||||||
|
|
||||||
=item --version
|
=item --version
|
||||||
|
|
||||||
|
short form: -v
|
||||||
|
|
||||||
Print tool's version and exit.
|
Print tool's version and exit.
|
||||||
|
|
||||||
=back
|
=back
|
||||||
|
Reference in New Issue
Block a user