#!perl

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2021-07-17'; # DATE
our $DIST = 'App-seq-intrange'; # DIST
our $VERSION = '0.001'; # VERSION

use strict;
use warnings;
use Getopt::Long;

use IntRange::Iter qw(intrange_iter);

my %Opts = (
    separator => "\n",
    format => undef,
    equal_width => 0,
);
Getopt::Long::Configure('bundling', 'no_ignore_case',
                        # because we want to allow negative numbers without error
                        'pass_through',
                    );
GetOptions(
    'help|h|?' => sub {
        print <<'_';
Usage: seq-intrange [options] <intrange>
       seq-intrange --help, -h, -?
       seq-intrange --version, -v

Examples:
  % seq-intrange 1,5-10,15

Options:
  --format=s, -f
  --equal-width, -w
  --separator=s, -s

_
        exit 0;
    },
    'version|v' => sub {
        no warnings 'once';
        print "seq-intrange version ", ($main::VERSION || "dev"),
            ($main::DATE ? " ($main::DATE)" : ""), "\n";
        exit 0;
    },
    'format|f=s' => \$Opts{format},
    'equal-width|w' => \$Opts{equal_width},
    'separator|s=s' => \$Opts{separator},
);

my $re_num = qr/\A-?\d+(\.\d+)?\z/;

@ARGV == 1 or die "seq-intrange: Please specify intrange specification\n";
my $format;
my $iter = intrange_iter($ARGV[0]);
while (defined (my $x = $iter->())) {
    unless (defined $format) {
        $format = $Opts{format};
        if (!defined $format && $Opts{equal_width}) {
            $format = "%04". # XXX we need to extract the largest number
                "d";
        }
        if (!defined $format) {
            $format = "%s";
        }
    }
    printf "${format}%s", $x, $Opts{separator};
}

# ABSTRACT: Like seq, but accepts intrange specification (e.g. 1,5-10,15)
# PODNAME: seq-intrange

__END__

=pod

=encoding UTF-8

=head1 NAME

seq-intrange - Like seq, but accepts intrange specification (e.g. 1,5-10,15)

=head1 VERSION

This document describes version 0.001 of seq-intrange (from Perl distribution App-seq-intrange), released on 2021-07-17.

=head1 SYNOPSIS

 % seq-intrange 1,5-10,15

=head1 DESCRIPTION

This variant of B<seq> accept intrange specification (e.g. 1,5-10,15).

=head1 OPTIONS

=head2 --format=s, -f

=head2 --separator=s, -s

=head2 --equal-width, -w

=head2 --help, -h, -?

=head2 --version

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 BUGS

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

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 SEE ALSO

B<seq>, the Unix utility.

Other seq variants or themes I've written: L<seq-pl> (from L<App::seq::pl>),
L<dateseq> (from L<App::dateseq>) which can generate date sequences, including
infinite ones, L<seq-pericmd> (from L<App::SeqPericmd>) which lets you customize
increment/step (e.g. 0.5) or limit the number of items.

L<Sah::Schema::IntRange>, L<Regexp::Pattern::IntRange>, L<IntRange::Iter>.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2021 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
