<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package RegExArgvParser;

use strict;
use warnings;
use Data::Dumper;
use File::Basename;

my $defined_param										=		{	"script-name"				=&gt;	"Name of the script",
																	"script-created-by"			=&gt;	"Script owner name",
																	"script-created-date"		=&gt;	"Script creation date",
																	"script-version-no"			=&gt;	"Version number of the script over SVN",
																	"script-tag-no"				=&gt;	"Script release tag number associated with SVN",
																	"script-description"		=&gt;	"Description about the script",
																	"script-license-agreement"	=&gt;	"End user license agreement of the script",
																	"short-name"				=&gt;	"Short name for input option argument",
																	"full-name"					=&gt;	"Full name for input option argument",
																	"description"				=&gt;	"Description about the input option argument",
																	"option"					=&gt;	"Input option is optional or required to check",
																	"type"						=&gt;	"Input argument is scalar or array type",
																	"regex-check"				=&gt;	"User spcific regex for input validation",
																	"default"					=&gt;	"Default value for user input",
																	"error-message"				=&gt;	"User defined error message to display"				
																};

sub new() {
    my $class																					=		shift;+
    my $self																					=		{	"script-details"	=&gt;	__check_supported_param( @_ )	};
    return bless $self, $class;
}


sub __check_supported_param() {
	my $param																					=		{ @_ };
	foreach ( keys %{ $param } ) {
		die ( "ArgumentParser - \($_\) is not supported input argument !" ) if( not defined $defined_param-&gt;{ $_ } );
		if(not $param-&gt;{'script-name'}) {
			$param-&gt;{ 'script-name' }															=		basename($0);
		}
	}
	return $param;
}


sub add_argument() {
	my $self                  																	=		shift;
	my $param																					=		{ @_ };
	
	die "ArgumentParser - User input argument option name are required !" if not $param-&gt;{ 'short-name' } and not $param-&gt;{ 'full-name' };
	die "ArgumentParser - Variable name for user input argument are required !" if not $param-&gt;{ 'variable-name' };
	
	$self-&gt;{ 'argument' }{ 'short-name' }{ $param-&gt;{ 'short-name' } }							=		$param-&gt;{ 'variable-name' } if( $param-&gt;{ 'short-name' } );
	$self-&gt;{ 'argument' }{ 'full-name' }{ $param-&gt;{ 'full-name' } }								=		$param-&gt;{ 'variable-name' } if( $param-&gt;{ 'full-name' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'short-name' }			=		$param-&gt;{ 'short-name' } if( $param-&gt;{ 'short-name' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'full-name' }				=		$param-&gt;{ 'full-name' } if( $param-&gt;{ 'full-name' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'description' }			=		$param-&gt;{ 'description' } if( $param-&gt;{ 'description' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'option' }				=		$param-&gt;{ 'option' } if( $param-&gt;{ 'option' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'regex-check' }			=		$param-&gt;{ 'regex-check' } if( $param-&gt;{ 'regex-check' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'error-message' }			=		$param-&gt;{ 'error-message' } if( $param-&gt;{ 'error-message' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'type' }					=		$param-&gt;{ 'type' } if( $param-&gt;{ 'type' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'default' }				=		$param-&gt;{ 'default' } if( $param-&gt;{ 'default' } );
	$self-&gt;{ 'argument' }{ 'detail' }{ $param-&gt;{ 'variable-name' } }{ 'value' }					=		undef;
}


sub parse_argument() {
	my $self															=		shift;
	
	if ( scalar( @ARGV ) == 0 ) {
		print "Insufficient input parameters\n";
		defined $self-&gt;{ 'script-details' }{ 'script-name' } ? print "For more information, please type :&gt;$self-&gt;{ 'script-details' }{ 'script-name' } -h or --help\n" : print "For more information, please type :&gt;script-name -h or --help\n";
		exit();
	}
	
	#
	# assign user input into defined argument
	#
	foreach ( my $input_index=0; $input_index &lt;= $#ARGV; $input_index++ ) {
		$self-&gt;help() if ( ( $ARGV[0] eq '-h' ) or ( $ARGV[0] eq '--help') );
		
		last if( $input_index &gt;= scalar(@ARGV) );
		
		my $first_option																		=		__get_defined_option_name_type($self, $ARGV[$input_index]);
		my $second_option																		=		__get_defined_option_name_type($self, $ARGV[$input_index + 1]);
		
		if( $first_option ) {
			my $option_variable_name															=		$self-&gt;{ 'argument' }{ $first_option }{ $ARGV[$input_index] };
			
			if ( $second_option ){
				if ($self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'default' }) {
					if( $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'type' } eq 'array' ) {
						$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' }	=		[ split (/,/, $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'default' }) ];
					}
					else {
						$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' }	=		$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'default' }
					}
				}
				next;	
			}
			
			if( $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'type' } eq 'array' ) {
				$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' }			=		[ split (/,/, $ARGV[$input_index + 1]) ] if(defined $ARGV[$input_index + 1]);
				if( ( not defined $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' } ) &amp;&amp; ( $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'default' }) ) {
					$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' }		=		[ split (/,/, $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'default' }) ];
				}
			}	
			else {
				$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' }			=		$ARGV[$input_index + 1] if(defined $ARGV[$input_index + 1]);
				if( ( not defined $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' } ) &amp;&amp; ( $self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'default' }) ) {
					$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'value' }		=		$self-&gt;{ 'argument' }{ 'detail' }{ $option_variable_name }{ 'default' };
				}
			}
		} 
		else {
			print "Invalid input parameters\n";
			defined $self-&gt;{ 'script-details' }{ 'script-name' } ? print "For more information, please type :&gt;$self-&gt;{ 'script-details' }{ 'script-name' } -h or --help\n" : print "For more information, please type :&gt;script-name -h or --help\n";
			exit();
		}
		
		$input_index++;
	}
	
	#
	# check arugment constraints
	#
	my $result																					=		{};
	foreach ( keys %{ $self-&gt;{ 'argument' }{ 'detail' } } ) {
		#
		#Check required || optional input
		#
		if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'option' } eq 'required' ) {
			if( not $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'value' } ) {
				if( not $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'error-message' } ) {
					print "Option (";
					print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } );
					print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } );
					print " ) value is required\n";
					defined $self-&gt;{ 'script-details' }{ 'script-name' } ? print "For more information, please type :&gt;$self-&gt;{ 'script-details' }{ 'script-name' } -h or --help\n" : print "For more information, please type :&gt;script-name -h or --help\n";
					exit();
				}
				else {
						print $self-&gt;{ 'script-details' }{ 'script-name' }."\n";
						print "Option (";
						print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } );
						print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } );
						print " ), ";
						print $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'error-message' }."\n";
						exit();
				}
			}
		}
		
		
				
		#
		#Check for regex
		#
		if( ( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'regex-check' } ) &amp;&amp; ($self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'value' }) ) {
			my $found_regex_error																=		undef;
			if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'type' } eq 'array' ) {
				foreach my $input_value ( @{$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'value' }} ) {
					
					my $regex_result															=		eval {
																											if( $input_value =~ m/$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'regex-check' }/gsi ) {
																												$found_regex_error	=	undef;
																											}
																											else {
																												$found_regex_error	=	"FOUND";
																											}
																										};
					last if( $found_regex_error );
				}
			}
			else {
				my $regex_result															=		eval {
																											if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'value' } =~ m/$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'regex-check' }/gsi ) {
																												$found_regex_error	=	undef;
																											}
																											else {
																												$found_regex_error	=	"FOUND";
																											}
																										};
			}
			if( $found_regex_error ) {
				if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'error-message' } ) {
					print $self-&gt;{ 'script-details' }{ 'script-name' }."\n";
					print "Option (";
					print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } );
					print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } );
					print " ), ";
					print $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'error-message' }."\n";
					exit();
				}
				else {
					print $self-&gt;{ 'script-details' }{ 'script-name' }."\n";
					print "Option (";
					print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'short-name' } );
					print " ".$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } if( $self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'full-name' } );
					print " ) - value is required\n";
					print $@;
					exit();
				}
			}
		}
		
		$result-&gt;{ $_ }																			=		$self-&gt;{ 'argument' }{ 'detail' }{ $_ }{ 'value' };
	}
	
	#print Dumper $result;
	print Dumper $result;
	return $result;
}


sub __get_defined_option_name_type() {
	my $self																					=		shift;
	my $option																					=		shift;
	
	if( defined $option ) {
		return "short-name" if( $self-&gt;{ 'argument' }{ 'short-name' }{ $option } );
		return  "full-name" if( $self-&gt;{ 'argument' }{ 'full-name' }{ $option } );
	}
	
	return undef;
}


sub help(){
	my ($self)																					=		@_;
	
	print "Script Name        : $self-&gt;{'script-name'}\n" if defined $self-&gt;{'script-name'};
	print "Version No.        : $self-&gt;{'script-version-no'}\n" if defined $self-&gt;{'script-version-no'};
	print "Tag No.            : $self-&gt;{'script-tag-no'}\n" if defined $self-&gt;{'script-tag-no'};
	print "Created by         : $self-&gt;{'script-created-by'}\n" if defined $self-&gt;{'script-created-by'};
	print "Created date       : $self-&gt;{'script-created-date'}\n" if defined $self-&gt;{'script-created-date'};
	print "Description        : $self-&gt;{'script-description'}\n" if defined $self-&gt;{'script-description'};
	
	if ( keys %{$self-&gt;{ 'argument' }} ) {
		print "Options:\n";
		foreach ( keys %{$self-&gt;{ 'argument' }} ) {
			print $self-&gt;{ 'argument' }{ $_ }{ 'short-name' }.' | '.$self-&gt;{ 'argument' }{ $_ }{ 'full-name' };
			print " " x ( 30 - ( length($self-&gt;{ 'argument' }{ $_ }{ 'short-name' }) + length($self-&gt;{ 'argument' }{ $_ }{ 'full-name' }) ) );
			print $self-&gt;{ 'argument' }{ $_ }{ 'description' }."\n";
		}
	}
	
	print "License agreement  : $self-&gt;{'script-license-agreement'}\n" if defined $self-&gt;{'script-license-agreement'};
}


1;</pre></body></html>