Last active
August 29, 2015 13:56
-
-
Save coderofsalvation/8998516 to your computer and use it in GitHub Desktop.
converts commandline arguments 'one --flag "123 4"' e.g. to a http get-string '?1=one&flag=123%204'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# converts commandline arguments to a http get-string | |
# usage: args2http one --flag 123 two --somekey somevalue <-- outputs: ?1=one&flag=123&2=two&somekey=somevalue | |
/ /bash/function/string/urlencode! | |
args2http(){ | |
httpstr="?"; pos=1; lastarg="" | |
for arg in "$@"; do | |
if [[ "${arg:0:1}" == "-" ]]; then | |
key="${arg//-/}"; httpstr="$httpstr&$key="; lastarg="option" | |
else | |
[[ "$lastarg" == "option" ]] && httpstr="$httpstr$(urlencode "$arg")" || { | |
httpstr="$httpstr&$pos=$(urlencode "$arg")"; ((pos++)); | |
}; | |
lastarg="positional" | |
fi | |
done | |
echo "${httpstr/?&/?}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment