1 package Pod::Perldoc::GetOptsOO;
7 BEGIN { # Make a DEBUG constant ASAP
8 *DEBUG = defined( &Pod::Perldoc::DEBUG )
9 ? \&Pod::Perldoc::DEBUG
15 my($target, $args, $truth) = @_;
20 "Starting switch processing. Scanning arguments [@$args]\n"
21 ) if $target->can('aside');
25 $truth = 1 unless @_ > 2;
27 DEBUG > 3 and print " Truth is $truth\n";
32 while( @$args and ($_ = $args->[0]) =~ m/^-(.)(.*)/s ) {
33 my($first,$rest) = ($1,$2);
34 if ($_ eq '--') { # early exit if "--"
38 if ($first eq '-' and $rest) { # GNU style long param names
39 ($first, $rest) = split '=', $rest, 2;
41 my $method = "opt_${first}_with";
42 if( $target->can($method) ) { # it's argumental
43 if($rest eq '') { # like -f bar
45 $target->warn( "Option $first needs a following argument!\n" ) unless @$args;
47 } else { # like -fbar (== -f bar)
51 DEBUG > 3 and print " $method => $rest\n";
52 $target->$method( $rest );
54 # Otherwise, it's not argumental...
57 if( $target->can( $method = "opt_$first" ) ) {
58 DEBUG > 3 and print " $method is true ($truth)\n";
59 $target->$method( $truth );
61 # Otherwise it's an unknown option...
63 } elsif( $target->can('handle_unknown_option') ) {
65 and print " calling handle_unknown_option('$first')\n";
68 $target->handle_unknown_option( $first ) || 0
73 $target->warn( "Unknown option: $first\n" );
76 if($rest eq '') { # like -f
78 } else { # like -fbar (== -f -bar )
79 DEBUG > 2 and print " Setting args->[0] to \"-$rest\"\n";
80 $args->[0] = "-$rest";
87 "Ending switch processing. Args are [@$args] with $error_count errors.\n"
88 ) if $target->can('aside');
99 Pod::Perldoc::GetOptsOO - Customized option parser for Pod::Perldoc
103 use Pod::Perldoc::GetOptsOO ();
105 Pod::Perldoc::GetOptsOO::getopts( $obj, \@args, $truth )
106 or die "wrong usage";
111 Implements a customized option parser used for
114 Rather like Getopt::Std's getopts:
118 =item Call Pod::Perldoc::GetOptsOO::getopts($object, \@ARGV, $truth)
120 =item Given -n, if there's a opt_n_with, it'll call $object->opt_n_with( ARGUMENT )
121 (e.g., "-n foo" => $object->opt_n_with('foo'). Ditto "-nfoo")
123 =item Otherwise (given -n) if there's an opt_n, we'll call it $object->opt_n($truth)
124 (Truth defaults to 1)
126 =item Otherwise we try calling $object->handle_unknown_option('n')
127 (and we increment the error count by the return value of it)
129 =item If there's no handle_unknown_option, then we just warn, and then increment
134 The return value of Pod::Perldoc::GetOptsOO::getopts is true if no errors,
135 otherwise it's false.
141 =head1 COPYRIGHT AND DISCLAIMERS
143 Copyright (c) 2002-2007 Sean M. Burke.
145 This library is free software; you can redistribute it and/or modify it
146 under the same terms as Perl itself.
148 This program is distributed in the hope that it will be useful, but
149 without any warranty; without even the implied warranty of
150 merchantability or fitness for a particular purpose.
154 Current maintainer: Mark Allen C<< <mallen@cpan.org> >>
156 Past contributions from:
157 brian d foy C<< <bdfoy@cpan.org> >>
158 Adriano R. Ferreira C<< <ferreira@cpan.org> >>,
159 Sean M. Burke C<< <sburke@cpan.org> >>