#!perl

# DATE
# VERSION

use 5.010;
use strict;
use warnings;
use Complete::Util qw(complete_array_elem);
use Getopt::Long::Subcommand qw(GetOptions);
#use Log::Any::IfLOG '$log';

my %Opts;
my $res = GetOptions(
    summary => 'Command with subcommands',
    # generic options
    options => {
        'help|h|?' => \$Opts{help},
        'version|v' => \$Opts{version},
        'verbose' => sub {},
    },
    subcommands => {
        subc1 => {
            summary => 'First subcommand',
            options => {
                'flag1|1' => sub{},
                'flag2|f' => sub{},
                'fruit=s' => sub{},
            },
        },
        subc2 => {
            summary => 'Second subcommand',
            options => {
                'bool|b!' => sub{},
                'int=i' => sub{},
                'fruit=s' => sub{},
            },
        },
        subc3 => {
            summary => 'Third subcommand',
            options => {
                'float|F=f' => sub{},
                'str|text|S=s' => sub{},
                'array=s@' => sub{},
                'int-comp-array=i' => sub{},
                'str-comp-sub=s' => sub{},
            },
            subcommands => {
                subsubc1 => {
                    summary => 'First subsubcommand',
                    options => {
                        'str|text|S=s' => sub{},
                        'array=s@' => sub{},
                        'city=s' => sub {},
                    },
                },
                subsubc2 => {
                    summary => 'Second subsubcommand',
                },
            },
        },
    },
    completion => sub {
        my %args = @_;
        my $subc  = $args{subcommand};
        my $word  = $args{word};
        my $ospec = $args{ospec} // '';

        #$log->tracef("subc: %s", $subc);
        if ($ospec eq 'int-comp-array=i') {
            return complete_array_elem(array=>[1..10], word=>$word);
        } elsif ($ospec eq 'str-comp-sub=s') {
            return complete_array_elem(array=>[map {"$word$_"} "a".."z"],
                                       word=>$word);
        } elsif ($subc->[0] eq 'subc1' && $ospec eq 'fruit=s') {
            return complete_array_elem(array=>[qw/apple apricot avocado/],
                                       word=>$word);
        } elsif ($subc->[0] eq 'subc2' && $ospec eq 'fruit=s') {
            return complete_array_elem(array=>[qw/banana blueberry blackberry/],
                                       word=>$word);
        } elsif ($subc->[0] eq 'subc3' && $subc->[1] eq 'subsubc1'
                     && $ospec eq 'city=s') {
            return complete_array_elem(array=>[qw/bandung solo surabaya/],
                                       word=>$word);
        } else {
            return undef;
        }
    },
);

if ($Opts{help}) {
    say <<_;
Usage:
  $0 --help
  $0 --version
  $0 <subcommand> [generic options] [subcommand options]

Subcommands:
  subc1 - First subcommand
  subc2 - Second subcommand
  subc3 - Third subcommand

Generic options:
  --help, -h, -?
  --version, -v
  --verbose

Options for subcommand subc1:
  ...

Options for subcommand subc2:
  ...

Options for subcommand subc3:
  ...
_
    exit 0;
}

if ($Opts{version}) {
    no warnings;
    say "$0 version $main::VERSION";
    exit 0;
}

#say $res ? "Getopt failed" : "Getopt succeeded";
# ABSTRACT: Script to demonstrate Getopt::Long::Subcommand
# PODNAME:

=head1 SYNOPSIS

 % demo-getopt-long-subcommand -h
 % demo-

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

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

Test completion:

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

=head2
