From 15690458fd27107ffafd2e146abd7f6cbcdc30ee Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 5 Nov 2013 17:57:17 +0100 Subject: [PATCH 1/6] _httpie: method is first argument, always --- src/_httpie | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/_httpie b/src/_httpie index 439c301..68fd419 100644 --- a/src/_httpie +++ b/src/_httpie @@ -10,17 +10,13 @@ # ------- # # * Akira Maeda +# * Valodim # # ------------------------------------------------------------------------------ # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------ -local -a _method -_method=(GET POST PUT DELETE HEAD OPTIONS TRACE CONNECT PATCH LINK UNLINK) - -_describe -t commands 'METHOD' _method || compadd "$@" - _httpie_urls() { local expl compset -S '[^:]*' @@ -56,4 +52,5 @@ _arguments -C -s \ "--version[show program's version number and exit.]" \ '--traceback[Prints exception traceback should one occur.]' \ '--debug[Prints exception traceback should one occur and other information useful for debugging HTTPie itself.]' \ - '*:URL:_httpie_urls' && return 0 + '1:method:(GET POST PUT DELETE HEAD OPTIONS TRACE CONNECT PATCH LINK UNLINK)' \ + '*:URL:_httpie_urls' && return 0 From 90be119649a660d3c526c123b6c415960662dbfb Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 6 Nov 2013 13:44:57 +0100 Subject: [PATCH 2/6] _httpie: properly work with parameters --- src/_httpie | 85 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/src/_httpie b/src/_httpie index 68fd419..b5a8dbc 100644 --- a/src/_httpie +++ b/src/_httpie @@ -17,11 +17,85 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------ +_httpie_params () { + + local ret=1 expl + + # or a url + if (( CURRENT <= 3 )) && [[ $words[2] != *:* ]] ; then + _httpie_urls && ret=0 + + # regular param, if we already have a url + elif (( CURRENT > 2 )); then + + # if the suffix is precisely : this is shorthand for a header + if [[ -prefix ':' ]]; then + PREFIX= + SUFFIX=: + fi + + # if we are in front of a : (possibly due to the PREFIX move before) + if [[ -suffix ':' ]]; then + + # this is rather buggy with normal tab behavior :\ + compstate[insert]=menu + _wanted http_header expl 'HTTP Header' \ + compadd -s ':' -S '' -- Content-Type Cookie && return 0 + fi + + # ignore all prefix stuff + compset -P '(#b)([^:@=]#)' + local name=$match[1] + + if compset -P ':'; then + _message "$name header content" + elif compset -P '@'; then + _files + elif compset -P ':='; then + _message "$name raw json data" + elif compset -P '=='; then + _message "$name url parameter value" + elif compset -P '='; then + _message "$name data field value" + else + typeset -a ops + ops=( + '=:data field' + '\::header' + '==:request parameter' + '@:data file field' + '\:=:raw json field' + ) + _describe -t httpparams "parameter types" ops -Q -S '' + fi + + ret=0 + + fi + + # first arg may be a request method + (( CURRENT == 2 )) && + _wanted http_method expl 'Request Method' \ + compadd GET POST PUT DELETE HEAD OPTIONS TRACE CONNECT PATCH LINK UNLINK && ret=0 + + return $ret + +} + _httpie_urls() { - local expl - compset -S '[^:]*' - _wanted url-schemas expl 'URL schema' compadd -S '' - \ - http:// https:// + + local ret=1 + + if ! [[ -prefix [-+.a-z0-9]#:// ]]; then + local expl + compset -S '[^:/]*' && compstate[to_end]='' + _wanted url-schemas expl 'URL schema' compadd -S '' http:// https:// && ret=0 + else + _urls && ret=0 + fi + + return $ret + } _arguments -C -s \ @@ -52,5 +126,4 @@ _arguments -C -s \ "--version[show program's version number and exit.]" \ '--traceback[Prints exception traceback should one occur.]' \ '--debug[Prints exception traceback should one occur and other information useful for debugging HTTPie itself.]' \ - '1:method:(GET POST PUT DELETE HEAD OPTIONS TRACE CONNECT PATCH LINK UNLINK)' \ - '*:URL:_httpie_urls' && return 0 + '*:args:_httpie_params' && return 0 From b94ba8198c07b5cb55d98fcfbe9a6ee43f2fdcc8 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 6 Nov 2013 14:55:26 +0100 Subject: [PATCH 3/6] _httpie: improve _arguments definitions, add print flags --- src/_httpie | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/_httpie b/src/_httpie index b5a8dbc..66c60cf 100644 --- a/src/_httpie +++ b/src/_httpie @@ -98,29 +98,47 @@ _httpie_urls() { } +_httpie_printflags () { + + local ret=1 + + compset -P '(#b)([a-zA-Z]#)' + + local -a flags + [[ $match[1] != *H* ]] && flags+=( "H:request headers" ) + [[ $match[1] != *B* ]] && flags+=( "B:request body" ) + [[ $match[1] != *h* ]] && flags+=( "h:response headers" ) + [[ $match[1] != *b* ]] && flags+=( "b:response body" ) + + _describe -t printflags "print flags" flags -S '' && ret=0 + + return $ret + +} + _arguments -C -s \ - {-j,--json}'[Data items from the command line are serialized as a JSON object.]' \ - {-f,--form}'[Data items from the command line are serialized as form fields.]' \ + '(-f)'{-j,--json}'[Data items from the command line are serialized as a JSON object.]' \ + '(-j)'{-f,--form}'[Data items from the command line are serialized as form fields.]' \ '--pretty[Controls output processing.]:PRETTY:(all colors format none)' \ - {-s,--style}'[Output coloring style]:STYLE:(autumn borland bw colorful default emacs friendly fruity manni monokai murphy native pastie perldoc ttr solarized tango trac vim vs)' \ - {-p,--print}'[String specifying what the output should contain]:OUTPUT_OPTIONS:(H B h b)' \ - {-v,--verbose}'[Print the whole request as well as the response.]' \ - {-h,--headers}'[Print only the response headers.]' \ - {-b,--body}'[Print only the response body.]' \ - {-S,--stream}'[Always stream the output by line, i.e., behave like `tail -f`.]' \ - {-o,--output}'[Save output to FILE.]:FILE:_files' \ - {-d,--download}'[Do not print the response body to stdout.]' \ - {-c,--continue}'[Resume an interrupted download.]' \ + '(-s --style)'{-s,--style}'[Output coloring style]:STYLE:(autumn borland bw colorful default emacs friendly fruity manni monokai murphy native pastie perldoc ttr solarized tango trac vim vs)' \ + '(-p --print)'{-p,--print}'=[String specifying what the output should contain]:print flags:_httpie_printflags' \ + '(-v --verbose)'{-v,--verbose}'[Print the whole request as well as the response.]' \ + '(-p)'{-h,--headers}'[Print only the response headers.]' \ + '(-p)'{-b,--body}'[Print only the response body.]' \ + '(-S --stream)'{-S,--stream}'[Always stream the output by line, i.e., behave like `tail -f`.]' \ + '(-o --output)'{-o,--output}'[Save output to FILE.]:FILE:_files' \ + '(-d --download)'{-d,--download}'[Do not print the response body to stdout.]' \ + '(-c --continue)'{-c,--continue}'[Resume an interrupted download.]' \ '--session[Create, or reuse and update a session.]:SESSION_NAME_OR_PATH' \ '--session-read-only[Create or read a session without updating it form the request/response exchange.]:SESSION_NAME_OR_PATH' \ - {-a,--auth}'[If only the username is provided (-a username)]:USER\:PASS' \ + '(-a --auth)'{-a,--auth}'[If only the username is provided (-a username)]:USER\:PASS' \ '--auth-type[The authentication mechanism to be used. Defaults to "basic".]:AUTH-TYPE:(basic digest)' \ '--proxy[String mapping protocol to the URL of the proxy (e.g.http:foo.bar:3128).]:PROXY' \ '--follow[Set this flag if full redirects are allowed]' \ - "--verify[Set to \"no\" to skip checking the host's SSL certificate.]:VERIFY:(yes no)" \ + "--verify[Set to \"no\" to skip checking the host's SSL certificate.]:verify certificate:(yes no)" \ '--allow-redirects[Set this flag if full redirects are allowed (e.g. re-POST-ing of data at new ``Location``)]' \ - '--timeout[Float describes the timeout of the request (Use socket.setdefaulttimeout() as fallback).]:SECONDS' \ - '--check-status[This flag instructs HTTPie to also check the HTTP status code and exit with an error if the status indicates one. ]' \ + '--timeout[Float describes the timeout of the request (Use socket.setdefaulttimeout() as fallback).]:timeout (seconds)' \ + '--check-status[This flag instructs HTTPie to also check the HTTP status code and exit with an error if the status indicates one.]' \ '--ignore-stdin[Do not attempt to read stdin.]' \ '--help[show help message.]' \ "--version[show program's version number and exit.]" \ From dbd9c214fc50ecbb793fac4cd03a59554452d458 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 6 Nov 2013 16:32:57 +0100 Subject: [PATCH 4/6] _httpie: use NORMARG for positioning --- src/_httpie | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/_httpie b/src/_httpie index 66c60cf..9eb49f0 100644 --- a/src/_httpie +++ b/src/_httpie @@ -22,11 +22,11 @@ _httpie_params () { local ret=1 expl # or a url - if (( CURRENT <= 3 )) && [[ $words[2] != *:* ]] ; then + if (( CURRENT <= NORMARG+1 )) && [[ $words[NORMARG] != *:* ]] ; then _httpie_urls && ret=0 # regular param, if we already have a url - elif (( CURRENT > 2 )); then + elif (( CURRENT > NORMARG )); then # if the suffix is precisely : this is shorthand for a header if [[ -prefix ':' ]]; then @@ -74,7 +74,7 @@ _httpie_params () { fi # first arg may be a request method - (( CURRENT == 2 )) && + (( CURRENT == NORMARG )) && _wanted http_method expl 'Request Method' \ compadd GET POST PUT DELETE HEAD OPTIONS TRACE CONNECT PATCH LINK UNLINK && ret=0 @@ -116,7 +116,9 @@ _httpie_printflags () { } -_arguments -C -s \ +integer NORMARG + +_arguments -n -C -s \ '(-f)'{-j,--json}'[Data items from the command line are serialized as a JSON object.]' \ '(-j)'{-f,--form}'[Data items from the command line are serialized as form fields.]' \ '--pretty[Controls output processing.]:PRETTY:(all colors format none)' \ From 512b6357e12c4891a05d22372984696b1f0718cc Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 6 Nov 2013 16:40:39 +0100 Subject: [PATCH 5/6] _httpie: fix for -pH problem --- src/_httpie | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/_httpie b/src/_httpie index 9eb49f0..dce6100 100644 --- a/src/_httpie +++ b/src/_httpie @@ -102,6 +102,9 @@ _httpie_printflags () { local ret=1 + # not sure why this is necessary, but it will complete "-pH" style without it + [[ $IPREFIX == "-p" ]] && IPREFIX+=" " + compset -P '(#b)([a-zA-Z]#)' local -a flags From 5524dac94da4ef31ae042f7564ca47587f321477 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 6 Nov 2013 16:52:52 +0100 Subject: [PATCH 6/6] _httpie: more _arguments updates --- src/_httpie | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/_httpie b/src/_httpie index dce6100..f1ea38d 100644 --- a/src/_httpie +++ b/src/_httpie @@ -122,31 +122,31 @@ _httpie_printflags () { integer NORMARG _arguments -n -C -s \ - '(-f)'{-j,--json}'[Data items from the command line are serialized as a JSON object.]' \ - '(-j)'{-f,--form}'[Data items from the command line are serialized as form fields.]' \ - '--pretty[Controls output processing.]:PRETTY:(all colors format none)' \ - '(-s --style)'{-s,--style}'[Output coloring style]:STYLE:(autumn borland bw colorful default emacs friendly fruity manni monokai murphy native pastie perldoc ttr solarized tango trac vim vs)' \ + '(-j --json -f)'{-j,--json}'[Data items from the command line are serialized as a JSON object.]' \ + '(-f --form -j)'{-f,--form}'[Data items from the command line are serialized as form fields.]' \ + '--pretty=[Controls output processing.]:output format:(all colors format none)' \ + '(-s --style)'{-s,--style}'=[Output coloring style]:STYLE:(autumn borland bw colorful default emacs friendly fruity manni monokai murphy native pastie perldoc ttr solarized tango trac vim vs)' \ '(-p --print)'{-p,--print}'=[String specifying what the output should contain]:print flags:_httpie_printflags' \ '(-v --verbose)'{-v,--verbose}'[Print the whole request as well as the response.]' \ - '(-p)'{-h,--headers}'[Print only the response headers.]' \ - '(-p)'{-b,--body}'[Print only the response body.]' \ + '(-p -h --headers)'{-h,--headers}'[Print only the response headers.]' \ + '(-p -b --body)'{-b,--body}'[Print only the response body.]' \ '(-S --stream)'{-S,--stream}'[Always stream the output by line, i.e., behave like `tail -f`.]' \ - '(-o --output)'{-o,--output}'[Save output to FILE.]:FILE:_files' \ - '(-d --download)'{-d,--download}'[Do not print the response body to stdout.]' \ + '(-o --output)'{-o,--output}'=[Save output to FILE.]:output file:_files' \ + '(-d --download)'{-d,--download}'=[Do not print the response body to stdout.]' \ '(-c --continue)'{-c,--continue}'[Resume an interrupted download.]' \ - '--session[Create, or reuse and update a session.]:SESSION_NAME_OR_PATH' \ - '--session-read-only[Create or read a session without updating it form the request/response exchange.]:SESSION_NAME_OR_PATH' \ - '(-a --auth)'{-a,--auth}'[If only the username is provided (-a username)]:USER\:PASS' \ - '--auth-type[The authentication mechanism to be used. Defaults to "basic".]:AUTH-TYPE:(basic digest)' \ - '--proxy[String mapping protocol to the URL of the proxy (e.g.http:foo.bar:3128).]:PROXY' \ - '--follow[Set this flag if full redirects are allowed]' \ - "--verify[Set to \"no\" to skip checking the host's SSL certificate.]:verify certificate:(yes no)" \ + '(--session-read-only)--session=[Create, or reuse and update a session.]:session name (or path)' \ + '(--session)--session-read-only=[Create or read a session without updating it form the request/response exchange.]:session name (or path)' \ + '(-a --auth)'{-a,--auth}'=[If only the username is provided (-a username)]:USER\:PASS' \ + '--auth-type=[The authentication mechanism to be used. Defaults to "basic".]:AUTH-TYPE:(basic digest)' \ + '--proxy=[String mapping protocol to the URL of the proxy.]:PROXY' \ + '--follow[Allow full redirects.]' \ + "--verify=[Enable or disable verification of ssl certificates.]:verify certificate:(yes no)" \ '--allow-redirects[Set this flag if full redirects are allowed (e.g. re-POST-ing of data at new ``Location``)]' \ - '--timeout[Float describes the timeout of the request (Use socket.setdefaulttimeout() as fallback).]:timeout (seconds)' \ + '--timeout=[Float describes the timeout of the request (Use socket.setdefaulttimeout() as fallback).]:timeout (seconds)' \ '--check-status[This flag instructs HTTPie to also check the HTTP status code and exit with an error if the status indicates one.]' \ '--ignore-stdin[Do not attempt to read stdin.]' \ - '--help[show help message.]' \ - "--version[show program's version number and exit.]" \ + '(- *)--help[show help message.]' \ + "(- *)--version[show program's version number and exit.]" \ '--traceback[Prints exception traceback should one occur.]' \ '--debug[Prints exception traceback should one occur and other information useful for debugging HTTPie itself.]' \ '*:args:_httpie_params' && return 0