Hello,

I wrote a script that parses curl --help output and generates a zsh completion
file. Once curl is built one can run "./zsh.pl src/curl > _curl" and then move
the _curl file under "/usr/share/zsh/site-completions/" and restart a shell.

Then "curl TAB", "curl -TAB", "curl --TAB", ... should provide help text and
information on the available options.

The first attached patch fixes the format of the --help output, the other adds
the script. It is not run as part of the build process right now, but I think
it'd be nice to add a configure option to build and install the completion file.

Cheers
From 70f40a5269857b30c29e4acb29461eb516cfb572 Mon Sep 17 00:00:00 2001
From: Alessandro Ghedini <[email protected]>
Date: Thu, 7 May 2015 15:34:30 +0200
Subject: [PATCH 1/2] tool_help: fix formatting for --next option

---
 src/tool_help.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/tool_help.c b/src/tool_help.c
index ef26ded..3153fcd 100644
--- a/src/tool_help.c
+++ b/src/tool_help.c
@@ -143,7 +143,7 @@ static const char *const helptext[] = {
   " -n, --netrc         Must read .netrc for user name and password",
   "     --netrc-optional  Use either .netrc or URL; overrides -n",
   "     --netrc-file FILE  Specify FILE for netrc",
-  " -:  --next          "
+  " -:, --next          "
   "Allows the following URL to use a separate set of options",
   "     --no-alpn       Disable the ALPN TLS extension (H)",
   " -N, --no-buffer     Disable buffering of the output stream",
-- 
2.1.4

From 7724c47cb3898368282a02b3f870e3a81789629f Mon Sep 17 00:00:00 2001
From: Alessandro Ghedini <[email protected]>
Date: Thu, 7 May 2015 16:07:24 +0200
Subject: [PATCH 2/2] add script for generating zsh completion

---
 zsh.pl | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100755 zsh.pl

diff --git a/zsh.pl b/zsh.pl
new file mode 100755
index 0000000..45c7ec7
--- /dev/null
+++ b/zsh.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/perl
+
+# Generate ZSH completion
+
+use strict;
+use warnings;
+
+my $curl = $ARGV[0] || 'curl';
+
+my $regex = '\s+(?:(-[^\s]+),\s)?(--[^\s]+)\s([^\s.]+)?\s+(.*)';
+my @opts = parse_main_opts('--help', $regex);
+
+my $opts_str;
+
+$opts_str .= qq{  $_ \\\n} foreach (@opts);
+chomp $opts_str;
+
+my $tmpl = <<"EOS";
+#compdef curl
+
+# curl zsh completion
+
+local curcontext="\$curcontext" state state_descr line
+typeset -A opt_args
+
+local rc=1
+
+_arguments -C -S \\
+$opts_str
+  '*:URL:_urls' && rc=0
+
+return rc
+EOS
+
+print $tmpl;
+
+sub parse_main_opts {
+    my ($cmd, $regex) = @_;
+
+    my @list;
+    my @lines = split /\n/, `"$curl" $cmd`;
+
+    foreach my $line (@lines) {
+        my ($short, $long, $arg, $desc) = ($line =~ /^$regex/) or next;
+
+        my $option = '';
+
+        $desc =~ s/'/''/g if defined $desc;
+        $desc =~ s/\[/\\\[/g if defined $desc;
+        $desc =~ s/\]/\\\]/g if defined $desc;
+
+        $option .= '{' . trim($short) . ',' if defined $short;
+        $option .= trim($long)  if defined $long;
+        $option .= '}' if defined $short;
+        $option .= '\'[' . trim($desc) . ']\'' if defined $desc;
+
+        $option .= ":$arg" if defined $arg;
+
+        $option .= ':_files' if defined $arg and $arg eq 'FILE';
+
+        push @list, $option;
+    }
+
+    # Sort longest first, because zsh won't complete an option listed
+    # after one that's a prefix of it.
+    @list = sort {
+        $a =~ /([^=]*)/; my $ma = $1;
+        $b =~ /([^=]*)/; my $mb = $1;
+
+        length($mb) <=> length($ma)
+    } @list;
+
+    return @list;
+}
+
+sub  trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
-- 
2.1.4

Attachment: signature.asc
Description: Digital signature

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to