From be8c0e6e18b87f3a543854459836d5fc3417efe3 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Mon, 30 Jan 2012 11:15:28 -0700 Subject: [PATCH] Fix parsing opts from config file with leading and trailing space. --- bin/pt-stalk | 2 +- lib/bash/parse_options.sh | 2 +- t/lib/bash/parse_options.sh | 11 ++++++++++- t/lib/samples/bash/config004.conf | 3 +++ 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 t/lib/samples/bash/config004.conf diff --git a/bin/pt-stalk b/bin/pt-stalk index aee02abf..17092df6 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -279,7 +279,7 @@ _parse_config_files() { echo "$config_opt" | grep '^[ ]*[^#]' >/dev/null 2>&1 || continue - config_opt="$(echo "$config_opt" | sed -e 's/^[ ]*//' -e 's/[ ]*\$//' -e 's/[ ]*=[ ]*/=/' -e 's/[ ]*#.*$//')" + config_opt="$(echo "$config_opt" | sed -e 's/^ *//g' -e 's/ *$//g' -e 's/[ ]*=[ ]*/=/' -e 's/[ ]*#.*$//')" [ "$config_opt" = "" ] && continue diff --git a/lib/bash/parse_options.sh b/lib/bash/parse_options.sh index 805b82f5..3bb2762f 100644 --- a/lib/bash/parse_options.sh +++ b/lib/bash/parse_options.sh @@ -325,7 +325,7 @@ _parse_config_files() { # Strip leading and trailing spaces, and spaces around the first =, # and end-of-line # comments. - config_opt="$(echo "$config_opt" | sed -e 's/^[ ]*//' -e 's/[ ]*\$//' -e 's/[ ]*=[ ]*/=/' -e 's/[ ]*#.*$//')" + config_opt="$(echo "$config_opt" | sed -e 's/^ *//g' -e 's/ *$//g' -e 's/[ ]*=[ ]*/=/' -e 's/[ ]*#.*$//')" # Skip blank lines. [ "$config_opt" = "" ] && continue diff --git a/t/lib/bash/parse_options.sh b/t/lib/bash/parse_options.sh index 5afa53b5..8c462eff 100644 --- a/t/lib/bash/parse_options.sh +++ b/t/lib/bash/parse_options.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=73 +TESTS=78 TMPFILE="$TEST_TMPDIR/parse-opts-output" TOOL="pt-stalk" @@ -166,6 +166,15 @@ is "$OPT_INT_OPT" "100" "Two --config int option" is "$ARGV" "" "Two --config ARGV" is "$EXT_ARGV" "--host=127.1 --user=daniel" "Two--config External ARGV" +# Spaces before and after the option[=value] lines. +parse_options "$T_LIB_DIR/samples/bash/po001.sh" --config $T_LIB_DIR/samples/bash/config004.conf + +is "$OPT_STRING_OPT" "foo" "Default string option (spacey)" +is "$OPT_TYPELESS_OPTION" "yes" "Default typeless option (spacey)" +is "$OPT_INT_OPT" "123" "Default int option (spacey)" +is "$ARGV" "" "ARGV (spacey)" +is "$EXT_ARGV" "" "External ARGV (spacey)" + # ############################################################################ # Option values with spaces. # ############################################################################ diff --git a/t/lib/samples/bash/config004.conf b/t/lib/samples/bash/config004.conf new file mode 100644 index 00000000..eb023329 --- /dev/null +++ b/t/lib/samples/bash/config004.conf @@ -0,0 +1,3 @@ + typeless-option + int-opt=123 +string-opt=foo