Permalink
Browse files

better option parsing

  • Loading branch information...
1 parent b19ed65 commit aab2dafe694cd4e9fba43a0bff85c0b1497ad26b @micha committed Jan 7, 2011
Showing with 16 additions and 6 deletions.
  1. +16 −6 resty
View
22 resty
@@ -14,7 +14,7 @@ export _resty_path=""
export _resty_nohistory=""
function resty() {
- local confdir host cookies method h2t editor domain _path opt dat res ret out err verbose raw i d tmpf args2
+ local confdir host cookies method h2t editor domain _path opt dat res ret out err verbose raw i j d tmpf args2 hasdata vimedit curlopt
confdir="${HOME}/.resty"
host="${confdir}/host"
@@ -24,13 +24,21 @@ function resty() {
|| which html2text || which cat)
editor=$(which "$EDITOR" || which vim || echo "vi")
+ [ "${method#P}" != "$method" ] || [ "$method" = "TRACE" ] && hasdata="yes"
+
[ -d "$cookies" ] || (mkdir -p "$cookies"; echo "http://localhost*" > "$host")
[ -n "$1" ] && [ "${1#/}" != "$1" ] \
&& _path=$(echo "$1"|sed 's/%/%25/g;s/\[/%5B/g;s/\]/%5D/g;s/|/%7C/g;s/\$/%24/g;s/&/%26/g;s/+/%2B/g;s/,/%2C/g;s/:/%3A/g;s/;/%3B/g;s/=/%3D/g;s/?/%3F/g;s/@/%40/g;s/ /%20/g;s/#/%23/g;s/{/%7B/g;s/}/%7D/g;s/\\/%5C/g;s/\^/%5E/g;s/~/%7E/g;s/`/%60/g') && shift
+ j=0
for i in "$@"; do
([ "$i" = "--verbose" ] || echo "$i" | grep -q '^-[a-zA-Z]*v[a-zA-Z]*$') \
- && verbose="yes"
+ && verbose="yes" && continue
+ [ "$i" = "-V" ] && vimedit="yes" && continue
+ [ "$i" = "-Z" ] || [ "$method" = "HEAD" ] || [ "$method" = "OPTIONS" ] \
+ && raw="yes" && continue
+ [ "$i" = "-W" ] && continue
+ curlopt[j]="$i" && j=$((j + 1))
done
[ -z "$_resty_host" ] && _resty_host=$(cat "$host" 2>/dev/null)
@@ -42,10 +50,10 @@ function resty() {
case "$method" in
HEAD|OPTIONS|GET|DELETE|POST|PUT|TRACE)
- dat=$( ( [ "${method#P}" != "$method" ] \
+ dat=$( ( [ "$hasdata" = "yes" ] \
&& ( ( [ -n "$1" ] && [ "${1#-}" = "$1" ] && echo "$1") \
|| echo "@-") ) || echo)
- if [ "${method#P}" != "$method" ] && [ "$1" = "-V" ]; then
+ if [ "$hasdata" = "yes" ] && [ "$1" = "-V" ]; then
tmpf=$(mktemp /tmp/resty.XXXXXX)
cat > $tmpf
(exec < /dev/tty; "$editor" $tmpf)
@@ -60,7 +68,7 @@ function resty() {
eval "args2=( $(cat "$confdir/$domain" 2>/dev/null |sed 's/^ *//' |grep ^$method |cut -b $((${#method}+2))-) )"
res=$((((curl -sLv $opt "$dat" -X $method \
-b "$cookies/$domain" -c "$cookies/$domain" \
- "${args2[@]}" "$@" "$_path" \
+ "${args2[@]}" "${curlopt[@]}" "$_path" \
|sed 's/^/OUT /' && echo) 3>&2 2>&1 1>&3) \
|sed 's/^/ERR /' && echo) 2>&1)
out=$(echo "$res" |sed '/^OUT /s/^....//p; d')
@@ -119,6 +127,8 @@ function TRACE() {
resty TRACE "$@"
}
-[ "$1" = "-W" ] && export _resty_nohistory="/dev/null" && shift
+for i in "$@"; do
+ [ "$i" = "-W" ] && export _resty_nohistory="/dev/null"
+done
resty "$@" >/dev/null 2>&1

0 comments on commit aab2daf

Please sign in to comment.