Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active August 29, 2015 13:56
Show Gist options
  • Save coderofsalvation/8998516 to your computer and use it in GitHub Desktop.
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'
# 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