The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Commandable::Command - represent metadata for an invokable command

ACCESSORS

The following simple methods return metadata fields about the command

name

description

   $name = $command->name;
   $desc = $command->description;

Strings giving the short name (to be used on a commandline), and descriptive text for the command.

arguments

   @args = $command->arguments;

A (possibly-empty) list of argument metadata structures.

options

   %opts = $command->options;

A (possibly-empty) kvlist of option metadata structures.

package

   $pkg = $command->packaage;

The package name as a plain string.

code

   $sub = $command->code;

A CODE reference to the code actually implementing the command.

config

   $conf = $command->config;

A HASH reference to the configuration of the command.

METHODS

parse_invocation

   @vals = $command->parse_invocation( $cinv );

Parses values out of a Commandable::Invocation instance according to the specification for the command's arguments. Returns a list of perl values suitable to pass into the function implementing the command.

This method will throw an exception if mandatory arguments are missing.

ARGUMENT SPECIFICATIONS

Each argument specification is given by an object having the following structure:

name

description

   $name = $argspec->name;

   $desc = $argspec->description;

Text strings for the user, used to generate the help text.

optional

   $bool = $argspec->optional;

If false, the option is mandatory and an error is raised if no value is provided for it. If true, it is optional and if absent an undef will passed instead.

slurpy

   $bool = $argspec->slurpy;

If true, the argument will be passed as an ARRAY reference containing the entire remaining list of tokens provided by the user.

OPTION SPECIFICATIONS

Each option specification is given by an object having the following structure:

name

   $name = $optspec->name;

A string giving the name of the option. This is the name it will be given in the options hash provided to the command subroutine.

names

   @names = $optspec->names;

A list containing the name plus all the aliases this option is known by.

description

   $desc = $optspec->description;

A text string containing information for the user, used to generate the help text.

mode

   $mode = $optspec->mode;

A string that describes the behaviour of the option.

set options do not expect a value to be suppled by the user, and will store a true value in the options hash if present.

value options take a value from the rest of the token, or the next token.

   --opt=value
   --opt value

multi_value options can be supplied more than once; values are pushed into an ARRAY reference which is passed in the options hash.

inc options may be supplied more than once; each occurance will increment the stored value by one.

default

   $val = $optspec->default;

A value to provide in the options hash if the user did not specify a different one.

negatable

   $bool = $optspec->negatable;

If true, also accept a --no-OPT option to reset the value of the option to undef.

typespec

   $type = $optspec->typespec;

If defined, gives a type specification that any user-supplied value must conform to.

The i type must be a string giving a (possibly-negative) decimal integer.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>