App-Chained

 view release on metacpan or  search on metacpan

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

	exit(0) ;
	}
	
return ;
}

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

sub run
{

=head2 [P]run()

Runs the sub command parsed on the command line.

I<Arguments> - None

I<Returns> - Nothing

I<Exceptions> Dies if the sub command B<run> field is improperly set.

=cut

my ($self) = @_ ;

return unless defined $self->{parsed_command} ;

my $sub_app = $self->{sub_apps}{$self->{parsed_command}} ;

if(defined $sub_app->{run})
	{
	if('CODE' eq ref($sub_app->{run}))
		{
		my @arguments ;
		@arguments = map {"'$_'"} @{$self->{command_options}} if(defined $self->{command_options}) ;
		
		$sub_app->{run}($self, $sub_app, \@arguments) ;
		}
	else
		{
		$self->{INTERACTION}{DIE}->("Error: sub app '$self->{parsed_command}' run subroutine is not a code reference.") ;
		}
	}
else
	{
	$self->{INTERACTION}{DIE}->("Error: sub app '$self->{parsed_command}' run subroutine is not defined.") ;
	}

return ;
}

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

sub generate_bash_completion
{
	
=head2 [P]generate_bash_completion()

The generated completion is in two parts:

A perl script used to generate  the completion (output on stdout) and a shell script that you must source (output on stderr).

 $> my_app -bash 1> my_app_perl_completion.pl 2> my_app_regiter_completion

Direction about how to use the completion scritp is contained in the generated script.

The completion will work for the top application till a command is input on the command line after that the completion is for the command.

=head3 command specific options

Your sub commands can define an B<options> field. The field should be set to a subroutine reference that returns a string of options the sub command
accepts. The format should be I<-option_name>. One option perl line.

Here is an example of how I added completion to a set sub commands (8 of them). The sub commands do not have a completion script
and rely on the wrapper for completion. 

I first set the B<options> field:

	{
	description => ...
	run => ...
	...
	
	options => sub {return `$name --dump_options`},
	}

I am using the sub command itself to generate the options. This way I don't have to maintain the list by hand (which is possible).

Modifying the sub command itself was trivial and very quick. I modified the following code (example in one of thesub commands)

  die 'Error parsing options!'unless 
    GetOptions
      (
      'master_template_file=s' => \$master_template_file,
      'h|help' => \&display_help, 
      ) ;
      
to  be

  die 'Error parsing options!'unless 
    GetOptions
        (
        'master_template_file=s' => \$master_template_file,
        'h|help' => \&display_help, 
	
       	'dump_options' => 
	  sub 
	  {
	  print join "\n", map {"-$_"} 
	      qw(
		master_template_file
		help
		) ;
	  exit(0) ;
          },
	) ;

Modfying the height or so scripts took only a few minutes.

Noiw I have command completion for all the sub command. Here is an example:



( run in 0.681 second using v1.01-cache-2.11-cpan-39bf76dae61 )