#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib 'lib';
use lib "$FindBin::Bin/../lib";
use OptArgs2;
use Data::Dumper;

$Data::Dumper::Indent   = 1;
$Data::Dumper::Sortkeys = 1;

cmd demo => (
    comment => 'OptArgs2 demonstration script',
    abbrev  => 1,
    optargs => [
        command => {
            isa      => 'SubCmd',
            comment  => '',
            required => 1,
        },
    ],
);

subcmd 'demo::arg' => (
    comment => 'arguments',
    optargs => [

        type => {
            isa      => 'SubCmd',
            comment  => 'arg type',
            required => 1,
        },
    ],
);

subcmd 'demo::arg::arrayref' => (
    comment => 'ArrayRef argument',
    optargs => [

        aaa => {
            isa      => 'ArrayRef',
            comment  => 'isa: ArrayRef, name: aaa',
            required => 1,
        },
    ],
);

subcmd 'demo::arg::greedy' => (
    comment => 'arguments',
    optargs => [
        filler => {
            isa      => 'Str',
            comment  => 'Str in front of a SubCmd',
            required => 1,
        },
        type => {
            isa      => 'SubCmd',
            comment  => 'arg type',
            required => 1,
        },
    ],
);

subcmd 'demo::arg::greedy::arrayref' => (
    comment => 'ArrayRef argument',
    optargs => [

        aaa => {
            isa      => 'ArrayRef',
            comment  => 'isa: ArrayRef, name: aaa',
            required => 1,
        },

        bbb => {
            isa     => 'ArrayRef',
            comment => 'isa: ArrayRef, name: bbb',
            default => [],
            greedy  => 1,
        },
    ],
);

subcmd 'demo::arg::greedy::hashref' => (
    comment => 'HashRef argument',
    optargs => [

        aaa => {
            isa      => 'HashRef',
            comment  => 'isa: HashRef, name: aaa',
            required => 1,
        },

        bbb => {
            isa     => 'HashRef',
            comment => 'isa: HashRef, name: bbb',
            greedy  => 1,
        },
    ],

);

subcmd 'demo::arg::greedy::str' => (
    comment => 'Str argument',
    optargs => [

        aaa => {
            isa      => 'Str',
            comment  => 'isa: Str, name: aaa',
            required => 1,
        },

        bbb => {
            isa     => 'Str',
            comment => 'isa: Str, name: bbb',
            default => 'some text',
            greedy  => 1,
        },
    ],
);

subcmd 'demo::arg::hashref' => (
    comment => 'HashRef argument',
    optargs => [

        aaa => {
            isa      => 'HashRef',
            comment  => 'isa: HashRef, name: aaa',
            required => 1,
        },

        bbb => {
            isa     => 'HashRef',
            comment => 'isa: HashRef, name: bbb',
            default => { x => 1 },

        },
    ],
);

subcmd 'demo::arg::int' => (
    comment => 'Int argument',
    optargs => [

        aaa => {
            isa      => 'Int',
            comment  => 'isa: Int, name: aaa',
            required => 1,
        },

        bbb => {
            isa     => 'Int',
            comment => 'isa: Int, name: bbb',
            default => 5,

        },
    ],
);

subcmd 'demo::arg::num' => (
    comment => 'Num argument',
    optargs => [

        aaa => {
            isa      => 'Num',
            comment  => 'isa: Num, name: aaa',
            required => 1,
        },

        bbb => {
            isa     => 'Num',
            comment => 'isa: Num, name: bbb',
            default => 6,

        },
    ],
);

subcmd 'demo::arg::str' => (
    comment => 'Str argument',
    optargs => [

        aaa => {
            isa      => 'Str',
            comment  => 'isa: Str, name: aaa',
            required => 1,
        },

        bbb => {
            isa     => 'Str',
            comment => 'isa: Str, name: bbb',
            default => 'some text',

        },
    ],
);

#    required => undef,
#    default  => undef,
#    greedy   => undef,
#    fallback => undef,

subcmd 'demo::opt' => (
    comment => 'options',
    optargs => [

        req => {
            alias    => 'r',
            isa      => '--Flag',
            comment  => 'isa: Flag, name: req',
            required => 1,
        },

        arrayref => {
            isa     => '--ArrayRef',
            comment => 'isa: ArrayRef, name: arrayref',
            alias   => 'a',
        },

        bool => {
            isa     => '--Bool',
            comment => 'isa: Bool, name: bool',
            alias   => 'b',
        },

        bool_default_0 => {
            isa     => '--Bool',
            comment => 'isa: Bool, name: bool_default_0',
            default => 0,
            alias   => 'c',
        },

        bool_default_1 => {
            isa     => '--Bool',
            comment => 'isa: Bool, name: bool_default_1',
            default => 1,
            alias   => 'd',
        },

        counter => {
            isa     => '--Counter',
            comment => 'isa: Counter, name: counter',
            alias   => 'e',
        },

        counter_default_3 => {
            isa     => '--Counter',
            comment => 'isa: Counter, name: counter_default_3',
            default => -3,
            alias   => 'f',
        },

        flag => {
            isa     => '--Flag',
            comment => 'isa: Flag, name: flag',
            alias   => 'g',
        },

        no_flag => {
            isa     => '--Flag',
            comment => 'isa: Flag, name: no_flag',
            alias   => 'i',
        },

        hashref => {
            isa     => '--HashRef',
            comment => 'isa: HashRef, name: hashref',
            alias   => 'j',
        },

        hashref_default => {
            isa     => '--HashRef',
            comment => 'isa: HashRef, name: hashref_default',
            default => {
                x => 1
            },
            alias => 'k',
        },

        int => {
            isa     => '--Int',
            comment => 'isa: Int, name: int',
            alias   => 'l',
        },

        int_default => {
            isa     => '--Int',
            comment => 'isa: Int, name: int_default',
            default => 5,
            alias   => 'm',
        },

        num => {
            isa     => '--Num',
            comment => 'isa: Num, name: num',
            alias   => 'n',
        },

        num_default => {
            isa     => '--Num',
            comment => 'isa: Num, name: num_default',
            default => 6,
            alias   => 'o',
        },

        str => {
            isa     => '--Str',
            comment => 'isa: Str, name: str',
            alias   => 'p',
        },

        str_default => {
            isa     => '--Str',
            comment => 'isa: Str, name: str_default',
            default => 'some text',
            alias   => 'q',
        },

        isa_name => {
            isa      => '--Str',
            isa_name => 'XXX',
            comment  => 'isa: Str, name: isa_name',
            alias    => 's',
        },
    ],
);

$SIG{__DIE__} = sub {
    my $err = shift;
    my $ref = ref $err;
    die "$err\n$ref\n"
      if $ref;
    die $err
      if $err;
};

my ( $cmd, $opts, $file ) = class_optargs('demo');
$opts->{__FILE__} = $file;
print Dumper( bless $opts, $cmd );
