| 1 | #============================================================================= | ||
| 2 | # | ||
| 3 | # Module: Term::CLI::ArgumentSet | ||
| 4 | # | ||
| 5 | # Description: Class for sets of (sub-)commands in Term::CLI | ||
| 6 | # | ||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||
| 8 | # Created: 05/02/18 | ||
| 9 | # | ||
| 10 | # Copyright (c) 2018 Steven Bakker | ||
| 11 | # | ||
| 12 | # This module is free software; you can redistribute it and/or modify | ||
| 13 | # it under the same terms as Perl itself. See "perldoc perlartistic." | ||
| 14 | # | ||
| 15 | # This software is distributed in the hope that it will be useful, | ||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 18 | # | ||
| 19 | #============================================================================= | ||
| 20 | |||
| 21 | use 5.014_001; | ||
| 22 | |||
| 23 | package Term::CLI::Role::ArgumentSet 0.051003 { | ||
| 24 | |||
| 25 | use Modern::Perl 1.20140107; | ||
| 26 | |||
| 27 | use Types::Standard 1.000005 qw( | ||
| 28 | ArrayRef | ||
| 29 | InstanceOf | ||
| 30 | Maybe | ||
| 31 | ); | ||
| 32 | |||
| 33 | use Moo::Role; | ||
| 34 | use namespace::clean 0.25; | ||
| 35 | |||
| 36 | has _arguments => ( | ||
| 37 | is => 'rw', | ||
| 38 | writer => '_set_arguments', | ||
| 39 | init_arg => 'arguments', | ||
| 40 | isa => Maybe[ArrayRef[InstanceOf['Term::CLI::Argument']]], | ||
| 41 | coerce => sub { | ||
| 42 | # Copy the array, so the reference we store becomes | ||
| 43 | # "internal", preventing accidental modification | ||
| 44 | # from the outside. | ||
| 45 | return [@{$_[0]}] | ||
| 46 | }, | ||
| 47 | ); | ||
| 48 | |||
| 49 | |||
| 50 | sub arguments { | ||
| 51 | return @{$_[0]->_arguments // []}; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub set_arguments { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | $self->_set_arguments([]); | ||
| 59 | $self->add_argument(@_); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub has_arguments { | ||
| 64 | my $self = shift; | ||
| 65 | return ($self->_arguments and scalar @{$self->_arguments} > 0); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub add_argument { | ||
| 70 | my ($self, @arguments) = @_; | ||
| 71 | |||
| 72 | if (!$self->_arguments) { | ||
| 73 | $self->_set_arguments([]); | ||
| 74 | } | ||
| 75 | |||
| 76 | push @{$self->_arguments}, @arguments; | ||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub argument_names { | ||
| 82 | my $self = shift; | ||
| 83 | return map { $_->name } $self->arguments; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | __END__ |