#!/usr/bin/perl -w
#
# Script test_getopts.pl
#
#
# COMMAND: test.pl -abc argument1 
#
# This program has three available options: 
# -v: to print messages
# -a
# -b 
# -c number 
# and only one argument which is printed at the standard output
#
# Once you have read it, try the following commands to understand:
#
#    (1) ./test_getopts.pl pepe.txt
#    (2) ./test_getopts.pl -v pepe.txt
#    (3) ./test_getopts.pl -va pepe.txt
#    (4) ./test_getopts.pl -vab pepe.txt
#    (5) ./test_getopts.pl -vabc 0.75 pepe.txt
#    (6) ./test_getopts.pl pepe.txt maria.txt
#    (7) ./test_getopts.pl -d maria.txt
#
# by Enrique Blanco, February 2002
#####################################################################################

use strict;
# Use this module where it is implemented the getopts function
use Getopt::Std;

# Hash containing the options selected by the user
my %opt;
# Parameter number one (it could be a filename)
my $param_one;
my $n_params;

#####################################################
# STEP 1: reading options
#####################################################

# Between quotations, you must put the options available to select
# If other options are selected by the user the program stops!
# As the option 'c' needs a value, type "c:"
(getopts('vabc:',\%opt)) or die("Problems reading options, try again, please\n");      

# Using the routine to print info about the status of the program
# notice that "\n" is not needed!
print_mess("Loading options: done! ");

#####################################################
# STEP 2: reading parameters
#####################################################
$n_params = scalar(@ARGV);

# if there are more than on file, quit the program
($n_params == 1) or die (print_error("Wrong number of parameters ($n_params)!\n"));
($param_one) = $ARGV[0];

# write on the screen the name of the parameter one
print ("First argument is... ",$param_one,"\n");

print_mess("Loading parameters: done! ");

#####################################################
# STEP 3: doing things according to the options selected
#####################################################

if (exists($opt{a}))
{
	# doing things for the "a" option
	print_mess("OPTION a: selected!");
}
if (exists($opt{b}))
{
	# doing things for the "b" option
	print_mess("OPTION b: selected!");
}
if (exists($opt{c}))
{
	# doing things for the "c" option (the associated value is at $opt{c}
	print_mess("OPTION c: selected and associated value is $opt{c}!");
}


#####################################################
# Finish successfully the program execution
#####################################################
print_mess("Everything well done. Exit!\n");
exit(0);
           
############ Subroutines to print messages (info) or errors(break the execution)

# INFO: print information about different stages of the processing
# The info is always send to the Standard Error!
# option "v" (verbose) allows to see the messages on the screen
sub print_mess
{
	my @mess = @_;

	print STDERR ("\t: @mess \n") if (exists($opt{v}));
}

# ERROR: print message and quit the program
sub print_error
{
	my @mess = @_;

	print STDERR ("\t: @mess \n") and exit();
}







