CLI-Dispatch

 view release on metacpan or  search on metacpan

t/lib/CLIDTestClass/Directly/Help.pm  view on Meta::CPAN

package CLIDTestClass::Directly::Help;

use strict;
use warnings;
use Test::Classy::Base;
use File::Spec;
use Try::Tiny;

sub help_command : Test {
  my $class = shift;

  my $ret = $class->dispatch(qw( help ));

  ok $ret eq 'help', $class->message('help command is ignored');
}

sub help_option : Tests(2) {
  my $class = shift;

  my $ret = $class->dispatch(qw( --help ));

  ok $ret =~ /single command test/, $class->message('has description');
  ok $ret !~ /help me/, $class->message('name section is removed');
}

sub dispatch {
  my $class = shift;

  local @ARGV = @_;

  open my $null, '>', File::Spec->devnull;
  my $stdout = select($null);

  my $ret;
  try   { $ret = CLIDTest::Directly::HelpMe->run_directly }
  catch { $ret = $_ || 'Obscure error' };

  select($stdout);

  return $ret;
}

no warnings 'redefine';

package #
  CLIDTest::Directly::HelpMe;

use base qw( CLI::Dispatch::Command );

sub options {qw( option=s )}

sub run {
  my ($self, @args) = @_;

  my $text;
  if ( @args ) {
    $text = join '', @args;
  }
  elsif ( $self->option('option') ) {
    $text = $self->option('option');
  }
  else {
    $text = 'no args';
  }

  return $text;
}



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