App-Chained

 view release on metacpan or  search on metacpan

lib/App/Chained.pm  view on Meta::CPAN

	}
else
	{
	$self->{INTERACTION}{DIE}->("Invalid argument '$valid_options'!") ;
	}

my %options = @options ;

for my $option_name (keys %options)
	{
	unless(exists $valid_options->{$option_name})
		{
		$self->{INTERACTION}{DIE}->
				(
				"$self->{NAME}: Invalid Option '$option_name' at '$self->{FILE}:$self->{LINE}'\nValid options:\n\t"
				.  join("\n\t", sort keys %{$valid_options}) . "\n"
				);
		}
	}

if
	(
	   (defined $options{FILE} && ! defined $options{LINE})
	|| (!defined $options{FILE} && defined $options{LINE})
	)
	{
	$self->{INTERACTION}{DIE}->("$self->{NAME}: Incomplete option FILE::LINE!") ;
	}

return(1) ;
}

#-------------------------------------------------------------------------------

sub parse_command_line
{

=head2 [P]parse_command_line()

Parses the option passed in the throught the named argument B<command_line_arguments>.  It will also handle some 
of the options directly, eg: --help, --apropos, ...

I<Arguments> - None

I<Returns> - Nothing

B<$self->{parsed_command}> is set to the command to run.

B<$self->{command_options}> is set to the options that are to be passed to the command

I<Exceptions> -Dies if an invalid command is passed in the options, warns if the options seem incorrect

=cut

my ($self) = @_ ;

my @command_line_arguments = @{$self->{command_line_arguments}} ;

if(@command_line_arguments)
	{
	local @ARGV = @command_line_arguments ;
	
	my @option_definitions = $self->get_options_definitions() ;
	GetOptions(@option_definitions);
	
	my @arguments_left_on_command_line = @ARGV ;
	
	my $command = shift @arguments_left_on_command_line ;
	my $options_ok = defined $command ? $command !~ /^-/sxm : 0 ;
	
	if($options_ok)
		{
		$self->{parsed_command} = $command ;
		$self->{command_options} = \@arguments_left_on_command_line ;
		}
		
	# run help, faq apropos, ... even if the command line was wrong
					
					
	if(${$self->{getopt_definitions}{h}} ||  ${$self->{getopt_definitions}{help}})
		{
		if(defined $command)
			{
			my $command_index = first_index {/$command/} @{$self->{command_line_arguments}} ;
			my $help_index = first_index {/-(h|help)/} @{$self->{command_line_arguments}} ;
			
			if($command_index < $help_index)
				{
				# the --help comes after the command. let the command handle it
				$self->run_help_command($command) ;
				exit(0) ;
				}
			else
				{
				$self->display_help() ;
				exit(0) ;
				}
			}
		else
			{
			$self->display_help() ;
			exit(0) ;
			}
		}
		
	if(${$self->{getopt_definitions}{version}})
		{
		$self->display_version() ;
		exit(0) ;
		}
		
	if(${$self->{getopt_definitions}{'apropos=s'}})
		{
		$self->display_apropos() ;
		exit(0) ;
		}
		
	if(${$self->{getopt_definitions}{'faq=s'}})
		{
		$self->display_faq() ;
		exit(0) ;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.500 second using v1.00-cache-2.02-grep-82fe00e-cpan-d29e8ade9f55 )