#!perl

our $DATE = '2016-12-03'; # DATE
our $VERSION = '0.003'; # VERSION

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

Getopt::Long::More::Configure('auto_help', 'auto_version');

my %opts = (
    cols => 1,
    bg   => 0,
    module => [],
    array => [],
);

my $res = GetOptions(
    'flag1|1'    => \$opts{flag1},
    'flag2|f'    => \$opts{flag2},
    'bool|b!'    => \$opts{bool},
    'int=i'      => optspec(
        handler => \$opts{int},
        summary => 'An integer number',
        default => 42,
    ),
    'module|M=s@' => optspec(
        required => 1,
        handler => $opts{module},
        summary => 'Module name(s)',
        description => <<'_',
One or more module names.

Each module must be valid Perl module name.
_
        completion => sub {
            require Complete::Util;
            my %args = @_;
            return {
                words => Complete::Util::complete_array_elem(
                    array=>[
                        "Complete::Util",
                        "Text::ANSITable",
                        "Text::ANSI::",
                        "Text::ANSI::Util",
                    ],
                    word=>$args{word},
                ),
                path_sep => '::',
            };
        },
    ),
    'float|F=f' => \$opts{float},
    'str|text|S=s' => \$opts{str},
    'array=s@' => $opts{array},
    'int-comp-array=i' => optspec(
        handler => \$opts{int_comp_array},
        completion => sub {
            require Complete::Util;
            my %args = @_;
            Complete::Util::complete_array_elem(array=>[1..10], word=>$args{word});
        },
    ),
    'str-comp-sub=s' => optspec(
        handler => \$opts{str_comp_sub},
        completion => sub {
            require Complete::Util;
            my %args = @_;
            return complete_array_elem(array=>[map {"$args{word}$_"} "a".."z"],
                                       word=>$args{word});
        },
    ),
    'show-pod' => sub {
        print Getopt::Long::More::OptionsPod;
        exit 0;
    },
    '<>' => optspec(
        handler => sub{},
        completion => sub {
            require Complete::Util;
            my %args = @_;
            my $argpos = $args{argpos};
            Complete::Util::complete_array_elem(
                array=>["arg$argpos-a", "arg$argpos-b"], word=>$args{word});
        },
    ),
);

print +($res ? "Getopt succeeded" : "Getopt failed"), "\n";
print "flag1: ", $opts{flag1} ? 1:0, "\n";
print "flag2: ", $opts{flag2} ? 1:0, "\n";
print "int: $opts{int}\n";
print "module: [", join(", ", @{$opts{module}}), "]\n";

# ABSTRACT: Script to demonstrate Getopt::Long::More
# PODNAME: demo-getopt-long-more

__END__

=pod

=encoding UTF-8

=head1 NAME

demo-getopt-long-more - Script to demonstrate Getopt::Long::More

=head1 VERSION

This document describes version 0.003 of demo-getopt-long-more (from Perl distribution Getopt-Long-More), released on 2016-12-03.

=head1 SYNOPSIS

Activate completion using (can be put in your bash startup file):

 % complete -C demo-getopt-long-more demo-getopt-long-more

Test completion:

 % demo-getopt-long-more <tab>
 % demo-getopt-long-more -<tab>
 % demo-getopt-long-more --int 1 -<tab>
 # and so on

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Getopt-Long-More>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-Getopt-Long-More>.

=head1 BUGS

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

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) 2016 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
