AppConfig-Std

 view release on metacpan or  search on metacpan

lib/AppConfig/Std.pm  view on Meta::CPAN

# we also make use of Pod::Usage, but require it if needed

use vars qw(@ISA $VERSION);

@ISA     = qw(AppConfig);

#=======================================================================
#
# new() - constructor
#
# The constructor:
#   > invokes the AppConfig constructor with standard config
#   > blesses the instance into this package
#   > defines the -help, -doc, -version, and -debug options
#       > configures with any additional options passed to constructor
#
#=======================================================================
sub new
{
    my $class = shift;
    my $cfg   = shift;

    my $self;


    $self = bless AppConfig->new({
                               GLOBAL => { ARGCOUNT => 1
                                         }}), $class;

    $self->define('help',    { ARGCOUNT => 0 } );
    $self->define('doc',     { ARGCOUNT => 0 } );
    $self->define('version', { ARGCOUNT => 0 } );
    $self->define('verbose', { ARGCOUNT => 0 } );
    $self->define('debug',   { ARGCOUNT => 0 } );

    $self->_configure($cfg) if defined $cfg;

    return $self;
}


#=======================================================================
#
# args() - parse command-line arguments (@ARGV)
#
# We over-ride the args() method, to handle the -doc, -help
# and -version command-line switches.
#
#=======================================================================
sub args
{
    my $self = shift;
    my $ref  = shift;

    my $result;


    #-------------------------------------------------------------------
    # Use AppConfig's args() method to parse the command-line.
    #-------------------------------------------------------------------
    $result = $self->SUPER::args($ref);

    #-------------------------------------------------------------------
    # If the command-line was successfully parsed (returned TRUE),
    # then check for the standard command-line switches.
    #-------------------------------------------------------------------
    if ($result) {
        $self->_handle_std_opts();
    }

    return $result;
}


#=======================================================================
#
# getopt() - parse command-line arguments (@ARGV)
#
# We over-ride the getopt() method, to handle the -doc, -help
# and -version command-line switches.
#
#=======================================================================
sub getopt
{
    my $self = shift;
    my $ref  = shift;

    my $result;


    #-------------------------------------------------------------------
    # Use AppConfig's getopt() method to parse the command-line.
    #-------------------------------------------------------------------
    $result = $self->SUPER::getopt($ref);

    #-------------------------------------------------------------------
    # If the command-line was successfully parsed (returned TRUE),
    # then check for the standard command-line switches.
    #-------------------------------------------------------------------
    if ($result) {
        $self->_handle_std_opts();
    }

    return $result;
}


#=======================================================================
#
# _handle_std_opts() - handle the standard options defined by us
#
#=======================================================================
sub _handle_std_opts
{
    my $self = shift;


    #-------------------------------------------------------------------
    # We only load Pod::Usage if we're gonna use it.
    # Because we're require'ing, the functions don't get exported
    # to us, hence the explicit namespace reference.
    #-------------------------------------------------------------------
    require Pod::Usage if $self->doc || $self->help;
    Pod::Usage::pod2usage({-verbose => 2, -exitval => 0}) if $self->doc();
    Pod::Usage::pod2usage({-verbose => 1, -exitval => 0}) if $self->help();
    _show_version() if $self->version();
}


#=======================================================================
#
# _show_version()
#
# Display the version number of the script. This assumes that
# the invoking script has defined $VERSION.
#
#=======================================================================
sub _show_version
{
    print "$main::VERSION\n";
    exit 0;
}


1;

__END__

=head1 NAME

AppConfig::Std - subclass of AppConfig that provides standard options

=head1 SYNOPSIS



( run in 3.458 seconds using v1.01-cache-2.11-cpan-98e64b0badf )