| 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, 2021-2024 -- leonerd@leonerd.org.uk | ||||||||||||
| 5 | |||||||||||||
| 6 | package Commandable::Finder::SubAttributes 0.14; | ||||||||||||
| 7 | |||||||||||||
| 8 | 3 | ▶ | use v5.26; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use experimental qw( signatures ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use base qw( Commandable::Finder ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 3 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 3 | ▶ | use Commandable::Command; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 3 | ▶ | use constant HAVE_ATTRIBUTE_STORAGE => eval { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
| |||||||||||||
| 18 | 3 | require Attribute::Storage; | |||||||||||
| 19 | 0 | Attribute::Storage->VERSION( '0.12' ); | |||||||||||
| 20 | 3 | }; | |||||||||||
| 21 | |||||||||||||
| 22 | =head1 NAME | ||||||||||||
| 23 | |||||||||||||
| 24 | C<Commandable::Finder::SubAttributes> - find commands stored as subs with attributes | ||||||||||||
| 25 | |||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||
| 27 | |||||||||||||
| 28 | use Commandable::Finder::SubAttributes; | ||||||||||||
| 29 | |||||||||||||
| 30 | my $finder = Commandable::Finder::SubAttributes->new( | ||||||||||||
| 31 | package => "MyApp::Commands", | ||||||||||||
| 32 | ); | ||||||||||||
| 33 | |||||||||||||
| 34 | my $help_command = $finder->find_command( "help" ); | ||||||||||||
| 35 | |||||||||||||
| 36 | foreach my $command ( $finder->find_commands ) { | ||||||||||||
| 37 | ... | ||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||
| 41 | |||||||||||||
| 42 | This implementation of L<Commandable::Finder> looks for functions that define | ||||||||||||
| 43 | commands, where each command is provided by an individual sub in a given | ||||||||||||
| 44 | package. | ||||||||||||
| 45 | |||||||||||||
| 46 | =head1 ATTRIBUTES | ||||||||||||
| 47 | |||||||||||||
| 48 | use Commandable::Finder::SubAttributes ':attrs'; | ||||||||||||
| 49 | |||||||||||||
| 50 | sub command_example | ||||||||||||
| 51 | :Command_description("An example of a command") | ||||||||||||
| 52 | { | ||||||||||||
| 53 | ... | ||||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | Properties about each command are stored as attributes on the named function, | ||||||||||||
| 57 | using L<Attribute::Storage>. | ||||||||||||
| 58 | |||||||||||||
| 59 | The following attributes are available on the calling package when imported | ||||||||||||
| 60 | with the C<:attrs> symbol: | ||||||||||||
| 61 | |||||||||||||
| 62 | =head2 Command_description | ||||||||||||
| 63 | |||||||||||||
| 64 | :Command_description("description text") | ||||||||||||
| 65 | |||||||||||||
| 66 | Gives a plain string description text for the command. | ||||||||||||
| 67 | |||||||||||||
| 68 | =head2 Command_arg | ||||||||||||
| 69 | |||||||||||||
| 70 | :Command_arg("argname", "description") | ||||||||||||
| 71 | |||||||||||||
| 72 | Gives a named argument for the command and its description. | ||||||||||||
| 73 | |||||||||||||
| 74 | If the name is suffixed by a C<?>, this argument is optional. (The C<?> itself | ||||||||||||
| 75 | will be removed from the name). | ||||||||||||
| 76 | |||||||||||||
| 77 | If the name is suffixed by C<...>, this argument is slurpy. (The C<...> itself | ||||||||||||
| 78 | will be removed from the name). | ||||||||||||
| 79 | |||||||||||||
| 80 | =head2 Command_opt | ||||||||||||
| 81 | |||||||||||||
| 82 | :Command_opt("optname", "description") | ||||||||||||
| 83 | |||||||||||||
| 84 | :Command_opt("optname", "description", "default") | ||||||||||||
| 85 | |||||||||||||
| 86 | Gives a named option for the command and its description. | ||||||||||||
| 87 | |||||||||||||
| 88 | If the name contains C<|> characters it provides multiple name aliases for the | ||||||||||||
| 89 | same option. | ||||||||||||
| 90 | |||||||||||||
| 91 | If the name field ends in a C<=> character, a value is expected for the | ||||||||||||
| 92 | option. It can either be parsed from the next input token, or after an C<=> | ||||||||||||
| 93 | sign of the same token: | ||||||||||||
| 94 | |||||||||||||
| 95 | --optname VALUE | ||||||||||||
| 96 | --optname=VALUE | ||||||||||||
| 97 | |||||||||||||
| 98 | If the name field ends in a C<@> character, a value is expected for the option | ||||||||||||
| 99 | and can be specified multiple times. All the values will be collected into an | ||||||||||||
| 100 | ARRAY reference. | ||||||||||||
| 101 | |||||||||||||
| 102 | If the name field ends in a C<+> character, the option can be specified | ||||||||||||
| 103 | multiple times and the total count will be used as the value. | ||||||||||||
| 104 | |||||||||||||
| 105 | If the name field ends in a C<!> character, the option is negatable. An option | ||||||||||||
| 106 | name of C<--no-OPTNAME> is recognised and will reset the value to C<undef>. By | ||||||||||||
| 107 | setting a default of some true value (e.g. C<1>) you can detect if this has | ||||||||||||
| 108 | happened. | ||||||||||||
| 109 | |||||||||||||
| 110 | An optional third argument may be present to specify a default value, if not | ||||||||||||
| 111 | provided by the invocation. | ||||||||||||
| 112 | |||||||||||||
| 113 | =head1 GLOBAL OPTION ATTRIBUTES | ||||||||||||
| 114 | |||||||||||||
| 115 | I<Since version 0.14> this module also allows attaching attributes to package | ||||||||||||
| 116 | variables in the package that stores the subroutines (often C<main>), which | ||||||||||||
| 117 | will then be handled automatically as global options by the finder. | ||||||||||||
| 118 | |||||||||||||
| 119 | Remember that these have to be I<package> variables (i.e. declared with | ||||||||||||
| 120 | C<our>); lexical variables (declared with C<my>) will not work. | ||||||||||||
| 121 | |||||||||||||
| 122 | our $VERBOSE | ||||||||||||
| 123 | :GlobalOption("verbose|v+", "Increase the verbosity of status output"); | ||||||||||||
| 124 | |||||||||||||
| 125 | This often serves as a convenient alternative to modules like L<Getopt::Long>, | ||||||||||||
| 126 | because it integrates with the C<help> command automatically. | ||||||||||||
| 127 | |||||||||||||
| 128 | =head2 GlobalOption | ||||||||||||
| 129 | |||||||||||||
| 130 | :GlobalOption("optname", "description") | ||||||||||||
| 131 | |||||||||||||
| 132 | Gives the name for this global option and its description. These are handled | ||||||||||||
| 133 | in the same way as for L</Command_opt> given above, except that no default is | ||||||||||||
| 134 | handled here. Instead, if the variable already has a value that will be taken | ||||||||||||
| 135 | as its default. | ||||||||||||
| 136 | |||||||||||||
| 137 | =cut | ||||||||||||
| 138 | |||||||||||||
| 139 | 2 | ▶ | sub import ( $pkg, @syms ) | ||||||||||
|
Subroutine
import: called CC=4 SCAR=25.8
| |||||||||||||
| 140 | { | ||||||||||||
| 141 | 2 | my $caller = caller; | |||||||||||
| 142 | |||||||||||||
| 143 | 2 | foreach ( @syms ) { | |||||||||||
| 144 | 0 | ▶ | if( $_ eq ":attrs" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 0 | ▶ | HAVE_ATTRIBUTE_STORAGE or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | croak "Cannot import :attrs as Attribute::Storage is not available"; | ||||||||||||
| 147 | |||||||||||||
| 148 | 0 | require Commandable::Finder::SubAttributes::Attrs; | |||||||||||
| 149 | 0 | Commandable::Finder::SubAttributes::Attrs->import_into( $caller ); | |||||||||||
| 150 | 0 | next; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | 0 | croak "Unrecognised import symbol $_"; | |||||||||||
| 154 | } | ||||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | =head1 CONSTRUCTOR | ||||||||||||
| 158 | |||||||||||||
| 159 | =cut | ||||||||||||
| 160 | |||||||||||||
| 161 | =head2 new | ||||||||||||
| 162 | |||||||||||||
| 163 | $finder = Commandable::Finder::SubAttributes->new( %args ) | ||||||||||||
| 164 | |||||||||||||
| 165 | Constructs a new instance of C<Commandable::Finder::SubAttributes>. | ||||||||||||
| 166 | |||||||||||||
| 167 | Takes the following named arguments: | ||||||||||||
| 168 | |||||||||||||
| 169 | =over 4 | ||||||||||||
| 170 | |||||||||||||
| 171 | =item package => STR | ||||||||||||
| 172 | |||||||||||||
| 173 | The name of the package to look in for command subs. | ||||||||||||
| 174 | |||||||||||||
| 175 | =item name_prefix => STR | ||||||||||||
| 176 | |||||||||||||
| 177 | Optional. Gives the name prefix to use to filter for subs that actually | ||||||||||||
| 178 | provide a command, and to strip off to find the name of the command. Default | ||||||||||||
| 179 | C<command_>. | ||||||||||||
| 180 | |||||||||||||
| 181 | =item underscore_to_hyphen => BOOL | ||||||||||||
| 182 | |||||||||||||
| 183 | Optional. If true, sub names that contain underscores will be converted into | ||||||||||||
| 184 | hyphens. This is often useful in CLI systems, allowing commands to be typed | ||||||||||||
| 185 | with hyphenated names (e.g. "get-thing") while the Perl sub that implements it | ||||||||||||
| 186 | is named with an underscores (e.g. "command_get_thing"). Defaults true, but | ||||||||||||
| 187 | can be disabled by passing a defined-but-false value such as C<0> or C<''>. | ||||||||||||
| 188 | |||||||||||||
| 189 | =back | ||||||||||||
| 190 | |||||||||||||
| 191 | Any additional arguments are passed to the C<configure> method to be used as | ||||||||||||
| 192 | configuration options. | ||||||||||||
| 193 | |||||||||||||
| 194 | =cut | ||||||||||||
| 195 | |||||||||||||
| 196 | 0 | ▶ | sub new ( $class, %args ) | ||||||||||
|
Subroutine
new: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||
| 197 | { | ||||||||||||
| 198 | 0 | ▶ | HAVE_ATTRIBUTE_STORAGE or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 199 | croak "Cannot create a $class as Attribute::Storage is not available"; | ||||||||||||
| 200 | |||||||||||||
| 201 | 0 | ▶ | my $package = ( delete $args{package} ) or croak "Require 'package'"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 202 | |||||||||||||
| 203 | 0 | ▶ | my $name_prefix = ( delete $args{name_prefix} ) // "command_"; | ||||||||||
|
Condition0%0/2
delete $args{"name_prefix"} // "command_"
MC/DC0%0/1
delete $args{"name_prefix"} // "command_"
delete $args{"name_prefix"}
Truth table0%0/2
A
A= delete $args{"name_prefix"}
| |||||||||||||
| 204 | 0 | ▶ | my $conv_under = ( delete $args{underscore_to_hyphen} ) // 1; | ||||||||||
|
Condition0%0/2
delete $args{"underscore_to_hyphen"} // 1
MC/DC0%0/1
delete $args{"underscore_to_hyphen"} // 1
delete $args{"underscore_to_hyphen"}
Truth table0%0/2
A
A= delete $args{"underscore_to_hyphen"}
| |||||||||||||
| 205 | |||||||||||||
| 206 | 0 | my $self = bless { | |||||||||||
| 207 | package => $package, | ||||||||||||
| 208 | name_prefix => $name_prefix, | ||||||||||||
| 209 | conv_under => $conv_under, | ||||||||||||
| 210 | }, $class; | ||||||||||||
| 211 | |||||||||||||
| 212 | 0 | ▶ | $self->configure( %args ) if %args; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 213 | |||||||||||||
| 214 | # TODO: This package name should probably be separately configurable | ||||||||||||
| 215 | 0 | ▶ | if( my %global_opts = Attribute::Storage::find_vars_with_attr( $package, "GlobalOption" ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 216 | 0 | foreach my $varname ( sort keys %global_opts ) { | |||||||||||
| 217 | 0 | my $varref = $global_opts{$varname}; | |||||||||||
| 218 | |||||||||||||
| 219 | 0 | my $optspec = Attribute::Storage::get_varattr( $varref, "GlobalOption" ); | |||||||||||
| 220 | 0 | my ( $name, $description ) = @$optspec; | |||||||||||
| 221 | |||||||||||||
| 222 | 0 | my %optspec = ( | |||||||||||
| 223 | name => $name, | ||||||||||||
| 224 | description => $description, | ||||||||||||
| 225 | into => $varref, | ||||||||||||
| 226 | ); | ||||||||||||
| 227 | |||||||||||||
| 228 | 0 | ▶ | $optspec{default} = $$varref if defined $$varref; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 229 | |||||||||||||
| 230 | 0 | $self->add_global_options( \%optspec ); | |||||||||||
| 231 | } | ||||||||||||
| 232 | } | ||||||||||||
| 233 | |||||||||||||
| 234 | 0 | return $self; | |||||||||||
| 235 | } | ||||||||||||
| 236 | |||||||||||||
| 237 | =head2 new_for_caller | ||||||||||||
| 238 | |||||||||||||
| 239 | =head2 new_for_main | ||||||||||||
| 240 | |||||||||||||
| 241 | $finder = Commandable::Finder::SubAttributes->new_for_caller( %args ) | ||||||||||||
| 242 | $finder = Commandable::Finder::SubAttributes->new_for_main( %args ) | ||||||||||||
| 243 | |||||||||||||
| 244 | Convenient wrapper constructors that pass either the caller's package name or | ||||||||||||
| 245 | C<main> as the package name. Combined with the C<find_and_invoke_ARGV> method | ||||||||||||
| 246 | these are particularly convenient for wrapper scripts: | ||||||||||||
| 247 | |||||||||||||
| 248 | #!/usr/bin/perl | ||||||||||||
| 249 | |||||||||||||
| 250 | use v5.14; | ||||||||||||
| 251 | use warnings; | ||||||||||||
| 252 | |||||||||||||
| 253 | use Commandable::Finder::SubAttributes ':attrs'; | ||||||||||||
| 254 | |||||||||||||
| 255 | exit Commandable::Finder::SubAttributes->new_for_main | ||||||||||||
| 256 | ->find_and_invoke_ARGV; | ||||||||||||
| 257 | |||||||||||||
| 258 | # command subs go here... | ||||||||||||
| 259 | |||||||||||||
| 260 | =cut | ||||||||||||
| 261 | |||||||||||||
| 262 | 0 | ▶ | sub new_for_caller ( $class, @args ) | ||||||||||
|
Subroutine
new_for_caller: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 263 | { | ||||||||||||
| 264 | 0 | return $class->new( package => scalar caller, @args ); | |||||||||||
| 265 | } | ||||||||||||
| 266 | |||||||||||||
| 267 | 0 | ▶ | sub new_for_main ( $class, @args ) | ||||||||||
|
Subroutine
new_for_main: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 268 | { | ||||||||||||
| 269 | 0 | return $class->new( package => "main", @args ); | |||||||||||
| 270 | } | ||||||||||||
| 271 | |||||||||||||
| 272 | 0 | ▶ | sub _wrap_code ( $self, $code ) | ||||||||||
|
Subroutine
_wrap_code: not called CC=1 SCAR=6.9
| |||||||||||||
| 273 | { | ||||||||||||
| 274 | 0 | return $code; | |||||||||||
| 275 | } | ||||||||||||
| 276 | |||||||||||||
| 277 | 0 | ▶ | sub _commands ( $self ) | ||||||||||
|
Subroutine
_commands: not called CC=5 SCAR=34.0
| |||||||||||||
| 278 | { | ||||||||||||
| 279 | 0 | my $prefix = qr/$self->{name_prefix}/; | |||||||||||
| 280 | |||||||||||||
| 281 | my %subs = Attribute::Storage::find_subs_with_attr( | ||||||||||||
| 282 | 0 | $self->{package}, "Command_description", | |||||||||||
| 283 | matching => qr/^$prefix/, | ||||||||||||
| 284 | ); | ||||||||||||
| 285 | |||||||||||||
| 286 | 0 | my %commands; | |||||||||||
| 287 | |||||||||||||
| 288 | 0 | foreach my $subname ( keys %subs ) { | |||||||||||
| 289 | 0 | my $code = $subs{$subname}; | |||||||||||
| 290 | |||||||||||||
| 291 | 0 | my $name = $subname =~ s/^$prefix//r; | |||||||||||
| 292 | 0 | ▶ | $name =~ s/_/-/g if $self->{conv_under}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 293 | |||||||||||||
| 294 | 0 | my $args; | |||||||||||
| 295 | 0 | ▶ | if( $args = Attribute::Storage::get_subattr( $code, "Command_arg" ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 296 | 0 | $args = [ map { Commandable::Command::_Argument->new( %$_ ) } @$args ]; | |||||||||||
| 297 | } | ||||||||||||
| 298 | |||||||||||||
| 299 | 0 | my $opts; | |||||||||||
| 300 | 0 | ▶ | if( $opts = Attribute::Storage::get_subattr( $code, "Command_opt" ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 301 | 0 | $opts = { map { my $o = Commandable::Command::_Option->new( %$_ ); | |||||||||||
| 302 | 0 | map { ( $_ => $o ) } $o->names | |||||||||||
| 303 | } @$opts }; | ||||||||||||
| 304 | } | ||||||||||||
| 305 | |||||||||||||
| 306 | $commands{ $name } = Commandable::Command->new( | ||||||||||||
| 307 | name => $name, | ||||||||||||
| 308 | description => Attribute::Storage::get_subattr( $code, "Command_description" ), | ||||||||||||
| 309 | arguments => $args, | ||||||||||||
| 310 | options => $opts, | ||||||||||||
| 311 | package => $self->{package}, | ||||||||||||
| 312 | 0 | code => $self->_wrap_code( $code ), | |||||||||||
| 313 | ); | ||||||||||||
| 314 | } | ||||||||||||
| 315 | |||||||||||||
| 316 | 0 | $self->add_builtin_commands( \%commands ); | |||||||||||
| 317 | |||||||||||||
| 318 | 0 | return \%commands; | |||||||||||
| 319 | } | ||||||||||||
| 320 | |||||||||||||
| 321 | 0 | ▶ | sub find_commands ( $self ) | ||||||||||
|
Subroutine
find_commands: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 322 | { | ||||||||||||
| 323 | 0 | return values $self->_commands->%*; | |||||||||||
| 324 | } | ||||||||||||
| 325 | |||||||||||||
| 326 | 0 | ▶ | sub find_command ( $self, $cmd ) | ||||||||||
|
Subroutine
find_command: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 327 | { | ||||||||||||
| 328 | 0 | return $self->_commands->{$cmd}; | |||||||||||
| 329 | } | ||||||||||||
| 330 | |||||||||||||
| 331 | =head1 AUTHOR | ||||||||||||
| 332 | |||||||||||||
| 333 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 334 | |||||||||||||
| 335 | =cut | ||||||||||||
| 336 | |||||||||||||
| 337 | 3 | 0x55AA; | |||||||||||