| 1 |
#! /usr/bin/perl |
| 2 |
# |
| 3 |
######################################################################### |
| 4 |
# This Perl script is Copyright (c) 2009, Peter J Billam # |
| 5 |
# www.pjb.com.au # |
| 6 |
# # |
| 7 |
# This script is free software; you can redistribute it and/or # |
| 8 |
# modify it under the same terms as Perl itself. # |
| 9 |
######################################################################### |
| 10 |
my $Version = '1.0'; |
| 11 |
my $VersionDate = '27sep2009'; |
| 12 |
use Term::Clui; |
| 13 |
# require '/home/pjb/dist/Term-Clui-1.42/Clui.pm'; import Term::Clui; |
| 14 |
|
| 15 |
my $multiple_choice = 0; |
| 16 |
my $filter_stdin = 0; |
| 17 |
while ($ARGV[0] =~ /^-([a-z])/) { |
| 18 |
if ($1 eq 'v') { shift; |
| 19 |
my $n = $0; $n =~ s{^.*/([^/]+)$}{$1}; |
| 20 |
print "$n version $Version $VersionDate\n"; |
| 21 |
exit 0; |
| 22 |
} elsif ($1 eq 'm') { $multiple_choice = 1; shift; |
| 23 |
} elsif ($1 eq 'f') { $filter_stdin = 1; shift; |
| 24 |
} else { |
| 25 |
print "usage:\n"; my $synopsis = 0; |
| 26 |
while (<DATA>) { |
| 27 |
if (/^=head1 SYNOPSIS/) { $synopsis = 1; next; } |
| 28 |
if ($synopsis && /^=head1/) { last; } |
| 29 |
if ($synopsis && /\S/) { s/^\s*/ /; print $_; next; } |
| 30 |
} |
| 31 |
exit 0; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
my $question = shift; |
| 36 |
my @list; |
| 37 |
if ($filter_stdin) { @list = <>; chomp(@list); } else { @list = @ARGV; } |
| 38 |
|
| 39 |
if ($multiple_choice) { |
| 40 |
print join("\n", choose($question, @list)),"\n"; |
| 41 |
} else { |
| 42 |
print choose($question, @list)."\n"; |
| 43 |
} |
| 44 |
|
| 45 |
__END__ |
| 46 |
|
| 47 |
=pod |
| 48 |
|
| 49 |
=head1 NAME |
| 50 |
|
| 51 |
choose - Lets the user choose between arguments, or lines of STDIN |
| 52 |
|
| 53 |
=head1 SYNOPSIS |
| 54 |
|
| 55 |
FILE=`choose 'Which header file ?' *.h` |
| 56 |
MY_GROUPS=`groups` |
| 57 |
chgrp `choose "Change $FILE to which group ?" $MY_GROUPS` $FILE |
| 58 |
|
| 59 |
lsusb | choose -f 'Which USB device ?' |
| 60 |
lsusb | choose -f -m 'Which USB devices ?' |
| 61 |
|
| 62 |
case `choose "Which SQL command ?" DELETE INSERT UPDATE` in |
| 63 |
'') exit ;; |
| 64 |
DELETE) ...;; |
| 65 |
INSERT) ...;; |
| 66 |
UPDATE) ...;; |
| 67 |
esac |
| 68 |
|
| 69 |
=head1 DESCRIPTION |
| 70 |
|
| 71 |
This script offers a shell-level interface to the Term::Clui CPAN-module. |
| 72 |
The first argument is the question; |
| 73 |
by default, subsequent arguments are offered as choices; |
| 74 |
with the B<-f> (Filter) option, the lines of STDIN are offered as the choices. |
| 75 |
|
| 76 |
For the user, it uses the Arrow-keys and Return, or B<q> to quit. |
| 77 |
If a B<-m> multiple-choice is being offered, |
| 78 |
then SpaceBar highlights choices additional to the one under the final Return. |
| 79 |
|
| 80 |
This program comes packaged with the Term::Clui module, |
| 81 |
in the C<examples/> directory. |
| 82 |
|
| 83 |
=head1 OPTIONS |
| 84 |
|
| 85 |
=over 3 |
| 86 |
|
| 87 |
=item B<-f> |
| 88 |
|
| 89 |
Causes I<choose> to work as a B<f>ilter (like I<grep>), |
| 90 |
so that the user chooses between lines from the standard input. |
| 91 |
(The default is that the user chooses between |
| 92 |
the 2nd and all subsequent arguments.) |
| 93 |
|
| 94 |
=item B<-m> |
| 95 |
|
| 96 |
This offers multiple-choice; the equivalent of |
| 97 |
calling I<Term::Clui::choose> in a list context. |
| 98 |
|
| 99 |
=item B<-v> |
| 100 |
|
| 101 |
Prints version number. |
| 102 |
|
| 103 |
=back |
| 104 |
|
| 105 |
=head1 CHANGES |
| 106 |
|
| 107 |
20090928 1.1 pod tidied up, and -f options documented |
| 108 |
20090927 1.0 first working version |
| 109 |
|
| 110 |
=head1 AUTHOR |
| 111 |
|
| 112 |
Peter J Billam http://www.pjb.com.au/comp/contact.html |
| 113 |
|
| 114 |
=head1 CREDITS |
| 115 |
|
| 116 |
Based on the CPAN module Term::Clui |
| 117 |
|
| 118 |
=head1 SEE ALSO |
| 119 |
|
| 120 |
http://search.cpan.org/perldoc?Term::Clui |
| 121 |
http://search.cpan.org/~pjb |
| 122 |
http://www.pjb.com.au/ |
| 123 |
perl(1) |
| 124 |
|
| 125 |
=cut |