2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11 # This software is licensed as described in the file COPYING, which
12 # you should have received as part of this distribution. The terms
13 # are also available at https://curl.se/docs/copyright.html.
15 # You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 # copies of the Software, and permit persons to whom the Software is
17 # furnished to do so, under the terms of the COPYING file.
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
22 # SPDX-License-Identifier: curl
24 ###########################################################################
34 Getopt::Long::GetOptions(
38 ) or Pod::Usage::pod2usage();
39 Pod::Usage::pod2usage() if $help;
41 my $regex = '\s+(?:(-[^\s]+),\s)?(--[^\s]+)\s*(\<.+?\>)?\s+(.*)';
42 my @opts = parse_main_opts('--help all', $regex);
44 if ($shell eq 'fish') {
45 print "# curl fish completion\n\n";
46 print qq{$_ \n} foreach (@opts);
47 } elsif ($shell eq 'zsh') {
50 $opts_str .= qq{ $_ \\\n} foreach (@opts);
58 local curcontext="\$curcontext" state state_descr line
72 die("Unsupported shell: $shell");
76 my ($cmd, $regex) = @_;
79 my @lines = call_curl($cmd);
81 foreach my $line (@lines) {
82 my ($short, $long, $arg, $desc) = ($line =~ /^$regex/) or next;
86 $arg =~ s/\:/\\\:/g if defined $arg;
88 $desc =~ s/'/'\\''/g if defined $desc;
89 $desc =~ s/\[/\\\[/g if defined $desc;
90 $desc =~ s/\]/\\\]/g if defined $desc;
91 $desc =~ s/\:/\\\:/g if defined $desc;
93 if ($shell eq 'fish') {
94 $option .= "complete --command curl";
95 $option .= " --short-option '" . strip_dash(trim($short)) . "'"
97 $option .= " --long-option '" . strip_dash(trim($long)) . "'"
99 $option .= " --description '" . strip_dash(trim($desc)) . "'"
101 } elsif ($shell eq 'zsh') {
102 $option .= '{' . trim($short) . ',' if defined $short;
103 $option .= trim($long) if defined $long;
104 $option .= '}' if defined $short;
105 $option .= '\'[' . trim($desc) . ']\'' if defined $desc;
108 $option .= ":'$arg'";
109 if ($arg =~ /<file ?(name)?>|<path>/) {
110 $option .= ':_files';
111 } elsif ($arg =~ /<dir>/) {
112 $option .= ":'_path_files -/'";
113 } elsif ($arg =~ /<url>/i) {
115 } elsif ($long =~ /ftp/ && $arg =~ /<method>/) {
116 $option .= ":'(multicwd nocwd singlecwd)'";
117 } elsif ($arg =~ /<method>/) {
118 $option .= ":'(DELETE GET HEAD POST PUT)'";
126 # Sort longest first, because zsh won't complete an option listed
127 # after one that's a prefix of it.
129 $a =~ /([^=]*)/; my $ma = $1;
130 $b =~ /([^=]*)/; my $mb = $1;
132 length($mb) <=> length($ma)
133 } @list if $shell eq 'zsh';
138 sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
139 sub strip_dash { my $s = shift; $s =~ s/^-+//g; return $s };
143 my $output = `"$curl" $cmd`;
145 die "Could not run curl: $!";
146 } elsif ((my $exit_code = $? >> 8) != 0) {
147 die "curl returned $exit_code with output:\n$output";
149 return split /\n/, $output;
156 completion.pl - Generates tab-completion files for various shells
160 completion.pl [options...]
162 --curl path to curl executable
164 --help prints this help