mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-10 13:11:32 +00:00
Merge lp:~daniel-nichter/percona-toolkit/bash-tool-libs r131 and update in pt-stalk.
This commit is contained in:
72
bin/pt-stalk
72
bin/pt-stalk
@@ -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.
|
||||
|
@@ -26,9 +26,11 @@ set -u
|
||||
|
||||
# Global variables. These must be global because declare inside a
|
||||
# sub will be scoped locally.
|
||||
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"
|
||||
|
||||
# Sub: usage
|
||||
# Print usage (--help) and list the program's options.
|
||||
@@ -46,15 +48,43 @@ 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
|
||||
|
||||
# No --help, --version, or errors.
|
||||
return 0
|
||||
}
|
||||
|
||||
# Sub: parse_options
|
||||
# Parse Perl POD options from a program file.
|
||||
#
|
||||
@@ -143,17 +173,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
|
||||
|
||||
@@ -171,6 +204,7 @@ parse_options() {
|
||||
# specified on the command line, then we re-eval $OPT_FOO=500 to update
|
||||
# $OPT_FOO.
|
||||
local i=0 # ARGV index
|
||||
local j=0 # OPT_ERRS index
|
||||
for opt; do
|
||||
if [ $# -eq 0 ]; then
|
||||
break # no more opts
|
||||
@@ -181,15 +215,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
|
||||
# Option does not begin with a hyphen (-), so treat it as
|
||||
@@ -217,7 +242,9 @@ 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
|
||||
|
||||
@@ -228,7 +255,9 @@ parse_options() {
|
||||
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
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
TESTS=25
|
||||
TESTS=26
|
||||
|
||||
TMPFILE="$TEST_TMPDIR/parse-opts-output"
|
||||
|
||||
@@ -11,6 +11,7 @@ source "$LIB_DIR/parse_options.sh"
|
||||
# Parse options from POD using all default values.
|
||||
# ############################################################################
|
||||
|
||||
TOOL="pt-stalk"
|
||||
TMPDIR="$TEST_TMPDIR"
|
||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE
|
||||
|
||||
@@ -64,12 +65,13 @@ is "$OPT_VERSION" "yes" "Short form"
|
||||
# ############################################################################
|
||||
|
||||
# Have to call this in a subshell because the error will cause an exit.
|
||||
(
|
||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1
|
||||
)
|
||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1
|
||||
is "`cat $TMPFILE`" "" "No warnings or errors yet"
|
||||
|
||||
usage_or_errors "$T_LIB_DIR/samples/bash/po001.sh" >$TMPFILE 2>&1
|
||||
local err=$?
|
||||
is "$err" "1" "Non-zero exit on unknown option"
|
||||
cmd_ok "grep -q 'Unknown option: foo' $TMPFILE" "Error on unknown option"
|
||||
cmd_ok "grep -q 'Unknown option: --foo' $TMPFILE" "Error on unknown option"
|
||||
|
||||
# ############################################################################
|
||||
# Done
|
||||
|
Reference in New Issue
Block a user