aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkate.ward <kate.ward@forestent.com>2013-01-05 13:57:46 +0000
committerkate.ward <kate.ward@forestent.com>2013-01-05 13:57:46 +0000
commitb075920c3109938f9b4a6ec8715031fcc9ee74f6 (patch)
tree0fc9eb32b0afa1c810e4f57ee72e1ab546cccc60
parentda1f85666887139f0473809fe09756c9fcc1ee30 (diff)
downloadshflags-b075920c3109938f9b4a6ec8715031fcc9ee74f6.tar.gz
added built-in and expr functions to do math
-rw-r--r--source/1.0/doc/CHANGES-1.0.txt2
-rw-r--r--source/1.0/src/shflags29
-rwxr-xr-xsource/1.0/src/shflags_test_private.sh27
3 files changed, 57 insertions, 1 deletions
diff --git a/source/1.0/doc/CHANGES-1.0.txt b/source/1.0/doc/CHANGES-1.0.txt
index a259c2c..5a125e6 100644
--- a/source/1.0/doc/CHANGES-1.0.txt
+++ b/source/1.0/doc/CHANGES-1.0.txt
@@ -44,6 +44,8 @@ Issue #22. Fixed broken testGetFlagInfo() test.
Created alternate validFloat() and validInt() functions that use shell built-ins
where possible to increase performance and reduce the usage of the expr command.
+Added separate built-in and expr functions for doing math.
+
Changes with 1.0.3
------------------
diff --git a/source/1.0/src/shflags b/source/1.0/src/shflags
index 1955839..814f450 100644
--- a/source/1.0/src/shflags
+++ b/source/1.0/src/shflags
@@ -118,9 +118,11 @@ fi
# can we use built-ins?
( echo "${FLAGS_TRUE#0}"; ) >/dev/null 2>&1
if [ $? -eq ${FLAGS_TRUE} ]; then
+ __FLAGS_FX_MATH='_flags_mathBuiltin'
__FLAGS_FX_VALID_FLOAT='_flags_validFloatBuiltin'
__FLAGS_FX_VALID_INT='_flags_validIntBuiltin'
else
+ __FLAGS_FX_MATH='_flags_mathExpr'
__FLAGS_FX_VALID_FLOAT='_flags_validFloatExpr'
__FLAGS_FX_VALID_INT='_flags_validIntExpr'
fi
@@ -756,7 +758,7 @@ _flags_parseGetopt()
# properly give user access to non-flag arguments mixed in between flag
# arguments. Its usage was replaced by FLAGS_ARGV, and it is being kept only
# for backwards compatibility reasons.
- FLAGS_ARGC=`${FLAGS_EXPR_CMD} $# - 1 - ${_flags_argc_}`
+ FLAGS_ARGC=`${__FLAGS_FX_MATH} $# - 1 - ${_flags_argc_}`
# handle options. note options with values must do an additional shift
while true; do
@@ -883,6 +885,31 @@ _flags_parseGetopt()
return ${flags_return}
}
+# Perform some path using built-ins.
+#
+# Args:
+# $@: string: math expression to evaluate
+# Returns:
+# string: the result
+_flags_mathBuiltin()
+{
+ # Variable assignment is needed as workaround for Solaris Bourne shell, which
+ # cannot parse a bare $((expression)).
+ _flags_expr_='$(($@))'
+ eval echo ${_flags_expr_}
+}
+
+# Perform some path using expr.
+#
+# Args:
+# $@: string: math expression to evaluate
+# Returns:
+# string: the result
+_flags_mathExpr()
+{
+ eval ${FLAGS_EXPR_CMD} $@
+}
+
#------------------------------------------------------------------------------
# public functions
#
diff --git a/source/1.0/src/shflags_test_private.sh b/source/1.0/src/shflags_test_private.sh
index f353bb3..7065940 100755
--- a/source/1.0/src/shflags_test_private.sh
+++ b/source/1.0/src/shflags_test_private.sh
@@ -179,6 +179,33 @@ testValidIntExpr()
_testValidInt _flags_validIntExpr
}
+_testMath()
+{
+ fx=$1
+
+ assertEquals 2 `${fx} 1 + 1`
+ assertEquals 2 `${fx} '1 + 1'`
+
+ assertNotEquals 3 `${fx} 1 + 1`
+ assertNotEquals 3 `${fx} '1 + 1'`
+}
+
+testMathBuiltin()
+{
+ # Are we running a shell that can handle a built-in version? The Solaris
+ # Bourne shell for one does not support what we need.
+ if [ "${__FLAGS_FX_VALID_INT}" != '_flags_validIntBuiltin' ]; then
+ echo 'SKIPPED: this shell does not support the necessary builtins'
+ return
+ fi
+ _testMath _flags_mathBuiltin
+}
+
+testMathExpr()
+{
+ _testMath _flags_mathExpr
+}
+
#------------------------------------------------------------------------------
# suite functions
#