#!perl

use strict;
use warnings;
use Log::ger;

use Complete::Util qw(complete_array_elem combine_answers);
use Getopt::Long::Complete qw(GetOptionsWithCompletion);

# AUTHORITY
# DATE
# DIST
# VERSION

my %opts;
my $res = GetOptionsWithCompletion(
    sub {
        my %args = @_;
        #use DD; dd \%args;
        my $word = $args{word};
        my $type = $args{type};

        if ($type eq 'arg') {
            my $argpos = $args{argpos};
            return complete_array_elem(
                array=>["arg$argpos-a", "arg$argpos-b"], word=>$word);
        } elsif ($type eq 'optval') {
            my $opts = ref $args{opt} eq 'ARRAY' ? $args{opt} : [$args{opt}];
            #log_trace "opts=%s", $opts;
            my @comps;
            if (grep { $_ eq '--int-comp-array' } @$opts) {
                push @comps, complete_array_elem(array=>[1..10], word=>$word);
            }
            if (grep { $_ eq '--str-comp-sub' } @$opts) {
                push @comps, complete_array_elem(array=>[map {"$word$_"} "a".."z"],
                                                 word=>$word);
            }
            if (grep { /\A(--planet|-P|--opt-planet|-p)\z/ } @$opts) {
                push @comps, {
                    words => complete_array_elem(
                        array=>[
                            "mercury",
                            "venus",
                            "mars",
                        ],
                        word=>$word,
                    ),
                    path_sep => '::',
                };
            }
            if (grep { /\A(--fruit)\z/ } @$opts) {
                push @comps, {
                    words => complete_array_elem(
                        array=>[
                            "banana",
                            "apple",
                            "apricot",
                            "durian",
                        ],
                        word=>$word,
                    ),
                    path_sep => '::',
                };
            }
            return combine_answers(@comps);
        }

        undef;
    },
    \%opts,
    'flag1|1',
    'flag2|f',
    'bool|b!',
    'int=i',
    'float|F=f',
    'str|text|S=s',
    'array=s@',
    'int-comp-array=i',
    'str-comp-sub=s',

    # to demonstrate completing optval
    'planet|P=s',
    'opt-planet|p:s',
    'fruit=s',

    'version|v' => sub{
        no warnings;
        print "demo-getopt-long-complete version $main::VERSION\n";
        exit 0;
    },
    'help|h|?' => sub {
        print <<_;
Usage:
  $0 --help (or -h, -?)
  $0 [opts]

Options:
  --array=s@
  --(no)bool, -b
  --flag1, -1
  --flag2, -f
  --float=f, -F
  --fruit=s
  --int-comp-array=i
  --int=i
  --opt-planet:s, -p
  --planet=s, -P
  --str-comp-sub=s
  --str=s, --text, -S
_
        exit 0;
    },
);

print $res ? "Getopt succeeded\n" : "Getopt failed\n";
print "Result:\n";
for (sort keys %opts) {
    print "  $_: $opts{$_}\n";
}

# ABSTRACT: Script to demonstrate Getopt::Long::Complete
# PODNAME:

=head1 SYNOPSIS

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

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

Test completion:

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

=head2
