mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-11 05:29:30 +00:00
Handle values with spaces. Still needs work; one test is failing.
This commit is contained in:
@@ -108,9 +108,13 @@ parse_options() {
|
|||||||
local file="$1"
|
local file="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
# Change --op=val to --op val because _parse_command_line() needs
|
# Reset the globals (mostly for testing).
|
||||||
# a space-separated list of "op val op val" etc.
|
ARGV=""
|
||||||
local opts=$(echo "$@" | perl -ne 's/--(\S+)=/--$1 /g, print')
|
EXT_ARGV=""
|
||||||
|
OPT_ERRS=0
|
||||||
|
OPT_VERSION="no"
|
||||||
|
OPT_HELP="no"
|
||||||
|
PO_DIR="$TMPDIR/po"
|
||||||
|
|
||||||
if [ ! -d "$PO_DIR" ]; then
|
if [ ! -d "$PO_DIR" ]; then
|
||||||
mkdir "$PO_DIR"
|
mkdir "$PO_DIR"
|
||||||
@@ -129,7 +133,7 @@ parse_options() {
|
|||||||
_parse_pod "$file"
|
_parse_pod "$file"
|
||||||
_eval_po
|
_eval_po
|
||||||
_parse_config_files
|
_parse_config_files
|
||||||
_parse_command_line $opts # do NOT quote, we want "--op" "val" not "--op val"
|
_parse_command_line "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_parse_pod() {
|
_parse_pod() {
|
||||||
@@ -230,16 +234,22 @@ _parse_config_files() {
|
|||||||
local config_files="/etc/percona-toolkit/percona-toolkit.conf /etc/percona-toolkit/$TOOL.conf $HOME/.percona-toolkit.conf $HOME/.$TOOL.conf"
|
local config_files="/etc/percona-toolkit/percona-toolkit.conf /etc/percona-toolkit/$TOOL.conf $HOME/.percona-toolkit.conf $HOME/.$TOOL.conf"
|
||||||
for config_file in $config_files; do
|
for config_file in $config_files; do
|
||||||
test -f "$config_file" || continue
|
test -f "$config_file" || continue
|
||||||
|
local dashdash=""
|
||||||
# The config file syntax is just like a command line except there
|
for conf_opt in $(grep '^[^# ]' "$config_file"); do
|
||||||
# is one option per line. In Bash, --foo --bar is the same as
|
if [ "$dashdash" ]; then
|
||||||
# --foo
|
if [ "$EXT_ARGV" ]; then
|
||||||
# --bar
|
EXT_ARGV="$EXT_ARGV $conf_opt"
|
||||||
# So we can simply cat the config file into/as the command line.
|
else
|
||||||
# The Perl changes --foo=bar to --foo bar because _parse_command_line()
|
EXT_ARGV="$conf_opt"
|
||||||
# needs a space-separated list of "opt val opt val" etc.
|
fi
|
||||||
_parse_command_line \
|
else
|
||||||
$(cat "$config_file" | perl -ne 's/--(\S+)=/--$1 /g, print')
|
_parse_command_line "$conf_opt"
|
||||||
|
if [ $? -eq 1 ]; then
|
||||||
|
dashdash=1
|
||||||
|
EXT_ARGV=""
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,20 +264,32 @@ _parse_command_line() {
|
|||||||
# a default value 100, then $OPT_FOO=100 already, but if --foo=500 is
|
# 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
|
# specified on the command line, then we re-eval $OPT_FOO=500 to update
|
||||||
# $OPT_FOO.
|
# $OPT_FOO.
|
||||||
|
local opt=""
|
||||||
|
local val=""
|
||||||
|
local next_opt_is_val=""
|
||||||
|
local opt_is_ok=""
|
||||||
|
local opt_is_negated=""
|
||||||
|
local real_opt=""
|
||||||
|
local required_arg=""
|
||||||
|
|
||||||
for opt in "$@"; do
|
for opt in "$@"; do
|
||||||
if [ $# -eq 0 ]; then
|
if [ "$next_opt_is_val" ]; then
|
||||||
break # no more opts
|
next_opt_is_val=""
|
||||||
|
if [ $# -eq 0 ] || [ $(expr "$opt" : "-") -eq 1 ]; then
|
||||||
|
OPT_ERRS=$(($OPT_ERRS + 1))
|
||||||
|
echo "$real_opt requires a $required_arg argument" >&2
|
||||||
|
continue
|
||||||
fi
|
fi
|
||||||
opt=$1
|
val="$opt"
|
||||||
|
opt_is_ok=1
|
||||||
|
else
|
||||||
if [ "$opt" = "--" ]; then
|
if [ "$opt" = "--" ]; then
|
||||||
shift
|
|
||||||
EXT_ARGV="$@"
|
EXT_ARGV="$@"
|
||||||
break
|
return 1
|
||||||
fi
|
fi
|
||||||
shift
|
|
||||||
|
# If option does not begin with a hyphen (-), it's a filename, etc.
|
||||||
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.
|
|
||||||
if [ -z "$ARGV" ]; then
|
if [ -z "$ARGV" ]; then
|
||||||
ARGV="$opt"
|
ARGV="$opt"
|
||||||
else
|
else
|
||||||
@@ -277,17 +299,23 @@ _parse_command_line() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Save real opt from cmd line for error messages.
|
# Save real opt from cmd line for error messages.
|
||||||
local real_opt="$opt"
|
real_opt="$opt"
|
||||||
|
|
||||||
# Strip leading -- or --no- from option.
|
# Strip leading -- or --no- from option.
|
||||||
if $(echo $opt | grep -q '^--no-'); then
|
if $(echo $opt | grep -q '^--no-'); then
|
||||||
neg=1
|
opt_is_negated=1
|
||||||
opt=$(echo $opt | sed 's/^--no-//')
|
opt=$(echo $opt | sed 's/^--no-//')
|
||||||
else
|
else
|
||||||
neg=0
|
opt_is_negated=""
|
||||||
opt=$(echo $opt | sed 's/^-*//')
|
opt=$(echo $opt | sed 's/^-*//')
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Split opt=val pair.
|
||||||
|
if $(echo $opt | grep '^[a-z-][a-z-]*=' >/dev/null 2>&1); then
|
||||||
|
val="$(echo $opt | awk -F= '{print $2}')"
|
||||||
|
opt="$(echo $opt | awk -F= '{print $1}')"
|
||||||
|
fi
|
||||||
|
|
||||||
# Find the option's spec file.
|
# Find the option's spec file.
|
||||||
if [ -f "$TMPDIR/po/$opt" ]; then
|
if [ -f "$TMPDIR/po/$opt" ]; then
|
||||||
spec="$TMPDIR/po/$opt"
|
spec="$TMPDIR/po/$opt"
|
||||||
@@ -304,29 +332,45 @@ _parse_command_line() {
|
|||||||
# says it has a type, then it requires a value and that value should
|
# 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
|
# be the next item ($1). Else, typeless options (like --version) are
|
||||||
# either "yes" if specified, else "no" if negatable and --no-opt.
|
# either "yes" if specified, else "no" if negatable and --no-opt.
|
||||||
local required_arg=$(cat "$spec" | awk -F: '/^type:/{print $2}')
|
required_arg=$(cat "$spec" | awk -F: '/^type:/{print $2}')
|
||||||
if [ -n "$required_arg" ]; then
|
if [ "$required_arg" ]; then
|
||||||
if [ $# -eq 0 ]; then
|
# Option takes a value.
|
||||||
OPT_ERRS=$(($OPT_ERRS + 1))
|
if [ "$val" ]; then
|
||||||
echo "$real_opt requires a $required_arg argument" >&2
|
opt_is_ok=1
|
||||||
continue
|
|
||||||
else
|
else
|
||||||
val="$1"
|
next_opt_is_val=1
|
||||||
shift
|
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
if [ $neg -eq 0 ]; then
|
# Option does not take a value.
|
||||||
val="yes"
|
if [ "$val" ]; then
|
||||||
|
OPT_ERRS=$(($OPT_ERRS + 1))
|
||||||
|
echo "Option $real_opt does not take a value" >&2
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if [ "$opt_is_negated" ]; then
|
||||||
|
val=""
|
||||||
else
|
else
|
||||||
val="no"
|
val="yes"
|
||||||
|
fi
|
||||||
|
opt_is_ok=1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ "$opt_is_ok" ]; then
|
||||||
# Get and transform the opt's long form. E.g.: -q == --quiet == QUIET.
|
# 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:])
|
opt=$(cat "$spec" | grep '^long:' | cut -d':' -f2 | sed 's/-/_/g' | tr [:lower:] [:upper:])
|
||||||
|
|
||||||
# Re-eval the option to update its global variable value.
|
# Re-eval the option to update its global variable value.
|
||||||
eval "OPT_$opt"="$val"
|
eval "OPT_$opt"="'$val'"
|
||||||
|
|
||||||
|
opt=""
|
||||||
|
val=""
|
||||||
|
next_opt_is_val=""
|
||||||
|
opt_is_ok=""
|
||||||
|
opt_is_negated=""
|
||||||
|
real_opt=""
|
||||||
|
required_arg=""
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
TESTS=37
|
TESTS=44
|
||||||
|
|
||||||
TMPFILE="$TEST_TMPDIR/parse-opts-output"
|
TMPFILE="$TEST_TMPDIR/parse-opts-output"
|
||||||
TOOL="pt-stalk"
|
TOOL="pt-stalk"
|
||||||
@@ -13,7 +13,7 @@ source "$LIB_DIR/parse_options.sh"
|
|||||||
# Parse options from POD using all default values.
|
# Parse options from POD using all default values.
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
|
|
||||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" 2>$TMPFILE
|
||||||
|
|
||||||
is "`cat $TMPFILE`" "" "No warnings or errors"
|
is "`cat $TMPFILE`" "" "No warnings or errors"
|
||||||
|
|
||||||
@@ -38,6 +38,8 @@ is "$OPT_NOPTION" "yes" "Default neg option (spec)"
|
|||||||
is "$OPT_INT_OPT" "50" "Specified int option (spec)"
|
is "$OPT_INT_OPT" "50" "Specified int option (spec)"
|
||||||
is "$OPT_INT_OPT2" "42" "Default int option with default (spec)"
|
is "$OPT_INT_OPT2" "42" "Default int option with default (spec)"
|
||||||
is "$OPT_VERSION" "" "--version (spec)"
|
is "$OPT_VERSION" "" "--version (spec)"
|
||||||
|
is "$ARGV" "" "ARGV"
|
||||||
|
is "$EXT_ARGV" "" "External ARGV"
|
||||||
|
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
# --option=value should work like --option value.
|
# --option=value should work like --option value.
|
||||||
@@ -56,7 +58,7 @@ parse_options "$T_LIB_DIR/samples/bash/po001.sh" --no-noption
|
|||||||
is "$OPT_STRING_OPT" "" "Default string option (neg)"
|
is "$OPT_STRING_OPT" "" "Default string option (neg)"
|
||||||
is "$OPT_STRING_OPT2" "foo" "Default string option with default (neg)"
|
is "$OPT_STRING_OPT2" "foo" "Default string option with default (neg)"
|
||||||
is "$OPT_TYPELESS_OPTION" "" "Default typeless option (neg)"
|
is "$OPT_TYPELESS_OPTION" "" "Default typeless option (neg)"
|
||||||
is "$OPT_NOPTION" "no" "Negated option (neg)"
|
is "$OPT_NOPTION" "" "Negated option (neg)"
|
||||||
is "$OPT_INT_OPT" "" "Default int option (neg)"
|
is "$OPT_INT_OPT" "" "Default int option (neg)"
|
||||||
is "$OPT_INT_OPT2" "42" "Default int option with default (neg)"
|
is "$OPT_INT_OPT2" "42" "Default int option with default (neg)"
|
||||||
is "$OPT_VERSION" "" "--version (neg)"
|
is "$OPT_VERSION" "" "--version (neg)"
|
||||||
@@ -103,7 +105,7 @@ cmd_ok \
|
|||||||
TOOL="pt-test"
|
TOOL="pt-test"
|
||||||
cp "$T_LIB_DIR/samples/bash/config001.conf" "$HOME/.$TOOL.conf"
|
cp "$T_LIB_DIR/samples/bash/config001.conf" "$HOME/.$TOOL.conf"
|
||||||
|
|
||||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" ""
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh"
|
||||||
|
|
||||||
is "$OPT_STRING_OPT" "abc" "Default string option (conf)"
|
is "$OPT_STRING_OPT" "abc" "Default string option (conf)"
|
||||||
is "$OPT_STRING_OPT2" "foo" "Default string option with default (conf)"
|
is "$OPT_STRING_OPT2" "foo" "Default string option with default (conf)"
|
||||||
@@ -112,16 +114,32 @@ is "$OPT_NOPTION" "yes" "Default neg option (conf)"
|
|||||||
is "$OPT_INT_OPT" "" "Default int option (conf)"
|
is "$OPT_INT_OPT" "" "Default int option (conf)"
|
||||||
is "$OPT_INT_OPT2" "42" "Default int option with default (conf)"
|
is "$OPT_INT_OPT2" "42" "Default int option with default (conf)"
|
||||||
is "$OPT_VERSION" "" "--version (conf)"
|
is "$OPT_VERSION" "" "--version (conf)"
|
||||||
is "$EXT_ARGV" "--host 127.1 --user daniel" "External ARGV (conf)"
|
is "$ARGV" "" "ARGV (conf)"
|
||||||
|
is "$EXT_ARGV" "--host=127.1 --user=daniel" "External ARGV (conf)"
|
||||||
|
|
||||||
# Command line should override config file.
|
# Command line should override config file.
|
||||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --string-opt zzz
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --string-opt zzz
|
||||||
|
|
||||||
is "$OPT_STRING_OPT" "zzz" "Command line overrides config file"
|
is "$OPT_STRING_OPT" "zzz" "Command line overrides config file"
|
||||||
|
|
||||||
|
# ############################################################################
|
||||||
|
# Option values with spaces.
|
||||||
|
# ############################################################################
|
||||||
|
|
||||||
|
# Config file
|
||||||
|
cp "$T_LIB_DIR/samples/bash/config002.conf" "$HOME/.$TOOL.conf"
|
||||||
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" ""
|
||||||
|
is "$OPT_STRING_OPT" "hello world" "Option value with space (conf)"
|
||||||
|
|
||||||
rm "$HOME/.$TOOL.conf"
|
rm "$HOME/.$TOOL.conf"
|
||||||
TOOL="pt-stalk"
|
TOOL="pt-stalk"
|
||||||
|
|
||||||
|
# Command line
|
||||||
|
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --string-opt "hello world"
|
||||||
|
is "$OPT_STRING_OPT" "hello world" "Option value with space (cmd line)"
|
||||||
|
is "$ARGV" "" "ARGV (cmd line)"
|
||||||
|
is "$EXT_ARGV" "" "External ARGV (cmd line)"
|
||||||
|
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
# Done
|
# Done
|
||||||
# ############################################################################
|
# ############################################################################
|
||||||
|
1
t/lib/samples/bash/config002.conf
Normal file
1
t/lib/samples/bash/config002.conf
Normal file
@@ -0,0 +1 @@
|
|||||||
|
--string-opt "hello world"
|
Reference in New Issue
Block a user