| 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-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package Commandable 0.14; | ||
| 7 | |||
| 8 | 1 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | C<Commandable> - utilities for commandline-based programs | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | This distribution contains a collection of utilities extracted from various | ||
| 18 | commandline-based programs I have written, in the hope of trying to find a | ||
| 19 | standard base to build these from in future. | ||
| 20 | |||
| 21 | Note that "commandline" does not necessarily mean "plain-text running in a | ||
| 22 | terminal"; simply that the mode of operation is that the user types a textual | ||
| 23 | representation of some action, and the program parses this text in order to | ||
| 24 | perform it. This could equally apply to a command input text area in a GUI | ||
| 25 | program. | ||
| 26 | |||
| 27 | =head1 PROGRAM STRUCTURE | ||
| 28 | |||
| 29 | A typical program using this distribution would have a single instance of a | ||
| 30 | "finder", whose job is to work out the set of commands offered by the program. | ||
| 31 | Various subclasses of finder are provided that use different techniques to | ||
| 32 | locate the individual commands, depending on the structure provided by the | ||
| 33 | program. | ||
| 34 | |||
| 35 | =over 4 | ||
| 36 | |||
| 37 | =item * | ||
| 38 | |||
| 39 | L<Commandable::Finder::SubAttributes> - expects to find each command | ||
| 40 | implemented as a subroutine within a single package. These subroutines should | ||
| 41 | all have attributes that provide description text, and specifications of | ||
| 42 | argument and option parsing. The code body of the subroutine is then used to | ||
| 43 | implement the actual command. | ||
| 44 | |||
| 45 | =item * | ||
| 46 | |||
| 47 | L<Commandable::Finder::MethodAttributes> - a variant of the above which | ||
| 48 | expects that commands are implemented as methods on an object instance. | ||
| 49 | |||
| 50 | =item * | ||
| 51 | |||
| 52 | L<Commandable::Finder::Packages> - expects to find each command implemented | ||
| 53 | as an entire package, with (constant) subroutines to give the description text | ||
| 54 | and argument and option parsing specifications. Another subroutine within the | ||
| 55 | package actually implements the command. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | As the user requests that commands be executed, the text of each request is | ||
| 60 | then wrapped in an instance of L<Commandable::Invocation>. This is then passed | ||
| 61 | to the finder instance to actually invoke a command by parsing its name, | ||
| 62 | options and arguments, and run the actual code body. | ||
| 63 | |||
| 64 | my $finder = Commandable::Finder::...->new( ... ); | ||
| 65 | |||
| 66 | my $cinv = Commandable::Invocation->new( $text ); | ||
| 67 | |||
| 68 | $finder->find_and_invoke( $cinv ); | ||
| 69 | |||
| 70 | The finder instance is not modified by individual invocations, and can be | ||
| 71 | reused if the program wishes to provide some sort of multiple invocation | ||
| 72 | ability; perhaps in the form of a REPL-like shell: | ||
| 73 | |||
| 74 | my $finder = ... | ||
| 75 | |||
| 76 | while( my $text = <STDIN> ) { | ||
| 77 | $finder->find_and_invoke( Commandable::Invocation->new( $text ) ); | ||
| 78 | } | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | =head1 AUTHOR | ||
| 83 | |||
| 84 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | 1 | 0x55AA; | |