3 # Generate ZSH completion
8 my $curl = $ARGV[0] || 'curl';
10 my $regex = '\s+(?:(-[^\s]+),\s)?(--[^\s]+)\s([^\s.]+)?\s+(.*)';
11 my @opts = parse_main_opts('--help', $regex);
15 $opts_str .= qq{ $_ \\\n} foreach (@opts);
23 local curcontext="\$curcontext" state state_descr line
38 my ($cmd, $regex) = @_;
41 my @lines = call_curl($cmd);
43 foreach my $line (@lines) {
44 my ($short, $long, $arg, $desc) = ($line =~ /^$regex/) or next;
48 $desc =~ s/'/'\\''/g if defined $desc;
49 $desc =~ s/\[/\\\[/g if defined $desc;
50 $desc =~ s/\]/\\\]/g if defined $desc;
52 $option .= '{' . trim($short) . ',' if defined $short;
53 $option .= trim($long) if defined $long;
54 $option .= '}' if defined $short;
55 $option .= '\'[' . trim($desc) . ']\'' if defined $desc;
57 $option .= ":$arg" if defined $arg;
60 if defined $arg and ($arg eq 'FILE' || $arg eq 'DIR');
65 # Sort longest first, because zsh won't complete an option listed
66 # after one that's a prefix of it.
68 $a =~ /([^=]*)/; my $ma = $1;
69 $b =~ /([^=]*)/; my $mb = $1;
71 length($mb) <=> length($ma)
77 sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
81 my $output = `"$curl" $cmd`;
83 die "Could not run curl: $!";
84 } elsif ((my $exit_code = $? >> 8) != 0) {
85 die "curl returned $exit_code with output:\n$output";
87 return split /\n/, $output;