123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- use strict;
- use warnings;
- use Getopt::Long();
- use Pod::Usage();
- my $curl = 'curl';
- my $shell = 'zsh';
- my $help = 0;
- Getopt::Long::GetOptions(
- 'curl=s' => \$curl,
- 'shell=s' => \$shell,
- 'help' => \$help,
- ) or Pod::Usage::pod2usage();
- Pod::Usage::pod2usage() if $help;
- my $regex = '\s+(?:(-[^\s]+),\s)?(--[^\s]+)\s*(\<.+?\>)?\s+(.*)';
- my @opts = parse_main_opts('--help all', $regex);
- if ($shell eq 'fish') {
- print "# curl fish completion\n\n";
- print qq{$_ \n} foreach (@opts);
- } elsif ($shell eq 'zsh') {
- my $opts_str;
- $opts_str .= qq{ $_ \\\n} foreach (@opts);
- chomp $opts_str;
- my $tmpl = <<"EOS";
- 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;
- } else {
- die("Unsupported shell: $shell");
- }
- sub parse_main_opts {
- my ($cmd, $regex) = @_;
- my @list;
- my @lines = call_curl($cmd);
- foreach my $line (@lines) {
- my ($short, $long, $arg, $desc) = ($line =~ /^$regex/) or next;
- my $option = '';
- $arg =~ s/\:/\\\:/g if defined $arg;
- $desc =~ s/'/'\\''/g if defined $desc;
- $desc =~ s/\[/\\\[/g if defined $desc;
- $desc =~ s/\]/\\\]/g if defined $desc;
- $desc =~ s/\:/\\\:/g if defined $desc;
- if ($shell eq 'fish') {
- $option .= "complete --command curl";
- $option .= " --short-option '" . strip_dash(trim($short)) . "'"
- if defined $short;
- $option .= " --long-option '" . strip_dash(trim($long)) . "'"
- if defined $long;
- $option .= " --description '" . strip_dash(trim($desc)) . "'"
- if defined $desc;
- } elsif ($shell eq 'zsh') {
- $option .= '{' . trim($short) . ',' if defined $short;
- $option .= trim($long) if defined $long;
- $option .= '}' if defined $short;
- $option .= '\'[' . trim($desc) . ']\'' if defined $desc;
- if (defined $arg) {
- $option .= ":'$arg'";
- if ($arg =~ /<file ?(name)?>|<path>/) {
- $option .= ':_files';
- } elsif ($arg =~ /<dir>/) {
- $option .= ":'_path_files -/'";
- } elsif ($arg =~ /<url>/i) {
- $option .= ':_urls';
- } elsif ($long =~ /ftp/ && $arg =~ /<method>/) {
- $option .= ":'(multicwd nocwd singlecwd)'";
- } elsif ($arg =~ /<method>/) {
- $option .= ":'(DELETE GET HEAD POST PUT)'";
- }
- }
- }
- push @list, $option;
- }
-
-
- @list = sort {
- $a =~ /([^=]*)/; my $ma = $1;
- $b =~ /([^=]*)/; my $mb = $1;
- length($mb) <=> length($ma)
- } @list if $shell eq 'zsh';
- return @list;
- }
- sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
- sub strip_dash { my $s = shift; $s =~ s/^-+//g; return $s };
- sub call_curl {
- my ($cmd) = @_;
- my $output = `"$curl" $cmd`;
- if ($? == -1) {
- die "Could not run curl: $!";
- } elsif ((my $exit_code = $? >> 8) != 0) {
- die "curl returned $exit_code with output:\n$output";
- }
- return split /\n/, $output;
- }
- __END__
|