File Coverage

blib/lib/CLI/Gwrap/Opt.pm
Criterion Covered Total %
statement 15 27 55.5
branch 0 8 0.0
condition 0 12 0.0
subroutine 5 7 71.4
pod 0 2 0.0
total 20 56 35.7


line stmt bran cond sub pod time code
1             #===============================================================================
2             #
3             # PODNAME: CLI::Gwrap::Opt.pm
4             # ABSTRACT: a single CLI option item for CLI::Gwrap
5             #
6             # AUTHOR: Reid Augustin
7             # EMAIL: reid@LucidPort.com
8             # CREATED: 07/08/2013 11:58:12 AM
9             #===============================================================================
10              
11 1     1   26 use 5.008;
  1         4  
  1         40  
12 1     1   6 use strict;
  1         1  
  1         36  
13 1     1   16 use warnings;
  1         2  
  1         44  
14              
15             package CLI::Gwrap::Opt;
16              
17 1     1   5 use Moo;
  1         2  
  1         7  
18 1     1   309 use Types::Standard qw( Int Str ArrayRef HashRef CodeRef );
  1         1  
  1         7  
19              
20             our $VERSION = '0.030'; # VERSION
21              
22             has 'type' => (is => 'ro', isa => Str);
23             has 'name' => (is => 'ro', isa => ArrayRef, trigger => sub {
24             my ($self, $new) = @_;
25              
26             if (not exists $self->{joiner}) {
27             $self->{joiner} = $new->[0] =~ m/\A-?.\z/
28             ? ' ' # single letter options, joiner defaults to space
29             : '='; # otherwise, use equals sign
30             }
31             },
32             );
33             has 'description' => (is => 'ro', isa => Str);
34             has 'state' => (is => 'ro');
35             has 'label' => (is => 'ro', isa => Str);
36             has 'choices' => (is => 'ro', isa => ArrayRef[Str]); # for radio buttons
37             has 'width' => (is => 'ro', isa => Int);
38             has 'joiner' => (is => 'ro', isa => Str);
39             has 'widget' => (is => 'rw');
40             has 'retriever' => (is => 'rw', isa => CodeRef);
41              
42             sub name_for_display {
43 0     0 0   my ($self, $verbatim) = @_;
44              
45 0 0         return $self->label if (defined $self->label); # override
46              
47 0           my $unaliased = $self->name_for_CLI;
48 0           my $aliased = $self->name->[1];
49              
50 0 0 0       if ($aliased
      0        
51             and $unaliased
52             and $self->name->[0] ne $aliased) {
53 0           return "$unaliased ($aliased)";
54             }
55 0   0       return $unaliased || $aliased;
56             }
57              
58             sub name_for_CLI {
59 0     0 0   my ($self, $verbatim) = @_;
60              
61 0           my $unaliased = $self->name->[0]; # unaliased
62 0 0 0       return $unaliased if ($verbatim or not $unaliased);
63 0 0         return "-$unaliased" if (length $unaliased == 1);
64 0           return "--$unaliased";
65             }
66              
67             1;
68              
69              
70              
71             =pod
72              
73             =head1 NAME
74              
75             CLI::Gwrap::Opt.pm - a single CLI option item for CLI::Gwrap
76              
77             =head1 VERSION
78              
79             version 0.030
80              
81             =head1 DESCRIPTION
82              
83             CLI::Gwrap::Opt encapsulates individual options for CLI::Gwrap.
84              
85             =head2 ATTRIBUTES
86              
87             =over
88              
89             =item type => 'string'
90              
91             A string naming the type of option (check, radio, string, etc).
92              
93             =item name => [ 'name', 'long name' ]
94              
95             This is the name of the option as used on the command line, and a
96             description that should be more useful for casual users. When B is
97             set, the default joiner is also determined (from the length of 'name'),
98             unless the joiner has already been set. Single letter 'name's get a space
99             (' ') joiner, and longer 'name's get an equals sign ('=').
100              
101             =item description' => (is => 'ro', isa => Str);
102              
103             A short description of the option which is usually presented to the user
104             when the mouse 'hovers' over the option.
105              
106             =item state => 'string'
107              
108             Initial state of the option. The option B determines the context for
109             the B: Bs are true/false, Bs are text, etc.
110              
111             =item label => 'string'
112              
113             Overrides the normal name/description rules for the option label in the
114             GUI.
115              
116             =item choices => [ 'choices', ... ]
117              
118             Reference to an array of the choices for a radio option.
119              
120             =item width => number
121              
122             The number of pixels for input widgets for which a width might make sense
123             (B, B, etc). These widgets normally exapnd with the
124             enclosing window, setting a width overrides this behavior.
125              
126             =item joiner => 'string'
127              
128             How to join the option name to the option value. Default for short
129             (single-letter) options is a space, and for long options is an equals sign.
130              
131             =item widget => object
132              
133             Used by the GUI wrapper to store a pointer to the widget for this option.
134              
135             =item retriever => coderef
136              
137             Used by the GUI wrapper to store a reference to a subroutine that can read
138             back the value of the option widget.
139              
140             =back
141              
142             =head1 SEE ALSO
143              
144             CLI::Gwrap
145              
146             =head1 AUTHOR
147              
148             Reid Augustin
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is copyright (c) 2013 by Reid Augustin.
153              
154             This is free software; you can redistribute it and/or modify it under
155             the same terms as the Perl 5 programming language system itself.
156              
157             =cut
158              
159              
160             __END__