| 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 | |
|---|
| 7 | package MT::Tool; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use warnings; |
|---|
| 11 | use charnames qw( :full ); |
|---|
| 12 | |
|---|
| 13 | use Carp qw( croak ); |
|---|
| 14 | use English qw( -no_match_vars ); |
|---|
| 15 | use Getopt::Long; |
|---|
| 16 | |
|---|
| 17 | sub 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 | |
|---|
| 26 | sub 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 | |
|---|
| 33 | sub usage; |
|---|
| 34 | sub help; |
|---|
| 35 | sub options {} |
|---|
| 36 | |
|---|
| 37 | sub 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 | |
|---|
| 53 | sub 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 | $class->show_usage(), exit if !$opts_good; |
|---|
| 67 | |
|---|
| 68 | return $verbose; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | 1; |
|---|
| 72 | |
|---|
| 73 | __END__ |
|---|
| 74 | |
|---|
| 75 | =head1 NAME |
|---|
| 76 | |
|---|
| 77 | MT::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 | |
|---|
| 117 | I<MT::Tool> provides shared infrastructure around command line tools for MT |
|---|
| 118 | applications. With these, you can provide a standard interface for your tools |
|---|
| 119 | similar to MT's other command line applications. |
|---|
| 120 | |
|---|
| 121 | =head1 USAGE |
|---|
| 122 | |
|---|
| 123 | =head2 use base qw( MT::Tool ) |
|---|
| 124 | |
|---|
| 125 | Declares the current package is a new tool conforming to the MT::Tool |
|---|
| 126 | interface. The following class methods should be defined as appropriate for |
|---|
| 127 | your tool. |
|---|
| 128 | |
|---|
| 129 | =head2 $class-E<gt>help() |
|---|
| 130 | |
|---|
| 131 | Return the text to use for your tool's C<--help> command. |
|---|
| 132 | |
|---|
| 133 | =head2 $class-E<gt>usage() |
|---|
| 134 | |
|---|
| 135 | Return the option text to use for your tool's C<--usage> command. The text is |
|---|
| 136 | also used for the C<--help> synopsis and when invalid options are used. |
|---|
| 137 | |
|---|
| 138 | =head2 $class-E<gt>options() |
|---|
| 139 | |
|---|
| 140 | Return the definition of your tool's additional options, as a hash suitable to |
|---|
| 141 | pass to Getopt::Long's C<GetOptions()>. The most common definitions for |
|---|
| 142 | Getopt::Long options are: |
|---|
| 143 | |
|---|
| 144 | =over 4 |
|---|
| 145 | |
|---|
| 146 | =item * C<I<option>!> |
|---|
| 147 | |
|---|
| 148 | Your option is a flag users can specify up to once. A bound variable will be |
|---|
| 149 | set when the flag is found. |
|---|
| 150 | |
|---|
| 151 | The contrary option C<--noI<option>> will be automatically provided. It will |
|---|
| 152 | clear the bound variable. |
|---|
| 153 | |
|---|
| 154 | =item * C<I<option>+> |
|---|
| 155 | |
|---|
| 156 | Your option is a flag users can specify more than once. A bound variable will |
|---|
| 157 | be B<incremented> when the flag is found. |
|---|
| 158 | |
|---|
| 159 | =item * C<I<option>=s> |
|---|
| 160 | |
|---|
| 161 | Your option takes a string argument. A bound scalar will be set to the argument |
|---|
| 162 | the user specifies when the option is found. If an arrayref is bound instead, |
|---|
| 163 | arguments will be added to the list when the option is given more than once. |
|---|
| 164 | |
|---|
| 165 | =item * C<I<option>|I<o>> |
|---|
| 166 | |
|---|
| 167 | Your option is availble both as the long form C<--I<option>> and the |
|---|
| 168 | abbreviated short form C<-I<o>>. Abbreviated forms can be combined with any of |
|---|
| 169 | the type declarations above. |
|---|
| 170 | |
|---|
| 171 | =back |
|---|
| 172 | |
|---|
| 173 | =head2 $class-E<gt>main() |
|---|
| 174 | |
|---|
| 175 | Perform your tool's task. Your implementation should call MT::Tool's |
|---|
| 176 | implementation to parse your options and handle the standard options such as |
|---|
| 177 | C<--help>. MT::Tool's implementation will return the level of verbosity |
|---|
| 178 | requested by the user (that is, how many times the C<-v> option was used). |
|---|
| 179 | |
|---|
| 180 | =head2 $tool-E<gt>set_up_app() |
|---|
| 181 | |
|---|
| 182 | This helper method creates a MT instance and configures it to look like |
|---|
| 183 | a L<MT::App> instance (although, it isn't), and invokes the 'init_app' |
|---|
| 184 | callback using this instance. |
|---|
| 185 | |
|---|
| 186 | =head2 $class-E<gt>show_help |
|---|
| 187 | |
|---|
| 188 | Displays command-line help provided by the C<MT::Tool> subclass L<help> |
|---|
| 189 | method. |
|---|
| 190 | |
|---|
| 191 | =head2 $class-E<gt>show_usage |
|---|
| 192 | |
|---|
| 193 | Displays commnad-line usage instructions provided by the C<MT::Tool> |
|---|
| 194 | subclass L<usage> method. |
|---|
| 195 | |
|---|
| 196 | =head1 SEE ALSO |
|---|
| 197 | |
|---|
| 198 | Getopt::Long |
|---|
| 199 | |
|---|
| 200 | =cut |
|---|
| 201 | |
|---|