File: define-classes-inline.t

package info (click to toggle)
libcli-framework-perl 0.05-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 456 kB
  • sloc: perl: 2,168; sql: 18; sh: 3; makefile: 2
file content (180 lines) | stat: -rw-r--r-- 3,224 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use strict;
use warnings;

use lib 'lib';
use lib 't/lib';

use Test::More tests => 1;
use File::Spec;

#~~~~~~
# Send STDOUT, STDERR to null device...
close STDOUT;
open ( STDOUT, '>', File::Spec->devnull() );
close STDERR;
open( STDERR, '>', File::Spec->devnull() );
#~~~~~~

ok( My::App->run() );

###################################
#
#   INLINE APPLICATION DEFINITION...
#
###################################

package My::App;
use base qw( CLI::Framework );

use strict;
use warnings;

#-------

sub usage_text {
    q{
    OPTIONS
        --db [path]  : db
        -v --verbose : be verbose
        -h --help    : show help

    COMMANDS
        x
    }
}

#-------

sub option_spec {
    [ 'help|h'      => 'show help' ],
    [ 'verbose|v'   => 'be verbose' ],
    [ 'db=s'        => 'db' ],
}

sub command_map {
    console => 'CLI::Framework::Command::Console',
    list    => 'CLI::Framework::Command::List',
    menu    => 'CLI::Framework::Command::Menu',
    'dump'  => 'CLI::Framework::Command::Dump',
    tree    => 'CLI::Framework::Command::Tree',
    x       => 'My::App::Command::X',
#    x       => 'My::Command::Shared::X',
}

sub command_alias {
    h   => 'help',

    t   => 'tree',
    d   => 'dump',

    sh  => 'console',
    c   => 'console',
    m   => 'menu',
}

#-------

sub init {
    my ($app, $opts) = @_;

    print __PACKAGE__.'::init()', "\n";
}

###################################
#
#   INLINE COMMAND DEFINITIONS...
#
###################################

package My::App::Command::X;
use base qw( CLI::Framework::Command );

use strict;
use warnings;

#-------

sub usage_text {
    q{
    x [--date=yyyy-mm-dd] [subcommands...]

    OPTIONS
       --date=yyyy-mm-dd:       date
   
    ARGUMENTS (subcommands)
        search:                    ...
    }
}

sub option_spec {
    return unless ref $_[0] eq __PACKAGE__; # non-inheritable behavior
    (
        [ 'date=s' => 'date that entry applies to' ],
    )
}

sub subcommand_alias {
    return unless ref $_[0] eq __PACKAGE__; # non-inheritable behavior
    (
        d   => 'do',
        a   => 'add',
        s   => 'search',
    )
}

sub validate {
    my ($self, $opts, @args) = @_;
    return unless ref $_[0] eq __PACKAGE__; # non-inheritable behavior

    # ...
}

sub notify_of_subcommand_dispatch {
    my ($self, $subcommand, $opts, @args ) = @_;
    return unless ref $_[0] eq __PACKAGE__; # non-inheritable behavior

warn __PACKAGE__.'::notify_of_subcommand_dispatch', "\n";
#require Data::Dumper; warn Data::Dumper::Dumper( [ $subcommand, $opts, \@args ] );

    # ...
}

#-------

package My::App::Command::X::Search;
use base qw( My::App::Command::X );

use strict;
use warnings;

sub usage_text {
    q{
    x search --regex=<regex> [--tag=<tag>]: search
    }
}

sub option_spec {
    (
        [ 'regex=s'  => 'regex' ],
        [ 'tag=s@'   => 'tag' ],
    )
}

sub validate {
    my ($self, $opts, @args) = @_;
    die "missing required option 'regex'\n" unless $opts->{regex};
}

sub run {
    my ($self, $opts, @args) = @_;

    my $regex = $opts->{regex};
    my $tags = $opts->{tag};

warn __PACKAGE__.'::run()', "\n";
    warn "searching...\n";# if $self->session('verbose');

    return '';
}

__END__