.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "CLI::Framework::Command 3pm" .TH CLI::Framework::Command 3pm "2011-03-10" "perl v5.20.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" CLI::Framework::Command \- CLIF Command superclass .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& # The code below shows a few of the methods your command classes are likely \& # to override... \& \& package My::Journal::Command::Search; \& use base qw( CLI::Framework::Command ); \& \& sub usage_text { q{ \& search [\-\-titles\-only] : search a journal \& } } \& \& sub option_spec { ( \& [ \*(Aqtitles\-only\*(Aq => \*(Aqsearch only journal titles\*(Aq ], \& ) } \& \& sub validate { \& my $self, $opts, @args) = @_; \& die "exactly one argument required (search regex)" unless @args == 1; \& } \& \& sub run { \& my ($self, $opts, @args) = @_; \& \& my $db = $self\->cache\->get( \*(Aqdb\*(Aq ) \& \& # perform search against $db... \& # $search_results = ... \& \& return $search_results; \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" CLI::Framework::Command (command class for use with CLI::Framework::Application) is the base class for \s-1CLIF\s0 commands. All \s-1CLIF\s0 commands inherit from this class. .SH "CONCEPTS" .IX Header "CONCEPTS" .IP "Subcommands" 4 .IX Item "Subcommands" Commands can have \*(L"subcommands,\*(R" which are also objects of CLI::Framework::Command. Subcommands can, in turn, have their own subcommands, and this pattern may repeat indefinitely. .Sp \&\fBNote\fR that in this documentation, the term \*(L"command\*(R" may be used to refer to both commands and subcommands. .IP "Registration of subcommands" 4 .IX Item "Registration of subcommands" Subcommands are \*(L"registered\*(R" with their parent commands. The parent commands can then forward subcommand responsibilities as appropriate. .IP "File-based commands vs. inline commands" 4 .IX Item "File-based commands vs. inline commands" Command classes (which inherit from CLI::Framework::Command) can be defined in their own package files or they may be declared inline in another package (e.g. a command package file could include the declaration of a subcommand package or command packages could be declared inline in the package file where the application is declared). As long as the classes have been loaded (making their way into the symbol table), \s-1CLIF\s0 can use the commands. .SH "OBJECT CONSTRUCTION" .IX Header "OBJECT CONSTRUCTION" .ie n .SS "manufacture( $command_package )" .el .SS "manufacture( \f(CW$command_package\fP )" .IX Subsection "manufacture( $command_package )" .Vb 2 \& # (manufacture MyApp::Command::Go and any subcommand trees beneath it) \& my $go = CLI::Framework::Command\->manufacture( \*(AqMyApp::Command::Go\*(Aq ); .Ve .PP CLI::Framework::Command is an abstract factory; \f(CW\*(C`manufacture()\*(C'\fR is the factory method that constructs and returns an object of the specific command class that is requested. .PP After instantiating an object of the requested command package, \f(CW\*(C`manufacture()\*(C'\fR attempts to load subcommands in the following 2 steps: .IP "1." 4 Attempt to find package \fBfiles\fR representing subcommands. For every subcommand S, S is registered as a child of the parent command. Next, steps 1 and 2 repeat, this time being invoked on S (i.e. with S as the parent in an attempt to find subcommands of S). .IP "2." 4 Attempt to find and register pre-compiled subcommands defined \fBinline\fR. Only pre-compiled subcommands are considered for registration (i.e. package files are not considered in this step). For every subcommand S, any pre-compiled subcommands that inherit \fBdirectly\fR from S are found and step 2 repeats for those classes. .PP Note the following rules about command class definition: .IP "\(bu" 4 If a command class is defined inline, its subcommand classes must be defined inline as well. .IP "\(bu" 4 If a command class is file-based, each of its subcommand classes can be either file-based or inline. Furthermore, it is not necessary for all of these subcommand classes to be defined in the same way \*(-- a mixture of file-based and inline styles can be used for the subcommands of a given command. .SS "\fInew()\fP" .IX Subsection "new()" .Vb 1 \& $object = $cli_framework_command_subclass\->new(); .Ve .PP Basic constructor. .SH "SHARED CACHE DATA" .IX Header "SHARED CACHE DATA" \&\s-1CLIF\s0 commands may need to share data with other commands and with their associated application. These methods support those needs. .ie n .SS "set_cache( $cache_object )" .el .SS "set_cache( \f(CW$cache_object\fP )" .IX Subsection "set_cache( $cache_object )" Set the internal cache object for this instance. .PP See cache. .SS "\fIcache()\fP" .IX Subsection "cache()" Retrieve the internal cache object for this instance. .PP See cache for an explanation of how to use this simple cache object. .SH "COMMAND DISPATCHING" .IX Header "COMMAND DISPATCHING" .ie n .SS "\fIget_default_usage()\fP / set_default_usage( $default_usage_text )" .el .SS "\fIget_default_usage()\fP / set_default_usage( \f(CW$default_usage_text\fP )" .IX Subsection "get_default_usage() / set_default_usage( $default_usage_text )" Get or set the default usage message for the command. This message is used by usage. .PP \&\fBNote\fR: \f(CW\*(C`get_default_usage()\*(C'\fR merely retrieves the usage data that has already been set. \s-1CLIF\s0 only sets the default usage message for a command when processing a run request for the command. Therefore, the default usage message for a command may be empty (if a run request for the command has not been given and you have not otherwise set the default usage message). .PP .Vb 2 \& $cmd\->set_default_usage( ... ); \& $usage_msg = $cmd\->get_default_usage(); .Ve .ie n .SS "usage( $subcommand_name, @subcommand_chain )" .el .SS "usage( \f(CW$subcommand_name\fP, \f(CW@subcommand_chain\fP )" .IX Subsection "usage( $subcommand_name, @subcommand_chain )" .Vb 2 \& # Command usage... \& print $cmd\->usage(); \& \& # Subcommand usage (to any level of depth)... \& $subcommand_name = \*(Aqlist\*(Aq; \& @subcommand_chain = qw( completed ); \& print $cmd\->usage( $subcommand_name, @subcommand_chain ); .Ve .PP Attempts to find and return a usage message for a command or subcommand. .PP If a subcommand is given, returns a usage message for that subcommand. If no subcommand is given or if the subcommand cannot produce a usage message, returns a general usage message for the command. .PP Logically, here is how the usage message is produced: .IP "\(bu" 4 If registered subcommand(s) are given, attempt to get usage message from a subcommand (\fBNote\fR that a sequence of subcommands could be given, e.g. \&\f(CW\*(C`$cmd\->usage(\*(Aqlist\*(Aq \*(Aqcompleted\*(Aq)\*(C'\fR, which would result in the usage message for the final subcommand, \f(CW\*(Aqcompleted\*(Aq\fR). If no usage message is defined for the subcommand, the usage message for the command is used instead. .IP "\(bu" 4 If the command has implemented usage_text, its return value is used as the usage message. .IP "\(bu" 4 Finally, if no usage message has been found, the default usage message produced by get_default_usage is returned. .ie n .SS "dispatch( $cmd_opts, @args )" .el .SS "dispatch( \f(CW$cmd_opts\fP, \f(CW@args\fP )" .IX Subsection "dispatch( $cmd_opts, @args )" For the given command request, \f(CW\*(C`dispatch\*(C'\fR performs any applicable validation and initialization with respect to supplied options \f(CW$cmd_opts\fR and arguments \&\f(CW@args\fR, then runs the command. .PP \&\f(CW@args\fR may indicate the request for a subcommand: .PP .Vb 1 \& { [subcmd\-opts] {...} } [subcmd\-args] .Ve .PP \&...as in the following command (where \*(L"usage\*(R" is the ): .PP .Vb 1 \& $ gen\-report \-\-html stats \-\-role=admin usage \-\-time=\*(Aq2d\*(Aq \*(Aq/tmp/stats.html\*(Aq .Ve .PP If a subcommand registered under the indicated command is requested, the subcommand is initialized and dispatched with its options \&\f(CW\*(C`[subcmd\-opts]\*(C'\fR and arguments. Otherwise, the command itself is run. .PP This means that a request for a subcommand will result in the \f(CW\*(C`run\*(C'\fR method of only the deepest-nested subcommand (because \f(CW\*(C`dispatch\*(C'\fR will keep forwarding to successive subcommands until the args no longer indicate that a subcommand is requested). Furthermore, the only command that can receive args is the final subcommand in the chain (but all commands in the chain can receive options). However, \fBNote\fR that each command in the chain can affect the execution process through its notify_of_subcommand_dispatch method. .SH "COMMAND REGISTRATION" .IX Header "COMMAND REGISTRATION" .SS "\fIregistered_subcommand_names()\fP" .IX Subsection "registered_subcommand_names()" .Vb 1 \& @registered_subcommands = $cmd\->registered_subcommand_names(); .Ve .PP Return a list of the currently-registered subcommands. .ie n .SS "registered_subcommand_object( $subcommand_name )" .el .SS "registered_subcommand_object( \f(CW$subcommand_name\fP )" .IX Subsection "registered_subcommand_object( $subcommand_name )" .Vb 1 \& $subcmd_obj = $cmd\->get_registered_subcommand( \*(Aqlock\*(Aq ); .Ve .PP Given the name of a registered subcommand, return a reference to the subcommand object. If the subcommand is not registered, returns undef. .ie n .SS "register_subcommand( $subcmd_obj )" .el .SS "register_subcommand( \f(CW$subcmd_obj\fP )" .IX Subsection "register_subcommand( $subcmd_obj )" .Vb 1 \& $cmd\->register_subcommand( $subcmd_obj ); .Ve .PP Register \f(CW$subcmd_obj\fR as a subcommand under master command \f(CW$cmd\fR. .PP If \f(CW$subcmd_obj\fR is not a CLI::Framework::Command, returns undef. Otherwise, returns \f(CW$subcmd_obj\fR. .ie n .SS "package_is_registered( $package_name )" .el .SS "package_is_registered( \f(CW$package_name\fP )" .IX Subsection "package_is_registered( $package_name )" Return a true value if the named class is registered as a subcommand. Returns a false value otherwise. .SS "\fIname()\fP" .IX Subsection "name()" .Vb 2 \& $s = My::Command::Squeak\->new(); \& $s\->name(); # => \*(Aqsqueak\*(Aq .Ve .PP \&\f(CW\*(C`name()\*(C'\fR takes no arguments and returns the name of the command. This method uses the normalized base name of the package as the command name, e.g. the command defined by the package My::Application::Command::Xyz would be named 'xyz'. .SH "COMMAND SUBCLASS HOOKS" .IX Header "COMMAND SUBCLASS HOOKS" Just as \s-1CLIF\s0 Applications have hooks that subclasses can use, \s-1CLIF\s0 Commands are able to influence the command dispatch process via several hooks. Except where noted, all hooks are optional \*(-- subclasses may choose whether or not to override them. .SS "\fIoption_spec()\fP" .IX Subsection "option_spec()" This method should return an option specification as expected by Getopt::Long::Descriptive (see Getopt::Long::Descriptive). The option specification is a list of arrayrefs that defines recognized options, types, multiplicities, etc. and specifies textual strings that are used as descriptions of each option: .PP .Vb 4 \& sub option_spec { \& [ "verbose|v" => "be verbose" ], \& [ "logfile=s" => "path to log file" ], \& } .Ve .PP Subclasses should override this method if commands accept options (otherwise, the command will not recognize any options). .SS "\fIsubcommand_alias()\fP" .IX Subsection "subcommand_alias()" .Vb 6 \& sub subcommand_alias { \& rm => \*(Aqremove\*(Aq, \& new => \*(Aqcreate\*(Aq, \& j => \*(Aqjump\*(Aq, \& r => \*(Aqrun\*(Aq, \& } .Ve .PP Subcommands can have aliases to support shorthand versions of subcommand names. .PP Subclasses should override this method if subcommand aliases are desired. Otherwise, the subcommands will only be recognized by their full command names. .ie n .SS "validate( $cmd_opts, @args )" .el .SS "validate( \f(CW$cmd_opts\fP, \f(CW@args\fP )" .IX Subsection "validate( $cmd_opts, @args )" To provide strict validation of a command request, a subclass may override this method. Otherwise, validation is skipped. .PP \&\f(CW$cmd_opts\fR is an options hash with the received command options as keys and their values as hash values. .PP \&\f(CW@args\fR is a list of the received command arguments. .PP \&\f(CW\*(C`validate()\*(C'\fR is called in void context. It is expected to throw an exception if validation fails. This allows your validation routine to provide a context-specific failure message. .PP \&\fBNote\fR that Getop::Long::Descriptive performs some validation of its own based on the option_spec. However, \f(CW\*(C`validate()\*(C'\fR allows more flexibility in validating command options and also allows validation of arguments. .ie n .SS "notify_of_subcommand_dispatch( $subcommand, $cmd_opts, @args )" .el .SS "notify_of_subcommand_dispatch( \f(CW$subcommand\fP, \f(CW$cmd_opts\fP, \f(CW@args\fP )" .IX Subsection "notify_of_subcommand_dispatch( $subcommand, $cmd_opts, @args )" If a request for a subcommand is received, the master command itself does not \&\f(CW\*(C`run()\*(C'\fR. Instead, its \f(CW\*(C`notify_of_subcommand_dispatch()\*(C'\fR method is called. This gives the master command a chance to act before the subcommand is run. .PP For example, suppose some (admittedly contrived) application, 'queue', has a command hierarchy with multiple commands: .PP .Vb 8 \& enqueue \& dequeue \& print \& property \& constraint \& maxlen \& behavior \& ... .Ve .PP In this case, \f(CW\*(C`$ queue property constraint maxlen\*(C'\fR might set the max length property for a queue. If the command hierarchy was built this way, \f(CW\*(C`maxlen\*(C'\fR would be the only command to \f(CW\*(C`run\*(C'\fR in response to that request. If \&\f(CW\*(C`constraint\*(C'\fR, the master command of \f(CW\*(C`maxlen\*(C'\fR, needs to hook into this execution path, \f(CW\*(C`notify_of_subcommand_dispatch()\*(C'\fR could be overridden in the command class that implements \f(CW\*(C`constraint\*(C'\fR. \f(CW\*(C`notify_of_subcommand_dispatch()\*(C'\fR would then be called just before \f(CW\*(C`dispatch\*(C'\fRing \f(CW\*(C`maxlen\*(C'\fR. .PP The \f(CW\*(C`notify_of_subcommand_dispatch()\*(C'\fR method is called in void context. .PP \&\f(CW$subcommand\fR is the subcommand object. .PP \&\f(CW$cmd_opts\fR is the options hash for the subcommand. .PP \&\f(CW@args\fR is the argument list for the subcommand. .SS "\fIusage_text()\fP" .IX Subsection "usage_text()" .Vb 5 \& sub usage_text { \& q{ \& dequeue: remove item from queue \& } \& } .Ve .PP If implemented, this method should simply return a string containing usage information for the command. It is used automatically to provide context-specific help. .PP Implementing this method is optional. See usage for details on how usage information is generated within the context of a \s-1CLIF\s0 application. .PP Users are encouraged to override this method. .ie n .SS "run( $cmd_opts, @args )" .el .SS "run( \f(CW$cmd_opts\fP, \f(CW@args\fP )" .IX Subsection "run( $cmd_opts, @args )" This method is responsible for the main execution of a command. It is called with the following parameters: .PP \&\f(CW$cmd_opts\fR is a pre-validated options hash with command options as keys and their user-provided values as hash values. .PP \&\f(CW@args\fR is a list of the command arguments. .PP The default implementation of this method simply calls usage to show help information for the command. Therefore, subclasses will usually override \f(CW\*(C`run()\*(C'\fR (Occasionally, it is useful to have a command that does little or nothing on its own but has subcommands that define the real behavior. In such occasional cases, it may not be necessary to override \f(CW\*(C`run()\*(C'\fR). .PP If an error occurs during the execution of a command via its \f(CW\*(C`run()\*(C'\fR method, the \f(CW\*(C`run()\*(C'\fR method code should throw an exception. The exception will be caught and handled appropriately by \s-1CLIF.\s0 .PP The return value of \f(CW\*(C`run()\*(C'\fR is treated as data to be processed by the render method in your \s-1CLIF\s0 Application class. \fBNote that nothing should be printed directly in your implementation of \&\f(CB\*(C`run\*(C'\fB\fR. If no output is to be produced, your \f(CW\*(C`run()\*(C'\fR method should return \&\f(CW\*(C`undef\*(C'\fR or empty string. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" .ie n .IP """Error: failed to instantiate command package \*(Aq\*(Aq via new()""" 4 .el .IP "\f(CWError: failed to instantiate command package \*(Aq\*(Aq via new()\fR" 4 .IX Item "Error: failed to instantiate command package via new()" manufacture was asked to manufacture an object of class , but failed while trying to invoke its constructor. .ie n .IP """Error: failed to instantiate subcommand \*(Aq\*(Aq via method new()""" 4 .el .IP "\f(CWError: failed to instantiate subcommand \*(Aq\*(Aq via method new()\fR" 4 .IX Item "Error: failed to instantiate subcommand via method new()" Object construction for the subcommand (whose package has already been \&\f(CW\*(C`require()d\*(C'\fR) was unsuccessful. .ie n .IP """cannot opendir """ 4 .el .IP "\f(CWcannot opendir \fR" 4 .IX Item "cannot opendir " While trying to manufacture subcommands in a directory tree, calling \f(CW\*(C`opendir()\*(C'\fR on the subdirectory with the name of the parent command failed. .SH "CONFIGURATION & ENVIRONMENT" .IX Header "CONFIGURATION & ENVIRONMENT" No special configuration requirements. .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" Carp .PP Getopt::Long::Descriptive .PP Exception::Class::TryCatch .PP Class::Inspector .PP CLI::Framework::Exceptions .SH "SEE ALSO" .IX Header "SEE ALSO" CLI::Framework .PP CLI::Framework::Application .PP CLI::Framework::Tutorial .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" Copyright (c) 2009 Karl Erisman (kerisman@cpan.org). All rights reserved. .PP This is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. .SH "AUTHOR" .IX Header "AUTHOR" Karl Erisman (kerisman@cpan.org)