#=============================================================================== # # Module: Term::CLI::ArgumentSet # # Description: Class for sets of (sub-)commands in Term::CLI # # Author: Steven Bakker (SBAKKER), # Created: 05/02/18 # # 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. # #=============================================================================== use 5.014_001; package Term::CLI::Role::ArgumentSet 0.03001 { use Modern::Perl; use Types::Standard qw( ArrayRef InstanceOf Maybe ); use Moo::Role; use namespace::clean; has _arguments => ( is => 'rw', writer => '_set_arguments', init_arg => 'arguments', isa => Maybe[ArrayRef[InstanceOf['Term::CLI::Argument']]], coerce => sub { # Copy the array, so the reference we store becomes # "internal", preventing accidental modification # from the outside. return [@{$_[0]}] }, ); sub arguments { return @{$_[0]->_arguments // []}; } sub has_arguments { my $self = shift; return ($self->_arguments and scalar @{$self->_arguments} > 0); } sub add_argument { my ($self, @arguments) = @_; if (!$self->_arguments) { $self->_set_arguments([]); } push @{$self->_arguments}, @arguments; return $self; } sub argument_names { my $self = shift; return map { $_->name } $self->arguments; } } 1; __END__ =pod =head1 NAME Term::CLI::Role::ArgumentSet - Role for (sub-)commands in Term::CLI =head1 VERSION version 0.03001 =head1 SYNOPSIS package Term::CLI::Command { use Moo; with('Term::CLI::Role::ArgumentSet'); ... }; my $cmd = Term::CLI::Command->new( ... ); $cmd->add_argument( Term::CLI::Argument->new(...) ); say "argument names:", join(', ', $cmd->argument_names); =head1 DESCRIPTION Role for L(3p) elements to represent a set of L(3p) objects. This role is consumed by L(3p). =head1 ATTRIBUTES This role defines two additional attributes: =over =item B =E I Reference to an array containing C object instances that describe the parameters that the command takes, or C. Note that the elements of the array are copied over to an internal array, so modifications to the I will not be seen. =back =head1 ACCESSORS AND PREDICATES =over =item B X Predicate function that returns whether or not any Ls have been added. =item B X Return the list of L object references that are owned by this object. =back =head1 METHODS =over =item B ( I, ... ) X Add I(s) to the argument set. I should be a reference to L object. =item B X Return the list of (sub-)command names (in the order they were specified). =item B ( I ) X Return a list of all commands in this object that match the I prefix. =item B ( I ) X Check whether I uniquely matches a command in this C object. Returns a reference to the appropriate L object if successful; otherwise, it sets the objects C field and returns C. Example: my $sub_cmd = $cmd->find_command($prefix); die $cmd->error unless $sub_cmd; =item B ( I ) X Wrapper function that will call the object's C function if it has been set, otherwise simply returns its arguments. =back =head1 SEE ALSO L(3p), L(3p). =head1 AUTHOR Steven Bakker Esbakker@cpan.orgE, 2018. =head1 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. =begin __PODCOVERAGE =head1 THIS SECTION SHOULD BE HIDDEN This section is meant for methods that should not be considered for coverage. This typically includes things like BUILD and DEMOLISH from Moo/Moose. It is possible to skip these when using the Pod::Coverage class (using C), but this is not an option when running C from the command line. The simplest trick is to add a hidden section with an item list containing these methods. =over =item BUILD =item DEMOLISH =back =end __PODCOVERAGE =cut