1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#!/usr/bin/perl
#
# Genrate bash_completion_dog
#
use strict;
my ($program) = @ARGV;
print "#!bash\n";
print "\n";
open IN, "$program -h |" or die "cannot find $program\n";
my @help = <IN>;
close IN;
# Hash of sub command arrays.
# E.g. $subcmds{'node'} = [kill, list, info, recovery, md]
my %subcmds;
# Hash of sub sub command arrays.
# E.g. $subsubcmds{'trace graph'} = [cat, stat]
my %subsubcmds;
# Hash of option arrays.
# E.g. $opts{'node list'} = [-a, --address, -p, --port, -r, --raw, -h, --help]
my %opts;
foreach (@help) {
if (/^ (\S+) (\S+)/) {
my ($cmd, $subcmd) = ($1, $2);
$subcmds{$cmd} = [] if (!defined($subcmds{$cmd}));
push @{$subcmds{$cmd}}, $subcmd;
$opts{"$cmd $subcmd"} = [];
$subsubcmds{"$cmd $subcmd"} = [];
# run sub command to get more detailed usage
open IN, "$program $cmd $subcmd -h |";
while (<IN>) {
if (/^ (-.), (--\S+)/) {
# get options
push @{$opts{"$cmd $subcmd"}}, $1;
push @{$opts{"$cmd $subcmd"}}, $2;
} elsif (/^ ([a-z]+)/) {
# get available subcommands
push @{$subsubcmds{"$cmd $subcmd"}}, $1;
}
}
close IN;
}
}
foreach my $cmd (keys %subcmds) {
my @subcmds = @{$subcmds{$cmd}};
print command($cmd, @subcmds);
foreach my $subcmd (@subcmds) {
print subcommand($cmd, $subcmd);
}
}
print <<__EOB__;
_dog()
{
local opts cur cmd subcmd
opts="@{[keys %subcmds]}"
cur="\${COMP_WORDS[COMP_CWORD]}"
if [ \$COMP_CWORD -gt 1 ]; then
cmd=\${COMP_WORDS[1]}
fi
if [ \$COMP_CWORD -gt 2 ]; then
subcmd=\${COMP_WORDS[2]}
fi
case "\${cmd}" in
__EOB__
foreach my $cmd (keys %subcmds) {
print <<__EOB__;
$cmd)
_dog_$cmd \${subcmd}
;;
__EOB__
}
print <<__EOB__;
"")
COMPREPLY=(\$( compgen -W "\${opts}" -- \${cur} ))
;;
*)
COMPREPLY=()
;;
esac
}
complete -F _dog dog
__EOB__
exit 0;
# get a completion function for dog command (e.g. _dog_vdi())
sub command {
my ($cmd, @subcmds) = @_;
my $output;
$output = <<__EOB__;
_dog_${cmd}()
{
local opts
opts="@subcmds"
case "\$1" in
__EOB__
foreach my $subcmd (@subcmds) {
$output .= <<__EOB__;
$subcmd)
_dog_${cmd}_${subcmd}
;;
__EOB__
}
$output .= <<__EOB__;
"")
COMPREPLY=(\$( compgen \\
-W "\${opts}" \\
-- "\${COMP_WORDS[COMP_CWORD]}" ))
;;
*)
COMPREPLY=()
;;
esac
}
__EOB__
$output =~ s/\t/ /g;
$output =~ s/^ //gm;
return $output;
}
# get a completion function for dog subcommands (e.g. _dog_vdi_create())
sub subcommand {
my ($cmd, $subcmd) = @_;
my $output;
my @opts = @{$opts{"$cmd $subcmd"}};
my @subsubcmds = @{$subsubcmds{"$cmd $subcmd"}};
$output = <<__EOB__;
_dog_${cmd}_${subcmd}()
{
local cur
cur="\${COMP_WORDS[COMP_CWORD]}"
case "\$cur" in
-*)
COMPREPLY=(\${COMPREPLY[@]} \\
\$( compgen \\
-W "@opts" \\
__EOB__
$output .= <<__EOB__;
-- \${cur} ))
;;
__EOB__
if ($cmd eq 'vdi' && $subcmd ne 'create') {
$output .= <<__EOB__;
*)
local dog="\${COMP_WORDS[0]}"
local vdilist="\$(\${dog} vdi list -r 2>/dev/null | awk '{print \$2}')"
COMPREPLY=(\$( compgen -W "@subsubcmds \${vdilist}" -- \${cur} ))
;;
__EOB__
} else {
$output .= <<__EOB__;
*)
COMPREPLY=(\$( compgen -W "@subsubcmds" -- \${cur} ))
;;
__EOB__
}
$output .= <<__EOB__;
esac
}
__EOB__
$output =~ s/\t/ /g;
$output =~ s/^ //gm;
return $output;
}
|