Comman...ibutes(3pm)
π Commandable::Finder::SubAttributes β the vibe
okay bestie, so this is User Contributed Perl Documentation for the module Commandable::Finder::SubAttributes, and no cap, it's the one that finds commands stored as subs with attributes. iykyk β¨
β¨ NAME β¨
Commandable::Finder::SubAttributes β find commands stored as subs with attributes π
π¦ SYNOPSIS π¦
use Commandable::Finder::SubAttributes;
my $finder = Commandable::Finder::SubAttributes->new(
package => "MyApp::Commands",
);
my $help_command = $finder->find_command( "help" );
foreach my $command ( $finder->find_commands ) {
...
}
πΈ DESCRIPTION πΈ
So this is one of the implementations of Commandable::Finder, and what it does is it goes looking for functions that define commands β where every single command is provided by its own individual sub living in a given package. one sub, one command, it's giving organized π
π ATTRIBUTES π
use Commandable::Finder::SubAttributes ':attrs';
sub command_example
:Command_description("An example of a command")
{
...
}
the way that the properties about each command get stored as attributes on the named function?? iconic. it uses Attribute::Storage for that, fr fr.
and when you import with the :attrs symbol, these are the attributes you get on the calling package:
π Command_description
:Command_description("description text")
gives a plain string description text for the command. simple, clean, slay β¨
π Command_arg
:Command_arg("argname", "description")
gives a named argument for the command, plus its description.
- if the name is suffixed by a
"?", then this argument is optional. (the"?"itself gets removed from the name, bestie β it's just a flag, not part of the name.) - if the name is suffixed by a
"...", this argument is slurpy. (and yes, the"..."itself gets removed from the name too.)
π Command_opt
:Command_opt("optname", "description")
:Command_opt("optname", "description", "default")
gives a named option for the command and its description.
- if the name contains
"|"characters, it provides multiple name aliases for the same option. so many names, one option, main character energy π - if the name field ends in a
"="character, a value is expected for the option. it can either be parsed from the next input token, or after an"="sign of the same token:
--optname VALUE
--optname=VALUE
- if the name field ends in a
"@"character, a value is expected for the option and it can be specified multiple times. all the values get collected into an ARRAY reference. - if the name field ends in a
"+"character, the option can be specified multiple times and the total count gets used as the value. counting era π₯ - if the name field ends in a
"!"character, the option is negatable. an option name of--no-OPTNAMEis recognised and will reset the value toundef. by setting a default of some true value (e.g.1) you can detect if this has happened. sneaky but useful π - an optional third argument may be present to specify a default value, if it's not provided by the invocation.
π GLOBAL OPTION ATTRIBUTES π
since version 0.14, this module also lets you attach attributes to package variables in the package that stores the subroutines (often main), and those get handled automatically as global options by the finder. glow up fr π
β οΈ bestie pls remember: these HAVE to be package variables (i.e. declared with"our"). lexical variables (declared with"my") will NOT work. don't do it. it will not vibe.
our $VERBOSE
:GlobalOption("verbose|v+", "Increase the verbosity of status output");
this often serves as a convenient alternative to modules like Getopt::Long, because it integrates with the "help" command automatically. obsessed π«Ά
π GlobalOption
:GlobalOption("optname", "description")
gives the name for this global option and its description. these get handled in the same way as for "Command_opt" given above, except that no default is handled here. instead, if the variable already has a value, that will be taken as its default. cute, right? πΈ
π CONSTRUCTOR π
π new
$finder = Commandable::Finder::SubAttributes->new( %args )
constructs a new instance of Commandable::Finder::SubAttributes. periodt.
takes the following named arguments:
package => STRβ the name of the package to look in for command subs.name_prefix => STRβ optional. gives the name prefix to use to filter for subs that actually provide a command, and to strip off to find the name of the command. default is"command_".underscore_to_hyphen => BOOLβ optional. if true, sub names that contain underscores will be converted into hyphens. this is often useful in CLI systems, allowing commands to be typed with hyphenated names (e.g."get-thing") while the Perl sub that implements it is named with underscores (e.g."command_get_thing"). defaults true, but can be disabled by passing a defined-but-false value such as0or''. so specific, love that for us β¨
any additional arguments are passed to the "configure" method to be used as configuration options.
π new_for_caller
π new_for_main
$finder = Commandable::Finder::SubAttributes->new_for_caller( %args )
$finder = Commandable::Finder::SubAttributes->new_for_main( %args )
convenient wrapper constructors that pass either the caller's package name or "main" as the package name. combined with the "find_and_invoke_ARGV" method, these are particularly convenient for wrapper scripts. it's giving effortless π¦
#!/usr/bin/perl
use v5.14;
use warnings;
use Commandable::Finder::SubAttributes ':attrs';
exit Commandable::Finder::SubAttributes->new_for_main
->find_and_invoke_ARGV;
# command subs go here...
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk> β the legend themselves π
and that's the whole page bestie, ty for coming to my ted talk π this was perl v5.40.0, dated 2024-09-08, and yeah β go write some commands, you're gonna ate π
β¨
π¦ where this page comes from
- ubuntu resolute: libcommandable-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcommandable-perl 0.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms