App-Framework
view release on metacpan or search on metacpan
lib/App/Framework/Feature/Options.pm view on Meta::CPAN
package App::Framework::Feature::Options ;
=head1 NAME
App::Framework::Feature::Options - Handle application options
=head1 SYNOPSIS
# Options are loaded by default as if the script contained:
use App::Framework '+Options' ;
=head1 DESCRIPTION
Options feature that provides command line options handling.
Options are defined once in a text format and this text format generates
both the command line options data, but also the man pages, help text etc.
=head2 Option Definition
Options are specified in the application __DATA__ section in the format:
-<name><specification> <Summary> <optional default setting>
<Description>
These user-specified options are added to the application framework options (defined dependent on whatever core/features/extensions are installed).
Also, the user may over ride default settings and descriptions on any application framework options by re-defining them in the script.
The parts of the specification are defined below.
=head3 name
The name defines the option name to be used at the command line, along with any command line option aliases (e.g. -log or -l, -logfile etc). Using the
option in the script is via a HASH where the key is the 'main' option name.
Where an option has one or more aliases, this list of names is separated by '|'. By default, the first name defined is the 'main' option name used
as the option HASH key. This may be overridden by quoting the name that is required to be the main name.
For example, the following name definitions:
-log|logfile|l
-l|'log'|logfile
-log
Are all access by the key 'log'
=head3 specification
(Note: This is a subset of the specification supported by L<Getopt::Long>).
The specification is optional. If not defined, then the option is a boolean value - is the user specifies the option on the command line
then the option value is set to 1; otherwise the option value is set to 0.
When the specification is defined, it is in the format:
[ <flag> ] <type> [ <desttype> ]
The option requires an argument of the given type. Supported types
are:
=over 4
=item s
String. An arbitrary sequence of characters. It is valid for the
argument to start with C<-> or C<-->.
=item i
Integer. An optional leading plus or minus sign, followed by a
sequence of digits.
=item o
Extended integer, Perl style. This can be either an optional leading
plus or minus sign, followed by a sequence of digits, or an octal
string (a zero, optionally followed by '0', '1', .. '7'), or a
hexadecimal string (C<0x> followed by '0' .. '9', 'a' .. 'f', case
insensitive), or a binary string (C<0b> followed by a series of '0'
and '1').
=item f
Real number. For example C<3.14>, C<-6.23E24> and so on.
=back
The I<desttype> can be C<@> or C<%> to specify that the option is
list or a hash valued. This is only needed when the destination for
the option value is not otherwise specified. It should be omitted when
not needed.
The I<flag>, if used, can be C<dev:> to specify that the option is meant for application developer
use only. In this case, the option will not be shown in the normal help and man pages, but will
only be shown when the -man-dev option is used.
=head3 summary
The summary is a simple line of text used to summarise the option. It is used in the man pages in 'usage' mode.
=head3 default
Defaults values are optional. If they are defined, they are in the format:
[default=<value>]
When a default is defined, if the user does not specify a value for an option then that option takes on the defualt value.
=head3 description
The summary is multiple lines of text used to fully describe the option. It is used in the man pages in 'man' mode.
=head2 Variable Expansion
Option values and default values can contain variables, defined using the standard Perl format:
$<name>
${<name>}
When the option is used, the variable is expanded and replaced with a suitable value. The value will be looked up from a variety of possible sources:
object fields (where the variable name matches the field name) or environment variables.
The variable name is looked up in the following order, the first value found with a matching name is used:
=over 4
=item *
Option names - the values of any other options may be used as variables in options
=item *
Application fields - any fields of the $app object may be used as variables
=item *
Environment variables - if no application fields match the variable name, then the environment variables are used
=back
=head2 Script Usage
The application framework passes a reference to the options HASH as the second parameter to the application subroutine B<app>. Alternatively,
the script can call the app object's alias to the options accessor, i.e. the B<options> method which returns the options hash. Yet another
alternative is to call the options accessor method directly. These alternatives are shown below:
sub app
{
my ($app, $opts_href, $args_href) = @_ ;
# use parameter
my $log = $opts_href->{log}
# access alias
my %options = $app->options() ;
$log = $options{log} ;
# access alias
%options = $app->Options() ;
$log = $options{log} ;
# feature object
%options = $app->feature('Options')->options() ;
$log = $options{log} ;
}
=head2 Examples
With the following script definition:
[OPTIONS]
-n|'name'=s Test name [default=a name]
String option, accessed as $opts_href->{name}.
-nomacro Do not create test macro calls
Boolean option, accessed as $opts_href->{nomacro}
-log=s Override default [default=another default]
Over rides the default log option (specified by the framework)
-int=i An integer
Example of integer option
-float=f An float
Example of float option
-array=s@ An array
Example of an array option
-hash=s% A hash
Example of a hash option
The following command line options are valid:
-int 1234 -float 1.23 -array a -array b -array c -hash key1=val1 -hash key2=val2 -nomacro
Giving the options HASH values:
'name' => 'a name'
'nomacro' => 1
'log' => 'another default'
'int' => 1234
'float' => 1.23
'array' => [ 'a', 'b', 'c' ]
'hash' => {
'key1' => 'val1',
'key2' => 'val2',
}
=cut
use strict ;
use Carp ;
our $VERSION = "1.005" ;
#============================================================================================
# USES
#============================================================================================
use Getopt::Long qw(:config no_ignore_case) ;
use App::Framework::Feature ;
use App::Framework::Base ;
#============================================================================================
# OBJECT HIERARCHY
#============================================================================================
our @ISA = qw(App::Framework::Feature) ;
#============================================================================================
# GLOBALS
#============================================================================================
=head2 FIELDS
( run in 0.629 second using v1.01-cache-2.11-cpan-e1769b4cff6 )