Commit e2b3a85f authored by Vitaly Lipatov's avatar Vitaly Lipatov

improve usearg

parent d4f9848d
......@@ -468,13 +468,17 @@ showcmd()
SETCOLOR_NORMAL
}
# Example: $ command $(usearg -d "$PARAM")
# will add -d "$PARAM" if $PARAM is not empty
# Example:
# $ command $(usearg -d "$PARAM")
# will add -d "$PARAM" if $PARAM is not empty
# $ command $(usearg -d "$PARAM" "empty-word)
# will add -d "$PARAM" if $PARAM is not 'empty-word'
usearg()
{
local ARG=$1
shift
[ -n "$1" ] && echo "$ARG $@"
[ "$1" != "$2" ] && echo "$ARG $1"
}
# return 0 if arg1 >= arg2
......
......@@ -8,8 +8,10 @@ check()
[ "$2" != "$3" ] && echo "FATAL with '$1': result '$2' do not match with '$3'" || echo "OK for '$1' with '$2'"
}
echo "$(usearg -f TEST)"
echo "$(usearg -f TEST MORE)"
echo "$(usearg -f $TEST)"
check "-f TEST" "$(usearg -f TEST)" "-f TEST"
check "-f 'TEST MORE'" "$(usearg -f 'TEST MORE')" "-f TEST MORE"
check "-f 'TEST MORE'" "$(usearg -f TEST MORE)" "-f TEST"
check "-f 'TEST TEST'" "$(usearg -f TEST TEST)" ""
check "TEST=$TEST-f \$TEST" "$(usearg -f $TEST)" ""
TEST=124
echo "$(usearg -f $TEST)"
check "TEST=$TEST -f \$TEST" "$(usearg -f $TEST)" "-f 124"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment