| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
use Data::Dumper; |
| 4 |
|
| 5 |
use Getopt::Complete ( |
| 6 |
# list the explicit values which are valid for this option |
| 7 |
'frog' => ['ribbit','urp','ugh'], |
| 8 |
|
| 9 |
# you can add any valid Getopt::Long specification to the key |
| 10 |
# if you put nothing: "=s" is assumed |
| 11 |
'names=s@' => ['eenie','meanie','miney'], |
| 12 |
|
| 13 |
# undef means we don't know how to complete the value: any value specified will do |
| 14 |
# this will result in no shell ompletions, but will still expect a value to be entered |
| 15 |
# unless it is boolean |
| 16 |
'name=s' => undef, |
| 17 |
|
| 18 |
# boolean values never have a completion list, and will yell if you are that foolish |
| 19 |
'go!' => undef, |
| 20 |
'fast!' => undef, |
| 21 |
|
| 22 |
# handle unnamed arguments from the command-line ("non-option" arguments) with a spcial key: |
| 23 |
# you can control completions on these too... |
| 24 |
'<>' => 'd', |
| 25 |
|
| 26 |
# CODE callbacks allow a the completion list to be dynamically resolved |
| 27 |
'fraggle' => sub { return ['rock','roll','fried fish','fried taters','fries and squid'] }, |
| 28 |
|
| 29 |
# support for Bash "compgen" builtins is present with some pre-made callbacks |
| 30 |
'myfile' => \&Getopt::Complete::files, |
| 31 |
'mydir' => \&Getopt::Complete::directories, |
| 32 |
|
| 33 |
# callbacks get extra info to help them, including the part of the |
| 34 |
# word already typed, and the remainder of the options already processed for context |
| 35 |
'type' => ['people','places'], |
| 36 |
'instance'=> sub { |
| 37 |
my ($command, $value, $key, $other_opts) = @_; |
| 38 |
if (my $type = $other_opts->{type}) { |
| 39 |
if ($type eq 'people') { |
| 40 |
return [qw/larry moe curly/] |
| 41 |
} |
| 42 |
elsif ($type eq 'places') { |
| 43 |
return [qw/here there everywhere/], |
| 44 |
} |
| 45 |
} |
| 46 |
return []; |
| 47 |
}, |
| 48 |
|
| 49 |
# we used-to fail to process these |
| 50 |
'dash-key' => ['dancer','prancer','vixen'], |
| 51 |
|
| 52 |
# options with dashes in the values are confusing to the user, but not the app... |
| 53 |
'valdash' => [qw/-a -b -bc -bd/], |
| 54 |
); |
| 55 |
|
| 56 |
print Dumper($ARGS); |
| 57 |
|
| 58 |
1; |
| 59 |
|
| 60 |
|