#!perl

our $DATE = '2014-10-24'; # DATE
our $VERSION = '0.03'; # VERSION

use 5.010;
use strict;
use warnings;

our %SPEC;

$SPEC{single} = {
    v => 1.1,
    summary => 'A function',
    description => <<'_',

Just a dummy description. Just a dummy description. Yup, just a dummy
description. Just a dummy description. Just a dummy description. Yeah, just a
dummy description. Just a dummy description.

_
    args => {
        arg1 => {
            schema => 'int*',
            pos => 0,
            cmdline_aliases => { a=>{} },
        },
        arg2 => {
            schema => 'int*',
            pos => 1,
            cmdline_aliases => { b=>{} },
        },
        op => {
            summary => 'Operation to apply to arg1 & arg2',
            schema  => ['str*', in => [qw/multiply subtract add divide/]],
            req     => 1,
            cmdline_aliases => {
                minus => { schema=>'bool', summary => 'Alias for --op=subtract', code=>sub {$_[0]{op} = 'minus'} },
            },
        },
    },
};
sub single {
    my %args = @_;

    # we need to do validation ourselves because P::C::Lite currently doesn't do
    # it for us.
    my $op = $args{op} or return [400, "Please specify op"];
    $op =~ /\A(multiply|subtract|add|divide)\z/ or return [400, "Invalid op"];
    my $a1 = $args{arg1}; !defined($a1) || $a1 =~ /\A[+-]?\d+\z/
        or return [400, "Invalid arg1 (not an integer)"];
    my $a2 = $args{arg2}; !defined($a2) || $a2 =~ /\A[+-]?\d+\z/
        or return [400, "Invalid arg1 (not an integer)"];

    if ($op eq 'multiply') {
        defined($a1) && defined($a2) or return [400, "arg1 & arg2 required"];
        [200, "OK", $a1 * $a2];
    } elsif ($op eq 'subtract') {
        defined($a1) && defined($a2) or return [400, "arg1 & arg2 required"];
        [200, "OK", $a1 - $a2];
    } elsif ($op eq 'add') {
        defined($a1) && defined($a2) or return [400, "arg1 & arg2 required"];
        [200, "OK", $a1 + $a2];
    } elsif ($op eq 'divide') {
        defined($a1) && defined($a2) or return [400, "arg1 & arg2 required"];
        $a2 == 0 and return [500, "Division by zero"];
        [200, "OK", $a1 / $a2];
    }
}

use Perinci::CmdLine::Any;
Perinci::CmdLine::Any->new(url=>'/main/single')->run;

# ABSTRACT: Test a single function (no subcommands)
# PODNAME: peri-eg-single-any

__END__

=pod

=encoding UTF-8

=head1 NAME

peri-eg-single-any - Test a single function (no subcommands)

=head1 VERSION

This document describes version 0.03 of peri-eg-single-any (from Perl distribution Perinci-Examples-Bin-Any), released on 2014-10-24.

=head1 SYNOPSIS

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Perinci-Examples-Bin-Any>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-Perinci-Examples-Bin-Any>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Examples-Bin-Any>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
