Merge lp:~daniel-nichter/percona-toolkit/bash-tool-libs r131 and update in pt-stalk.

This commit is contained in:
Daniel Nichter
2011-12-15 12:16:33 -07:00
3 changed files with 108 additions and 47 deletions

View File

@@ -51,23 +51,52 @@ die() {
set -u
declare -a ARGV # non-option args (probably input files)
declare EXT_ARGV # everything after -- (args for an external command)
OPT_ERR=${OPT_ERR:-""}
declare -a ARGV # non-option args (probably input files)
declare EXT_ARGV # everything after -- (args for an external command)
declare -a OPT_ERRS # errors while parsing options, for usage_or_errors()
OPT_VERSION="no"
OPT_HELP="no"
usage() {
local file=$1
local usage=$(grep '^Usage: ' $file)
if [ "$OPT_ERR" ]; then
echo "Error: ${OPT_ERR}" >&2
fi
echo $usage >&2
echo >&2
echo "For more information, 'man $TOOL' or 'perldoc $file'." >&2
}
usage_or_errors() {
local file=$1
if [ "$OPT_VERSION" = "yes" ]; then
local version=$(grep '^pt-[^ ]\+ [0-9]' $file)
echo "$version"
return 1
fi
if [ "$OPT_HELP" = "yes" ]; then
usage "$file"
return 1
fi
local n_errs=${#OPT_ERRS[*]}
if [ $n_errs -gt 0 ]; then
local i=0
echo "Errors parsing command line options:" >&2
echo >&2
while [ $i -lt $n_errs ]; do
echo " * ${OPT_ERRS[$i]}" >&2
i=$(($i + 1))
done
echo >&2
usage $file
return 1
fi
return 0
}
parse_options() {
local file=$1
shift
@@ -128,17 +157,20 @@ parse_options() {
fi
;;
*)
die "Invalid attribute in $TMPDIR/po/$opt_spec: $line"
echo "Invalid attribute in $TMPDIR/po/$opt_spec: $line" >&2
exit 1
esac
done < $TMPDIR/po/$opt_spec
if [ -z "$opt" ]; then
die "No long attribute in option spec $TMPDIR/po/$opt_spec"
echo "No long attribute in option spec $TMPDIR/po/$opt_spec" >&2
exit 1
fi
if [ $neg -eq 1 ]; then
if [ -z "$default_val" ] || [ "$default_val" != "yes" ]; then
die "Option $opt_spec is negatable but not default: yes"
echo "Option $opt_spec is negatable but not default: yes" >&2
exit 1
fi
fi
@@ -146,6 +178,7 @@ parse_options() {
done
local i=0 # ARGV index
local j=0 # OPT_ERRS index
for opt; do
if [ $# -eq 0 ]; then
break # no more opts
@@ -156,15 +189,6 @@ parse_options() {
EXT_ARGV="$@"
break
fi
if [ "$opt" = "--version" ]; then
version=$(grep '^pt-[^ ]\+ [0-9]' $0)
echo "$version"
exit 0
fi
if [ "$opt" = "--help" ]; then
usage $file
exit 0
fi
shift
if [ $(expr "$opt" : "-") -eq 0 ]; then
ARGV[i]="$opt"
@@ -187,14 +211,18 @@ parse_options() {
else
spec=$(grep "^shortform:-$opt\$" $TMPDIR/po/* | cut -d ':' -f 1)
if [ -z "$spec" ]; then
die "Unknown option: $opt"
OPT_ERRS[j]="Unknown option: $real_opt"
j=$((j+1))
continue
fi
fi
required_arg=$(cat $spec | grep '^type:' | cut -d':' -f2)
if [ -n "$required_arg" ]; then
if [ $# -eq 0 ]; then
die "$real_opt requires a $required_arg argument"
OPT_ERRS[j]="$real_opt requires a $required_arg argument"
j=$((j+1))
continue
else
val="$1"
shift
@@ -618,6 +646,7 @@ lock_waits() {
# ###########################################################################
# Global variables
# ###########################################################################
TOOL=`basename $0`
OKTORUN=1
ITER=1
@@ -886,6 +915,7 @@ if [ "$(basename "$0")" = "pt-stalk" ] \
mk_tmpdir
parse_options $0 "$@"
rm_tmpdir
usage_or_errors $0 || exit $?
# Now that we have the cmd line opts, check that we can actually
# connect to MySQL.