Goo::CommandLineManager - Manage command line parameters


Goo documentation Contained in the Goo distribution.

Index


Code Index:

NAME

Top

Goo::CommandLineManager - Manage command line parameters

SYNOPSIS

Top

use Goo::CommandLineManager;

DESCRIPTION

Top

Manage command line arguments.

METHODS

Top

new

constructor

get_last_parameter

pop off the last parameter on the command line

get_parameters

return all the parameters on the command line

get_parameter

return the parameter at a given position

get_selected_option

return the switch that is specified

add_option

add an option to manage on the command line

get_switch

return the value of the switch

show_help

display the help for all the command options

AUTHOR

Top

Nigel Hamilton <nigel@trexy.com>

SEE ALSO

Top


Goo documentation Contained in the Goo distribution.

package Goo::CommandLineManager;

###############################################################################
# Nigel Hamilton
#
# Copyright Nigel Hamilton 2004
# All Rights Reserved
#
# Author: 		Nigel Hamilton
# Filename:		Goo::CommandLineManager.pm
# Description: 	Manage command line parameters
#
# Date	 		Change
# -----------------------------------------------------------------------------
# 31/10/2004	Auto generated file
# 31/10/2004	Needed to reuse handling command lines
#
###############################################################################

use strict;
use Goo::Object;

use base qw(Goo::Object);


###############################################################################
#
# new - constructor
#
###############################################################################

sub new {

	my ($class, @parameters) = @_;

	my $this = $class->SUPER::new();

   	# the first parameter is the switch
    $this->{switch} 	= shift(@parameters);
    $this->{switch} 	=~ s/\-//g;
    $this->{parameters} = \@parameters;

	return $this;

}


###############################################################################
#
# get_last_parameter - pop off the last parameter
#
###############################################################################

sub get_last_parameter {
	
	my ($this) = @_;

	return pop @{ $this->{parameters} };
	
}


###############################################################################
#
# get_parameters - return all the parameters
#
###############################################################################

sub get_parameters {
	
	my ($this) = @_;

	return @{ $this->{parameters} };
	
}


###############################################################################
#
# get_parameter - return an option that corresponds to the right switch
#
###############################################################################

sub get_parameter {
	
	my ($this, $order) = @_;

	$order--;

	#print join("<---array \n", @{ $this->{parameters} });

	my $parameter = @{ $this->{parameters} }[$order];
	
	#print "parameter -- $order === --->$parameter<--\n";
	
	return $parameter;

}


###############################################################################
#
# get_selected_option - return an option that corresponds to the right switch
#
###############################################################################

sub get_selected_option {

	my ($this) = @_;

        my $switch = $this->{switch};

        $switch =~ s/\-//g;

        return $this->{switch};

}


###############################################################################
#
# add_option - add an option to manage on the command line
#
###############################################################################

sub add_option {

	my ($this, $option) = @_;

	# add the switch to this object
	$this->{options}->{$option->get_short_label()} = $option;
        $this->{options}->{$option->get_long_label()}  = $option;

}


###############################################################################
#
# get_switch - return the value of the switch
#
###############################################################################

sub get_switch {

	my ($this) = @_;

        return $this->{switch};

}


###############################################################################
#
# show_help - display the help for all the command options
#
###############################################################################

sub show_help {

	my ($this) = @_;

        foreach my $option (sort keys %{$this->{options}}) {

                # print "option ==== $option \n";
                print "\t\t-$option      \t".$this->{options}->{$option}->get_help()."\n";
        }

}


1;


__END__