root/branches/release-35/lib/MT/Tool.pm @ 1950

Revision 1950, 4.2 kB (checked in by mpaschal, 20 months ago)

Check in MT::Tool helper
Convert report-slow-requests and upgrade tools to use MT::Tool
BugzID: 67410

Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::Tool;
8
9use strict;
10use warnings;
11use charnames qw( :full );
12
13use Carp qw( croak );
14use English qw( -no_match_vars );
15use Getopt::Long;
16
17sub show_help {
18    my $class = shift;
19    my $help = $class->help();
20    # TODO: strip spaces more smartly for people who may format
21    # their help() methods differently.
22    $help =~ s/ ^ [\N{SPACE}]{4} //xmsg;
23    print $help;
24}
25
26sub show_usage {
27    my $class = shift;
28    print qq{usage:  $PROGRAM_NAME },
29        join (qq{\n        $PROGRAM_NAME }, $class->usage()),
30        qq{\n};
31}
32
33sub usage;
34sub help;
35sub options {}
36
37sub set_up_app {
38    # TODO: a Tool should probably be its own App, so we can use *all*
39    # the MT::App infrastructure. For now fake it like rpt does.
40    require MT;
41    my $mt = MT->new() or die MT->errstr;
42
43    $mt->{vtbl} = { };
44    $mt->{is_admin} = 0;
45    $mt->{template_dir} = 'cms';
46    $mt->{user_class} = 'MT::Author';
47    $mt->{plugin_template_path} = 'tmpl';
48    $mt->run_callbacks('init_app', $mt);
49
50    return $mt;
51}
52
53sub main {
54    my $class = shift;
55
56    $class->set_up_app();
57
58    my $verbose;
59    my $opts_good = GetOptions(
60        'help!'      => sub { $class->show_usage(); $class->show_help(); exit; },
61        'usage!'     => sub { $class->show_usage();                      exit; },
62        'verbose|v+' => \$verbose,
63
64        $class->options(),
65    );
66    usage(), exit if !$opts_good;
67
68    return $verbose;
69}
70
711;
72
73__END__
74
75=head1 NAME
76
77MT::Tool - shared infrastructure for command line tools
78
79=head1 SYNOPSIS
80
81    package Foobar::Tool::MakeQuuxen
82    use strict;
83
84    use lib  qw( extlib lib );
85    use base qw( MT::Tool );
86
87    sub help {
88        q{
89            --special    Make extra special quuxen.
90        };
91    }
92
93    sub usage { '[--special]' }
94
95    my ($special);
96
97    sub options {
98        return (
99            'special!' => \$special,
100        );
101    }
102
103    sub main {
104        my $class = shift;
105        ($verbose) = $class->SUPER::main(@_);
106
107        ## Make those quuxen!
108        ...
109    }
110
111    __PACKAGE__->main() unless caller;
112
113    1;
114
115=head1 DESCRIPTION
116
117I<MT::Tool> provides shared infrastructure around command line tools for MT
118applications. With these, you can provide a standard interface for your tools
119similar to MT's other command line applications.
120
121=head1 USAGE
122
123=head2 use base qw( MT::Tool )
124
125Declares the current package is a new tool conforming to the MT::Tool
126interface. The following class methods should be defined as appropriate for
127your tool.
128
129=head2 $class-E<gt>help()
130
131Return the text to use for your tool's C<--help> command.
132
133=head2 $class-E<gt>usage()
134
135Return the option text to use for your tool's C<--usage> command. The text is
136also used for the C<--help> synopsis and when invalid options are used.
137
138=head2 $class-E<gt>options()
139
140Return the definition of your tool's additional options, as a hash suitable to
141pass to Getopt::Long's C<GetOptions()>. The most common definitions for
142Getopt::Long options are:
143
144=over 4
145
146=item * C<I<option>!>
147
148Your option is a flag users can specify up to once. A bound variable will be
149set when the flag is found.
150
151The contrary option C<--noI<option>> will be automatically provided. It will
152clear the bound variable.
153
154=item * C<I<option>+>
155
156Your option is a flag users can specify more than once. A bound variable will
157be B<incremented> when the flag is found.
158
159=item * C<I<option>=s>
160
161Your option takes a string argument. A bound scalar will be set to the argument
162the user specifies when the option is found. If an arrayref is bound instead,
163arguments will be added to the list when the option is given more than once.
164
165=item * C<I<option>|I<o>>
166
167Your option is availble both as the long form C<--I<option>> and the
168abbreviated short form C<-I<o>>. Abbreviated forms can be combined with any of
169the type declarations above.
170
171=back
172
173=head2 $class-E<gt>main()
174
175Perform your tool's task. Your implementation should call MT::Tool's
176implementation to parse your options and handle the standard options such as
177C<--help>. MT::Tool's implementation will return the level of verbosity
178requested by the user (that is, how many times the C<-v> option was used).
179
180=head1 SEE ALSO
181
182Getopt::Long
183
184=cut
185
Note: See TracBrowser for help on using the browser.