xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Perldoc/lib/Pod/Perldoc/GetOptsOO.pm (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1898184e3Ssthenpackage Pod::Perldoc::GetOptsOO;
2898184e3Ssthenuse strict;
3898184e3Ssthen
4898184e3Ssthenuse vars qw($VERSION);
5*9f11ffb7Safresh1$VERSION = '3.28';
6898184e3Ssthen
7898184e3SsthenBEGIN { # Make a DEBUG constant ASAP
8898184e3Ssthen  *DEBUG = defined( &Pod::Perldoc::DEBUG )
9898184e3Ssthen   ? \&Pod::Perldoc::DEBUG
10898184e3Ssthen   : sub(){10};
11898184e3Ssthen}
12898184e3Ssthen
13898184e3Ssthen
14898184e3Ssthensub getopts {
15898184e3Ssthen  my($target, $args, $truth) = @_;
16898184e3Ssthen
17898184e3Ssthen  $args ||= \@ARGV;
18898184e3Ssthen
19898184e3Ssthen  $target->aside(
20898184e3Ssthen    "Starting switch processing.  Scanning arguments [@$args]\n"
21898184e3Ssthen  ) if $target->can('aside');
22898184e3Ssthen
23898184e3Ssthen  return unless @$args;
24898184e3Ssthen
25898184e3Ssthen  $truth = 1 unless @_ > 2;
26898184e3Ssthen
27898184e3Ssthen  DEBUG > 3 and print "   Truth is $truth\n";
28898184e3Ssthen
29898184e3Ssthen
30898184e3Ssthen  my $error_count = 0;
31898184e3Ssthen
32898184e3Ssthen  while( @$args  and  ($_ = $args->[0]) =~ m/^-(.)(.*)/s ) {
33898184e3Ssthen    my($first,$rest) = ($1,$2);
34898184e3Ssthen    if ($_ eq '--') {	# early exit if "--"
35898184e3Ssthen      shift @$args;
36898184e3Ssthen      last;
37898184e3Ssthen    }
38898184e3Ssthen    if ($first eq '-' and $rest) {      # GNU style long param names
39898184e3Ssthen      ($first, $rest) = split '=', $rest, 2;
40898184e3Ssthen    }
41898184e3Ssthen    my $method = "opt_${first}_with";
42898184e3Ssthen    if( $target->can($method) ) {  # it's argumental
43898184e3Ssthen      if($rest eq '') {   # like -f bar
44898184e3Ssthen        shift @$args;
45898184e3Ssthen        $target->warn( "Option $first needs a following argument!\n" ) unless @$args;
46898184e3Ssthen        $rest = shift @$args;
47898184e3Ssthen      } else {            # like -fbar  (== -f bar)
48898184e3Ssthen        shift @$args;
49898184e3Ssthen      }
50898184e3Ssthen
51898184e3Ssthen      DEBUG > 3 and print " $method => $rest\n";
52898184e3Ssthen      $target->$method( $rest );
53898184e3Ssthen
54898184e3Ssthen    # Otherwise, it's not argumental...
55898184e3Ssthen    } else {
56898184e3Ssthen
57898184e3Ssthen      if( $target->can( $method = "opt_$first" ) ) {
58898184e3Ssthen        DEBUG > 3 and print " $method is true ($truth)\n";
59898184e3Ssthen        $target->$method( $truth );
60898184e3Ssthen
61898184e3Ssthen      # Otherwise it's an unknown option...
62898184e3Ssthen
63898184e3Ssthen      } elsif( $target->can('handle_unknown_option') ) {
64898184e3Ssthen        DEBUG > 3
65898184e3Ssthen         and print " calling handle_unknown_option('$first')\n";
66898184e3Ssthen
67898184e3Ssthen        $error_count += (
68898184e3Ssthen          $target->handle_unknown_option( $first ) || 0
69898184e3Ssthen        );
70898184e3Ssthen
71898184e3Ssthen      } else {
72898184e3Ssthen        ++$error_count;
73898184e3Ssthen        $target->warn( "Unknown option: $first\n" );
74898184e3Ssthen      }
75898184e3Ssthen
76898184e3Ssthen      if($rest eq '') {   # like -f
77898184e3Ssthen        shift @$args
78898184e3Ssthen      } else {            # like -fbar  (== -f -bar )
79898184e3Ssthen        DEBUG > 2 and print "   Setting args->[0] to \"-$rest\"\n";
80898184e3Ssthen        $args->[0] = "-$rest";
81898184e3Ssthen      }
82898184e3Ssthen    }
83898184e3Ssthen  }
84898184e3Ssthen
85898184e3Ssthen
86898184e3Ssthen  $target->aside(
87898184e3Ssthen    "Ending switch processing.  Args are [@$args] with $error_count errors.\n"
88898184e3Ssthen  ) if $target->can('aside');
89898184e3Ssthen
90898184e3Ssthen  $error_count == 0;
91898184e3Ssthen}
92898184e3Ssthen
93898184e3Ssthen1;
94898184e3Ssthen
95898184e3Ssthen__END__
96898184e3Ssthen
97898184e3Ssthen=head1 NAME
98898184e3Ssthen
99898184e3SsthenPod::Perldoc::GetOptsOO - Customized option parser for Pod::Perldoc
100898184e3Ssthen
101898184e3Ssthen=head1 SYNOPSIS
102898184e3Ssthen
103898184e3Ssthen    use Pod::Perldoc::GetOptsOO ();
104898184e3Ssthen
105898184e3Ssthen    Pod::Perldoc::GetOptsOO::getopts( $obj, \@args, $truth )
106898184e3Ssthen       or die "wrong usage";
107898184e3Ssthen
108898184e3Ssthen
109898184e3Ssthen=head1 DESCRIPTION
110898184e3Ssthen
111898184e3SsthenImplements a customized option parser used for
112898184e3SsthenL<Pod::Perldoc>.
113898184e3Ssthen
114898184e3SsthenRather like Getopt::Std's getopts:
115898184e3Ssthen
116898184e3Ssthen=over
117898184e3Ssthen
118898184e3Ssthen=item Call Pod::Perldoc::GetOptsOO::getopts($object, \@ARGV, $truth)
119898184e3Ssthen
120898184e3Ssthen=item Given -n, if there's a opt_n_with, it'll call $object->opt_n_with( ARGUMENT )
121898184e3Ssthen   (e.g., "-n foo" => $object->opt_n_with('foo').  Ditto "-nfoo")
122898184e3Ssthen
123898184e3Ssthen=item Otherwise (given -n) if there's an opt_n, we'll call it $object->opt_n($truth)
124898184e3Ssthen   (Truth defaults to 1)
125898184e3Ssthen
126898184e3Ssthen=item Otherwise we try calling $object->handle_unknown_option('n')
127898184e3Ssthen   (and we increment the error count by the return value of it)
128898184e3Ssthen
129898184e3Ssthen=item If there's no handle_unknown_option, then we just warn, and then increment
130898184e3Ssthen   the error counter
131898184e3Ssthen
132898184e3Ssthen=back
133898184e3Ssthen
134898184e3SsthenThe return value of Pod::Perldoc::GetOptsOO::getopts is true if no errors,
135898184e3Ssthenotherwise it's false.
136898184e3Ssthen
137898184e3Ssthen=head1 SEE ALSO
138898184e3Ssthen
139898184e3SsthenL<Pod::Perldoc>
140898184e3Ssthen
141898184e3Ssthen=head1 COPYRIGHT AND DISCLAIMERS
142898184e3Ssthen
143898184e3SsthenCopyright (c) 2002-2007 Sean M. Burke.
144898184e3Ssthen
145898184e3SsthenThis library is free software; you can redistribute it and/or modify it
146898184e3Ssthenunder the same terms as Perl itself.
147898184e3Ssthen
148898184e3SsthenThis program is distributed in the hope that it will be useful, but
149898184e3Ssthenwithout any warranty; without even the implied warranty of
150898184e3Ssthenmerchantability or fitness for a particular purpose.
151898184e3Ssthen
152898184e3Ssthen=head1 AUTHOR
153898184e3Ssthen
154898184e3SsthenCurrent maintainer: Mark Allen C<< <mallen@cpan.org> >>
155898184e3Ssthen
156898184e3SsthenPast contributions from:
157898184e3Ssthenbrian d foy C<< <bdfoy@cpan.org> >>
158898184e3SsthenAdriano R. Ferreira C<< <ferreira@cpan.org> >>,
159898184e3SsthenSean M. Burke C<< <sburke@cpan.org> >>
160898184e3Ssthen
161898184e3Ssthen=cut
162