CLI-Gwrap

 view release on metacpan or  search on metacpan

lib/CLI/Gwrap/Opt.pm  view on Meta::CPAN

#===============================================================================
#
#      PODNAME:  CLI::Gwrap::Opt.pm
#     ABSTRACT:  a single CLI option item for CLI::Gwrap
#
#       AUTHOR:  Reid Augustin
#        EMAIL:  reid@LucidPort.com
#      CREATED:  07/08/2013 11:58:12 AM
#===============================================================================

use 5.008;
use strict;
use warnings;

package CLI::Gwrap::Opt;

use Moo;
use Types::Standard qw( Int Str ArrayRef HashRef CodeRef );

our $VERSION = '0.030'; # VERSION

has 'type'        => (is => 'ro', isa => Str);
has 'name'        => (is => 'ro', isa => ArrayRef, trigger => sub {
        my ($self, $new) = @_;

        if (not exists $self->{joiner}) {
            $self->{joiner} = $new->[0] =~ m/\A-?.\z/
                ? ' '       # single letter options, joiner defaults to space
                : '=';      # otherwise, use equals sign
            }
    },
);
has 'description' => (is => 'ro', isa => Str);
has 'state'       => (is => 'ro');
has 'label'       => (is => 'ro', isa => Str);
has 'choices'     => (is => 'ro', isa => ArrayRef[Str]); # for radio buttons
has 'width'       => (is => 'ro', isa => Int);
has 'joiner'      => (is => 'ro', isa => Str);
has 'widget'      => (is => 'rw');
has 'retriever'   => (is => 'rw', isa => CodeRef);

sub name_for_display {
    my ($self, $verbatim) = @_;

    return $self->label if (defined $self->label);  # override

    my $unaliased = $self->name_for_CLI;
    my $aliased = $self->name->[1];

    if ($aliased
        and $unaliased
        and $self->name->[0] ne $aliased) {
        return "$unaliased ($aliased)";
    }
    return $unaliased || $aliased;
}

sub name_for_CLI {
    my ($self, $verbatim) = @_;

    my $unaliased = $self->name->[0];     # unaliased
    return $unaliased if ($verbatim or not $unaliased);
    return "-$unaliased" if (length $unaliased == 1);
    return "--$unaliased";
}

1;



=pod

=head1 NAME

CLI::Gwrap::Opt.pm - a single CLI option item for CLI::Gwrap

=head1 VERSION

version 0.030

=head1 DESCRIPTION

CLI::Gwrap::Opt encapsulates individual options for CLI::Gwrap.

=head2 ATTRIBUTES

=over

=item type => 'string'

A string naming the type of option (check, radio, string, etc).

=item name => [ 'name', 'long name' ]

This is the name of the option as used on the command line, and a
description that should be more useful for casual users.  When B<name> is



( run in 0.509 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )