| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2018-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Commandable::Invocation 0.14; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 8 | ▶ | use v5.26; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 8 | ▶ | use experimental qw( signatures ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | C<Commandable::Invocation> - represents one invocation of a CLI command | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | my %commands = ( | ||||||||||||||||||||
| 19 | exit => sub { exit }, | ||||||||||||||||||||
| 20 | print => sub { print $_[0]->peek_remaining }, | ||||||||||||||||||||
| 21 | ... | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | while(1) { | ||||||||||||||||||||
| 25 | my $inv = Commmandable::Invocation->new( scalar <STDIN> ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | $commands{ $inv->pull_token }->( $inv ); | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | Instances of this class represent the text of a single invocation of a CLI | ||||||||||||||||||||
| 33 | command, allowing it to be incrementally parsed and broken into individual | ||||||||||||||||||||
| 34 | tokens during dispatch and invocation. | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head2 Tokens | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | When parsing for the next token, strings quoted using quote marks (C<"">) will | ||||||||||||||||||||
| 39 | be retained as a single token. Otherwise, tokens are split on (non-preserved) | ||||||||||||||||||||
| 40 | whitespace. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | Quote marks and backslashes may be escaped using C<\> characters. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head2 new | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | $inv = Commandable::Invocation->new( $text ) | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | Constructs a new instance, initialised to contain the given text string. | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 45 | ▶ | sub new ( $class, $text ) | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 59 | { | ||||||||||||||||||||
| 60 | 45 | $text =~ s/^\s+//; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 45 | return bless { | |||||||||||||||||||
| 63 | text => $text, | ||||||||||||||||||||
| 64 | }, $class; | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head2 new_from_tokens | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | $inv = Commandable::Invocation->new_from_tokens( @tokens ) | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | I<Since version 0.03.> | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | Constructs a new instance, initialised to contain text from the given tokens, | ||||||||||||||||||||
| 74 | such that subsequent calls to L</pull_token> will yield the given list of | ||||||||||||||||||||
| 75 | tokens. This may be handy for constructing instances from C<@ARGV> or similar | ||||||||||||||||||||
| 76 | cases where text has already been parsed and split into tokens. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 13 | ▶ | sub new_from_tokens ( $class, @tokens ) | ||||||||||||||||||
|
Subroutine
new_from_tokens: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 81 | { | ||||||||||||||||||||
| 82 | 13 | my $self = $class->new( "" ); | |||||||||||||||||||
| 83 | 13 | $self->putback_tokens( @tokens ); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 13 | return $self; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head1 METHODS | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 143 | ▶ | sub _next_token ( $self ) | ||||||||||||||||||
|
Subroutine
_next_token: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 143 | ▶ | if( $self->{text} =~ m/^"/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | $self->{text} =~ m/^"((?:\\.|[^"])*)"\s*/ and | ||||||||||||||||||||
| 96 | 4 | ▶ | $self->{trim_pos} = $+[0], return $self->_unescape( $1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||
| 99 | $self->{text} =~ m/^(\S+)\s*/ and | ||||||||||||||||||||
| 100 | 139 | ▶ | $self->{trim_pos} = $+[0], return $self->_unescape( $1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 43 | return undef; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 64 | ▶ | sub _escape ( $self, $s ) | ||||||||||||||||||
|
Subroutine
_escape: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 107 | { | ||||||||||||||||||||
| 108 | 64 | return $s =~ s/(["\\])/\\$1/gr; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 100 | ▶ | sub _unescape ( $self, $s ) | ||||||||||||||||||
|
Subroutine
_unescape: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 112 | { | ||||||||||||||||||||
| 113 | 100 | return $s =~ s/\\(["\\])/$1/gr; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head2 peek_token | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | $token = $inv->peek_token; | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | Looks at, but does not remove, the next token in the text string. Subsequent | ||||||||||||||||||||
| 121 | calls to this method will yield the same string, as will the next call to | ||||||||||||||||||||
| 122 | L</pull_token>. | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 3 | ▶ | sub peek_token ( $self ) | ||||||||||||||||||
|
Subroutine
peek_token: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 127 | { | ||||||||||||||||||||
| 128 | 3 | ▶ | return $self->{next_token} //= $self->_next_token; | ||||||||||||||||||
|
Condition66%2/3
$self->{"next_token"} //= $self->_next_token
MC/DC50%1/2
$self->{"next_token"} //= $self->_next_token
$self->{"next_token"}
$self->_next_token
Truth table66%2/3
A //= B
A= $self->{"next_token"}B= $self->_next_token
| |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head2 pull_token | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | $token = $inv->pull_token; | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | Removes the next token from the text string and returns it. | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 142 | ▶ | sub pull_token ( $self ) | ||||||||||||||||||
|
Subroutine
pull_token: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 140 | { | ||||||||||||||||||||
| 141 | 142 | ▶ | my $token = $self->{next_token} //= $self->_next_token; | ||||||||||||||||||
|
Condition100%3/3
$self->{"next_token"} //= $self->_next_token
MC/DC100%2/2
$self->{"next_token"} //= $self->_next_token
$self->{"next_token"}
$self->_next_token
Truth table100%3/3
A //= B
A= $self->{"next_token"}B= $self->_next_token
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 142 | ▶ | substr $self->{text}, 0, $self->{trim_pos}, "" if defined $token; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | 142 | undef $self->{next_token}; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 142 | return $token; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =head2 peek_remaining | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | $text = $inv->peek_remaining; | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | I<Since version 0.04.> | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | Returns the entire unparsed content of the rest of the text string. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 34 | ▶ | sub peek_remaining ( $self ) | ||||||||||||||||||
|
Subroutine
peek_remaining: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 160 | { | ||||||||||||||||||||
| 161 | 34 | return $self->{text}; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 putback_tokens | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | $inv->putback_tokens( @tokens ); | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | I<Since version 0.02.> | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | Prepends text back onto the stored text string such that subsequent calls to | ||||||||||||||||||||
| 171 | L</pull_token> will yield the given list of tokens once more. This takes care | ||||||||||||||||||||
| 172 | to quote tokens with spaces inside, and escape any embedded backslashes or | ||||||||||||||||||||
| 173 | quote marks. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | This method is intended to be used, for example, around a commandline option | ||||||||||||||||||||
| 176 | parser which handles mixed options and arguments, to put back the non-option | ||||||||||||||||||||
| 177 | positional arguments after the options have been parsed and removed from it. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 47 | ▶ | sub putback_tokens ( $self, @tokens ) | ||||||||||||||||||
|
Subroutine
putback_tokens: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 182 | { | ||||||||||||||||||||
| 183 | $self->{text} = join " ", | ||||||||||||||||||||
| 184 | ( map { | ||||||||||||||||||||
| 185 | 64 | my $s = $self->_escape( $_ ); | |||||||||||||||||||
| 186 | 64 | ▶ | $s =~ m/ / ? qq("$s") : $s | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 187 | } @tokens ), | ||||||||||||||||||||
| 188 | 47 | ( length $self->{text} ? $self->{text} : () ); | |||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 8 | 0x55AA; | |||||||||||||||||||