Re: [perl #18872] File::Basename example misleading
[p5sagit/p5-mst-13.2.git] / utils / cpan
1 #!/usr/bin/perl
2 # $Id: cpan,v 1.3 2002/08/30 08:55:15 k Exp $
3 use strict;
4
5 =head1 NAME
6
7 cpan - easily interact with CPAN from the command line
8
9 =head1 SYNOPSIS
10
11         # with arguments, installs specified modules
12         cpan module_name [ module_name ... ]
13         
14         # with switches, installs modules with extra behavior
15         cpan [-cimt] module_name [ module_name ... ]
16         
17         # without arguments, starts CPAN shell
18         cpan
19         
20         # without arguments, but some switches
21         cpan [-ahrv]
22
23 =head1 DESCRIPTION
24
25 This script provides a command interface (not a shell) to CPAN.pm.
26
27 =head2 Meta Options
28
29 These options are mutually exclusive, and the script processes
30 them in this order: [ahvr].  Once the script finds one, it ignores
31 the others, and then exits after it finishes the task.  The script
32 ignores any other command line options.
33
34 =over 4
35
36 =item -a
37
38 Creates the CPAN.pm autobundle with CPAN::Shell->autobundle.  
39
40 =item -h
41
42 Prints a help message.
43
44 =item -r
45
46 Recompiles dynamically loaded modules with CPAN::Shell->recompile.
47
48 =item -v
49
50 Print the script version and CPAN.pm version.
51
52 =back
53
54 =head2 Module options
55
56 These options are mutually exclusive, and the script processes
57 them in alphabetical order. 
58
59 =over 4
60
61 =item c
62
63 Runs a `make clean` in the specified module's directories.
64
65 =item i
66
67 Installed the specified modules.
68
69 =item m
70
71 Makes the specified modules.
72
73 =item t
74
75 Runs a `make test` on the specified modules.
76
77 =back
78
79 =head2 Examples
80
81         # print a help message
82         cpan -h
83         
84         # print the version numbers
85         cpan -v
86         
87         # create an autobundle
88         cpan -a
89         
90         # recompile modules
91         cpan -r 
92         
93         # install modules
94         cpan -i Netscape::Booksmarks Business::ISBN
95
96 =head1 TO DO
97
98 * add options for other CPAN::Shell functions
99 autobundle, clean, make, recompile, test
100
101 =head1 BUGS
102
103 * none noted
104
105 =head1 SEE ALSO
106
107 Most behaviour, including environment variables and configuration,
108 comes directly from CPAN.pm.
109
110 =head1 AUTHOR
111
112 brian d foy <bdfoy@cpan.org>
113
114 =cut
115
116 use CPAN ();
117 use Getopt::Std;
118
119 my $VERSION = 
120         sprintf "%d.%02d", q$Revision: 1.3 $ =~ m/ (\d+) \. (\d+) /xg;
121
122 my $Default = 'default';
123
124 my $META_OPTIONS = 'ahvr';
125
126 my %CPAN_METHODS = (
127         $Default => 'install',
128         'c'      => 'clean',
129         'i'      => 'install',
130         'm'      => 'make',
131         't'      => 'test',
132         );
133
134 my @cpan_options = grep { $_ ne $Default } sort keys %CPAN_METHODS;
135
136 my $arg_count = @ARGV;
137 my %options;
138
139 Getopt::Std::getopts( 
140         join( '', @cpan_options, $META_OPTIONS ), \%options );
141         
142 if( $options{h} )
143         {
144         print STDERR "Printing help message -- ignoring other arguments\n"
145                 if $arg_count > 1;
146
147         print STDERR "Use perldoc to read the documentation\n";
148         exit 0;
149         }
150 elsif( $options{v} )
151         {
152         print STDERR "Printing version message -- ignoring other arguments\n"
153         
154                 if $arg_count > 1;
155
156         my $CPAN_VERSION = CPAN->VERSION;
157         print STDERR "cpan script version $VERSION\n" .
158                 "CPAN.pm version $CPAN_VERSION\n";
159         exit 0;
160         }
161 elsif( $options{a} )
162         {
163         print "Creating autobundle in ", $CPAN::Config->{cpan_home}, 
164                 "/Bundle\n";
165         print STDERR "Creating autobundle -- ignoring other arguments\n"
166                 if $arg_count > 1;
167
168         CPAN::Shell->autobundle;
169         exit 0;
170         }
171 elsif( $options{r} )
172         {
173         print STDERR "Creating autobundle -- ignoring other arguments\n"
174                 if $arg_count > 1;
175                 
176         CPAN::Shell->recompile;
177         }
178 else
179         {
180         my $switch = '';
181         
182         foreach my $option ( @cpan_options )
183                 {
184                 next unless $options{$option};
185                 $switch = $option;
186                 last;
187                 }
188         
189            if( not $switch and     @ARGV ) { $switch = $Default;     }
190         elsif( not $switch and not @ARGV ) { CPAN::shell(); exit 0;  }  
191         elsif(     $switch and not @ARGV ) 
192                 { die "Nothing to $CPAN_METHODS{$switch}!\n"; }
193
194         my $method = $CPAN_METHODS{$switch};
195         die "CPAN.pm cannot $method!\n" unless CPAN::Shell->can( $method );
196         
197         foreach my $arg ( @ARGV )
198                 {
199                 CPAN::Shell->$method( $arg );
200                 }
201         }
202         
203 1;