#!usr/bin/perl

=head1 NAME

GetOptionsExample.pl - This program describes how to utilize the 
GetOptions Perl Module

=head1 SYNOPSIS
 
This program takes as input a variety of options to demonstrate how 
the GetOptions module works within your perl code. 

=head1 USAGE

Usage: GetOptionsExample.pl [OPTIONS]

=head1 Optional Arguments:

Displays the quick summary of the program options.

=head2 --num NUMBER

This option takes the number NUMBER and if it is truely is a number 
will print it out to the command line. 

=head2 --string STRING

This option takes the string STRING and if it is truely is a string
will print it out to the command line. 

=head2 --flag

This option sets the $flag variable to one if 

=head2 --file FILE

This option is similar to --string only the string is a file

=head2 --version

Displays the version information.


=head2 --help

Displays the quick summary of program options.

=head1 OUTPUT

Comments on the options that were set

=head1 SYSTEM REQUIREMENTS

=over

=item * Perl (version 5.8.5 or better) - http://www.perl.org

=back

=head1 CONTACT US

    If you have trouble installing and excecuting GetOptionsExample.pl, 
    please contact us at
    
    btmcinnes at vcu dot edu.

=head1 Author

 Bridget T. McInnes, Virginia Commonwealth University

=head1 COPYRIGHT

Copyright (c) 2015

 Bridget T. McInnes, Virginia Commonwealth University 
 bthomson at vcu dot edu
                     
This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the Free 
Software Foundation; either version 2 of the License, or (at your option) 
any later version.

This program is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with 
this program; if not, write to:

 The Free Software Foundation, Inc.,
 59 Temple Place - Suite 330,               
 Boston, MA  02111-1307, USA.          
  
=cut

##########################################################################

                        #   CODE STARTS HERE

##########################################################################
#  reference the getOption cpan page

use Getopt::Long;

eval(GetOptions( "version", "help", "num=i", "string=s", "flag", "file=s")) or die ("Please check the above mentioned option(s).\n");

#   if help is defined, print out help
if( defined $opt_help ) {
    $opt_help = 1;
    &showHelp;
    exit;
}

# if version is requested, show version
if( defined $opt_version ) {
    $opt_version = 1;
    &showVersion();
    exit;
}

print "--num NUMBER\n";
if(defined $opt_num) { 
    print "  You have defined --num as $opt_num\n\n";
}
else { 
    print "  You did not define the --num option\n\n";
}

print "--string STRING\n";
if(defined $opt_string) { 
    print "  You have defined --string as $opt_string\n\n";
}
else { 
    print "  You did not define the --string option\n\n";
}

print "--flag\n";
if(defined $opt_flag) { 
    print "  You have defined the --flag option ($opt_flag)\n\n";
    print '  You have defined the --flag option ($opt_flag)\n\n';
    print "\n";
}
else { 
    print "  You did not define the --flag option ($opt_flag)\n\n";
}

print "--file FILE\n";
if(defined $opt_file) { 
    if(! (-e $opt_file)) { 
	print "  You have defined --file $opt_file but $opt_file does not exist\n\n"; 
	&askHelp(); 
	exit; 
    }
    else { 
	print "  You have defined --file $opt_file and it exists and can be opened\n\n"; 
    }
}
else { 
    print "  You did not define the --file option\n\n";
}




##############################################################################                 

#  function to output help messages for this program                                         

##############################################################################                

sub showHelp() {

    print "This is a utility that provides an example of how to set\n";
    print "and use the GetOptions module for the programs written in\n"; 
    print "our lab -- this includes the help and showVersion information\n\n";

    print "Usage: GetOptionsExample.pl [OPTIONS] \n\n";

    print "OPTIONS:\n\n";

    print "--num NUMBER           This option takes the number NUMBER \n\n";

    print "--string STRING        This option takes the string STRING \n\n"; 

    print "--flag                 This option sets the $flag (binary)\n\n";

    print "--file FILE            This option is similar to --string only\n";
    print "                       the string is a file\n\n";

    print "--version                Prints the version number\n\n";

    print "--help                   Prints this help message.\n\n";
}

##############################################################################            

#  function to output the version number                                                   

##############################################################################              

sub showVersion {
    print '$Id: GetOptionsExample.pl,v 1.0 2015/10/02 11:17 btmcinnes Exp $';
    print "\nCopyright (c) 2015- Bridget McInnes\n"; 
}

##############################################################################              

#  function to output "ask for help" message when user's goofed                              

##############################################################################               

sub askHelp {
    print STDERR "Type GetOptionsExample.pl --help for help.\n";
}



