package inc::Installer::OptionsGetter; use strict; use base qw( inc::Installer inc::Installer::Logger inc::Installer::OptionsProviderI ); our @ARGV_COPY = @ARGV; use Getopt::Long (); use Pod::Usage (); use inc::Installer::TargetHandler; use inc::Installer::OutPaths; use inc::Installer::BuildTools; use inc::Installer::CPAN; use inc::Installer::ConfigFileWriter; { my $SINGLETON; sub new { my $class = shift; if ( not $SINGLETON ) { my $self = { '_OptionsProviders' => [ inc::Installer::TargetHandler->new, inc::Installer::OutPaths->new, inc::Installer::BuildTools->new, inc::Installer::CPAN->new, inc::Installer::ConfigFileWriter->new, ], }; bless $self, $class; $SINGLETON = $self; } return $SINGLETON; } } sub SetOptionsProviders { my ( $self, @providers ) = @_; for my $prov ( @providers ) { if ( $prov->isa('inc::Installer::OptionsProviderI') ) { $self->debug("Added options provider: '$prov'"); push @{ $self->{'_OptionsProviders'} }, $prov; } else { $self->throw( 'Bad::Object' => "'$prov' not an options provider" ); } } } sub GetOptionsProviders { my $self = shift; return $self->{'_OptionsProviders'}; } sub GetOptionsHelp { my $self = shift; my $help = <<"!GROK!THIS!"; Usage: $^X $0 [--verbose] [--help] --[OPTION] [ .. --[OPTION] ] Configuration: --help|h|? Display this help and exit --verbose Raise verbosity level, can be used multiple times --quiet Lower verbosity level, can be used multiple times --man Print man page and exit !GROK!THIS! $help .= $_->OptionsHelp for @{ $self->GetOptionsProviders }; return $help; } sub GetOptions { my $self = shift; my %custom_args = map { $_->Options } @{ $self->GetOptionsProviders }; my $help; Getopt::Long::GetOptions( %custom_args, 'interactive' => \$inc::Installer::Interactive, 'install-prereq' => \$inc::Installer::InstallPrereq, 'help|?' => \$help, 'verbose' => sub { $inc::Installer::Logger::LogLevel++ }, 'quiet' => sub { $inc::Installer::Logger::LogLevel-- }, 'man' => sub { Pod::Usage::pod2usage( '-pathlist' => \@INC, '-input' => 'inc/Installer.pm', ) }, ); my $args; $args .= "\n\t'$_' => '$custom_args{$_}' " for ( sort { $a cmp $b } keys %custom_args ); $self->debug( "Custom arguments for GetOptions: $args" ); if ( $help ) { Pod::Usage::pod2usage( '-msg' => $self->GetOptionsHelp, '-pathlist' => \@INC, '-input' => 'inc/Installer.pm', ); } } 1;