Data-Conveyor

 view release on metacpan or  search on metacpan

lib/Data/Conveyor/Service/Interface/Shell.pm  view on Meta::CPAN

    }
    if (length $example_pod) {
        $example_pod = "=over 4\n\n$example_pod\n\n=back\n\n";
    }
    if ($example_count == 1) {
        $example_pod = "Example:\n\n$example_pod";
    } elsif ($example_count > 1) {
        $example_pod = "Examples:\n\n$example_pod";
    }
    $example_pod;
}

sub pod_to_text {
    my ($self, $pod) = @_;
    open my $pod_fh, '<', \$pod
      or die "can't open filehandle to scalar \$pod";
    my $text = '';
    open my $text_fh, '>', \$text
      or die "can't open filehandle to scalar \$text";
    my $parser = Pod::Text->new;
    $parser->parse_from_filehandle($pod_fh, $text_fh);
    close $pod_fh  or die "can't close filehandle to scalar \$pod";
    close $text_fh or die "can't close filehandle to scalar \$text";
    $text;
}

sub summary_for_service_method {
    my ($self, $method) = @_;
    $self->svc->get_summary_for_method($method);
}

# don't call this just "help_for_service_method", or Term::Shell's
# find_handler() will find it and assume that there's a command
# "for_service_method".
sub get_help_for_service_method {
    my ($self, $method) = @_;
    my $description = $self->svc->get_description_for_method($method);
    my $param_help  = $self->get_param_help_for_method($method);
    my $example_pod = $self->get_example_help_for_method($method);
    my $pod         = <<EOPOD;
=pod

$method

$description

$param_help

$example_pod

=cut
EOPOD
    $self->pod_to_text($pod);
}

# Don't call this run_service_method, or Term::Shell will think it's a
# command.
sub execute_service_method {
    my $self   = shift;
    my $method = shift;
    local @ARGV = @_;
    my %opt;
    GetOptions(\%opt, $self->getopt_spec_for_method($method))
      or return $self->run_help($method);
    if (@ARGV) {
        print "extraneous arguments [@ARGV]\n\n";
        return $self->run_help($method);
    }
    my $params = $self->svc->get_params_for_method($method);

    # if there's a mandatory 'ticket' param, it defaults to the current ticket
    # number
    if ((   grep {
                     $_->{name} eq 'ticket'
                  && $_->{necessity} eq $self->delegate->SIP_MANDATORY
            } @$params
        )
        && !(defined $opt{ticket})
      ) {
        my $ticket_no = $self->ticket_no;
        unless ($ticket_no) {
            print
              "--ticket not given and there is no current ticket number.\n\n";
            return $self->run_help($method);
        }
        $opt{ticket} = $ticket_no;
    }

    # if there's a 'limit' param, it defaults to the current limit
    if ((grep { $_->{name} eq 'limit' } @$params) && !(defined $opt{limit})) {
        $opt{limit} = $self->limit;
    }

    # check other mandatory parameters
    my @params = $self->svc->get_params_for_method($method);
    for my $param (@params) {
        next if defined $opt{ $param->{name} };

        # If the method only has one parameter and there is something left in
        # @ARGV (unparsed by GetOptions), assume it's that parameter's value.
        #
        # This way, you can say "somecmd somevalue" instead of "somecmd -d
        # somevalue" if "-d" is the only arguments. It's just a little bit
        # more convenient and intuitive.
        if ((@params == 1) && (@ARGV >= 1)) {
            $opt{ $param->{name} } = shift @ARGV;
            next;
        }
        next unless $param->{necessity} eq $self->delegate->SIP_MANDATORY;
        print "missing mandatory parameter [$param->{name}]\n\n";
        return $self->run_help($method);
    }
    $self->svc->apply_param_aliases_and_defaults($method, \%opt);
    try {
        $self->print_result($self->svc->run_method($method, %opt));
    }
    catch Data::Conveyor::Exception::ServiceMethodHelp with {
        print $_[0]->custom_message . "\n\n";
        $self->run_help($method);
    }
    catch Error with {



( run in 2.884 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )