NAME

Term::CLI::Argument::Enum - class for "enum" string arguments in Term::CLI

VERSION

version 0.053004

SYNOPSIS

use Term::CLI::Argument::Enum;

# static value list
my $arg = Term::CLI::Argument::Enum->new(
    name => 'arg1',
    value_list => [qw( foo bar baz )],
);

# dynamic value list
my $arg = Term::CLI::Argument::Enum->new(
    name => 'arg1',
    value_list => sub {  my @values = (...); \@values },
);

DESCRIPTION

Class for "enum" string arguments in Term::CLI(3p).

This class inherits from the Term::CLI::Argument(3p) class.

CLASS STRUCTURE

Inherits from:

Term::CLI::Argument(3p).

Consumes:

None.

CONSTRUCTORS

new
OBJ = Term::CLI::Argument::Enum(
    name => STRING,
    value_list => ArrayRef | CodeRef
);

See also Term::CLI::Argument(3p). The value_list argument is mandatory and can either be a reference to an array, or a code refrerence.

A value list consisting of a code reference can be used to implement dynamic values. The code reference will be called with a single argument consisting of the reference to the Term::CLI::Argument::Enum object.

ACCESSORS

See also Term::CLI::Argument(3p).

value_list

A reference to a either a list of valid values for the argument or a subroutine which returns a reference to such a list.

METHODS

See also Term::CLI::Argument(3p).

The following methods are added or overloaded:

validate
complete

EXAMPLES

Return values depending on the time of day:

# Valid values for 'at' depend on the current time of day.
# Before 1pm, 'today' is possible, otherwise only 'tomorrow'.
my $arg = Term::CLI::Argument::Enum(
    name => 'at',
    value_list => sub {
        my ($self) = @_;
        my $hr = (localtime)[2];
        return ($hr < 13) ? ['today', 'tomorrow'] : ['tomorrow'];
    }
);

Return values based on a predefined list of values that can be (temporarily) overridden with local():

my @LIST = qw( one two three );

my $arg = Term::CLI::Argument::Enum(
    name => 'arg',
    value_list => sub { return \@LIST }
);

...

# Will allow 'one', 'two', 'three' for 'arg'.
$cli->execute($cli->readline);

{
    local(@LIST) = qw( four five six );
    # Now allow 'four', 'five', 'six' for 'arg'.
    $cli->execute($cli->readline);
}

# Allow 'one', 'two', 'three' for 'arg' again.
$cli->execute($cli->readline);

SEE ALSO

Term::CLI::Argument(3p), Term::CLI(3p).

AUTHOR

Steven Bakker <sbakker@cpan.org>, 2018.

COPYRIGHT AND LICENSE

Copyright (c) 2018 Steven Bakker

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See "perldoc perlartistic."

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.