File Coverage

blib/lib/OptArgs2/Cmd.pm
Criterion Covered Total %
statement 55 61 90.1
branch 10 20 50.0
condition 6 15 40.0
subroutine 9 10 90.0
pod 0 4 0.0
total 80 110 72.7


line stmt bran cond sub pod time code
1             package OptArgs2::Cmd;
2 6     6   50 use strict;
  6         14  
  6         245  
3 6     6   27 use warnings;
  6         11  
  6         335  
4 6     6   31 use parent 'OptArgs2::CmdBase';
  6         12  
  6         40  
5              
6             ### START Class::Inline ### v0.0.1 Wed Dec 3 10:44:51 2025
7             require Carp;
8             our ( @_CLASS, $_FIELDS, %_NEW );
9              
10             sub new {
11 11     11 0 23 my $class = shift;
12 11   33     57 my $CLASS = ref $class || $class;
13 11   66     52 $_NEW{$CLASS} //= do {
14 6         15 my ( %seen, @new, @build );
15 6         18 my @possible = ($CLASS);
16 6         22 while (@possible) {
17 12         27 my $c = shift @possible;
18 6     6   863 no strict 'refs';
  6         12  
  6         5358  
19 12 50       34 push @new, $c . '::_NEW' if exists &{ $c . '::_NEW' };
  12         76  
20 12 50       18 push @build, $c . '::BUILD' if exists &{ $c . '::BUILD' };
  12         58  
21 12         27 $seen{$c}++;
22 12 50       15 if ( exists &{ $c . '::DOES' } ) {
  12         40  
23 0         0 push @possible, grep { not $seen{$_}++ } $c->DOES('*');
  0         0  
24             }
25 12         22 push @possible, grep { not $seen{$_}++ } @{ $c . '::ISA' };
  6         62  
  12         79  
26             }
27 6         82 [ [ reverse(@new) ], [ reverse(@build) ] ];
28             };
29 11 50       80 my $self = { @_ ? @_ > 1 ? @_ : %{ $_[0] } : () };
  0 50       0  
30 11         120 bless $self, $CLASS;
31 11         262 my $attrs = { map { ( $_ => 1 ) } keys %$self };
  32         79  
32 11         25 map { $self->$_($attrs) } @{ $_NEW{$CLASS}->[0] };
  21         132  
  11         45  
33             {
34 10         16 local $Carp::CarpLevel = 3;
  10         23  
35 10         52 Carp::carp("OptArgs2::Cmd: unexpected argument '$_'") for keys %$attrs
36             }
37 10         19 map { $self->$_ } @{ $_NEW{$CLASS}->[1] };
  19         103  
  10         28  
38 9         40 $self;
39             }
40              
41             sub _NEW {
42 10     10   22 CORE::state $fix_FIELDS = do {
43 5 50       45 $_FIELDS = { @_CLASS > 1 ? @_CLASS : %{ $_CLASS[0] } };
  0         0  
44 5 50       38 $_FIELDS = $_FIELDS->{'FIELDS'} if exists $_FIELDS->{'FIELDS'};
45             };
46 10         20 map { delete $_[1]->{$_} } 'name', 'no_help';
  20         40  
47             }
48              
49             sub __RO {
50 0     0   0 my ( undef, undef, undef, $sub ) = caller(1);
51 0         0 Carp::confess("attribute $sub is read-only");
52             }
53              
54             sub name {
55 5 50   5 0 14 __RO() if @_ > 1;
56 5   33     26 $_[0]{'name'} //= $_FIELDS->{'name'}->{'default'}->( $_[0] );
57             }
58              
59             sub no_help {
60 9 50   9 0 26 __RO() if @_ > 1;
61 9   33     106 $_[0]{'no_help'} //= $_FIELDS->{'no_help'}->{'default'};
62             }
63             @_CLASS = grep 1, ### END Class::Inline ###
64             name => {
65             default => sub {
66             my $x = $_[0]->class;
67              
68             # once legacy code goes move this into optargs()
69             if ( $x eq 'main' ) {
70             require File::Basename;
71             File::Basename::basename($0),;
72             }
73             else {
74             $x =~ s/.*://;
75             $x =~ s/_/-/g;
76             $x;
77             }
78             },
79             },
80             no_help => { default => 0 },
81             ;
82              
83             our @CARP_NOT = @OptArgs2::CARP_NOT;
84              
85             sub BUILD {
86 9     9 0 15 my $self = shift;
87              
88 9 50 33     31 $self->add_opt(
89             isa => OptArgs2::USAGE_HELP(),
90             show_default => 0,
91             )
92             unless $self->no_help
93             or 'CODE' eq ref $self->optargs; # legacy interface
94             }
95              
96             1;
97              
98             __END__