#!perl

## no critic: TestingAndDebugging::RequireUseStrict

# IFUNBUILT
# use strict;
# use warnings;
# END IFUNBUILT

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2023-11-23'; # DATE
our $DIST = 'App-errno'; # DIST
our $VERSION = '0.071'; # VERSION

use Getopt::Long::EvenLess;

sub _help {
        print <<'_';
Usage:
  errno <num>
  errno <search string>
  errno --list (-l)
  errno --help (-h, -?)
  errno --version (-v)
_
}

my @errnos; # [name, numeric code, message]
{
    require Errno;
    no strict 'refs';
    for (sort @Errno::EXPORT_OK) {
        my $num = &{"Errno::$_"};
        local $! = $num;
        push @errnos, [$_, $num, "$!"];
    }
    @errnos = sort { $a->[1] <=> $b->[1] } @errnos;
}

GetOptions(
    'help|h|?' => sub {
        _help();
        exit 0;
    },
    'version|v' => sub {
        no warnings 'once';
        print "errno version $main::VERSION\n";
        exit 0;
    },
    'list|l' => sub {
        for (@errnos) { print "$_->[0]\t$_->[1]\t$_->[2]\n" }
        exit 0;
    },
);

if (@ARGV != 1) {
    _help();
    exit 1;
}

if ($ARGV[0] =~ /\A\d+\z/) {
    local $! = +$ARGV[0];
    my $str = "$!";
    print $str, "\n";
    exit $str =~ /unknown error/i ? 1:0;
} else {
    my $search = lc($ARGV[0]);
    my $found;
    for (@errnos) {
        if (index(lc($_->[0]), $search) >= 0 ||
                index(lc($_->[2]), $search) >= 0) {
            print "$_->[0]\t$_->[1]\t$_->[2]\n";
            $found++;
        }
    }
    exit($found ? 0 : 1);
}

# ABSTRACT: List/show errno
# PODNAME: errno

__END__

=pod

=encoding UTF-8

=head1 NAME

errno - List/show errno

=head1 VERSION

This document describes version 0.071 of errno (from Perl distribution App-errno), released on 2023-11-23.

=head1 SYNOPSIS

Show the error message string for a certain errno code:

 % errno 23

Search error message string:

 % errno file
 % errno ENOT

List all known errno's on your system:

 % errno -l

=head1 DESCRIPTION

This script can be used to show the error message of a particular C<errno>, or
list all available errno's on your system. It is basically a simple CLI for the
L<Errno>.pm Perl module.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-errno>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-errno>.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 CONTRIBUTING


To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.

Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023, 2015 by perlancar <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.

=head1 BUGS

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

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.

=cut
