App-Framework
view release on metacpan or search on metacpan
lib/App/Framework/Feature/Args.pm view on Meta::CPAN
package App::Framework::Feature::Args ;
=head1 NAME
App::Framework::Feature::Args - Handle application command line arguments
=head1 SYNOPSIS
# Args are loaded by default as if the script contained:
use App::Framework '+Args' ;
# Alternatives...
# Open no file handles
use App::Framework '+Args(open=none)' ;
# Open only input file handles
use App::Framework '+Args(open=in)' ;
# Open only output file handles
use App::Framework '+Args(open=out)' ;
# Open all file handles (the default)
use App::Framework '+Args(open=all)' ;
=head1 DESCRIPTION
Args feature that provides command line arguments handling.
Arguments are defined once in a text format and this text format generates
both the command line arguments data, but also the man pages, help text etc.
=head2 Argument Definition
Arguments are specified in the application __DATA__ section in the format:
* <name>=<specification> <Summary> <optional default setting>
<Description>
The parts of the specification are defined below.
=head3 name
The name defines the name of the key to use to access the argument value in the arguments hash. The application framework
passes a reference to the argument hash as the third parameter to the application subroutine B<app> (see L</Script Usage>)
=head3 specification
The specification is in the format:
[ <direction> ] [ <binary> ] <type> [ <multiple> ]
The optional I<direction> is only valid for file or directory types. For a file or directory types, if no direction is specified then
it is assumed to be input. Direction can be one of:
=over 4
=item <
An input file or directory
=item >
An output file or directory
=item >>
An output appended file
=back
An optional 'b' after the direction specifies that the file is binary mode (only used when the type is file).
The B<type> must be specified and may be one of:
=over 4
=item f
A file
=item d
A directory
=item s
Any string
=back
Additionally, an optional multiple can be specified. If used, this can only be specified on the last argument. When it is used, this tells the
application framework to use the last argument as an ARRAY, pushing all subsequent specified arguments onto this. Accessing the argument
in the script returns the ARRAY ref containing all of the command line argument values.
Multiple can be:
=over 4
=item '@'
One or more items
=item '*'
Zero or more items. There is also a special case (the real reason for *) where the argument specification is of the form '<f*' (input file multiple). Here, if the script user does not
specify any arguments on the command line for this argument then the framework opens STDIN and provides it as a file handle.
=back
=head3 summary
The summary is a simple line of text used to summarise the argument. 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 argument then that argument takes on the defualt value.
Also, all subsequent arguments must also be defined as optional.
=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 Feature Options
The Args feature allows control over how it opens files. By default, any input or output file definitions also create equivalent file handles
(the files being opened for read/write automatically). These file handles are made available only in the arguments HASH. The key name for the handle
being the name of the argument with the suffix '_fh'.
For example, the following definition:
[ARGS]
* file=f Input file
A simple input directory name (directory must exist)
* out=>f Output file (file will be created)
An output filename
And the command line arguments:
infile.txt outfile.txt
Results in the arguments HASH:
'file' => 'infile.txt'
'out' => 'outfile.txt'
'file_fh' => <file handle of 'infile.txt'>
'out_fh' => <file handle of 'outfile.txt'>
If this behaviour is not required, then you can get the framework to open just input files, output files, or none by using the 'open' option.
Specify this in the App::Framework 'use' line as an argument to the Args feature:
# Open no file handles
use App::Framework '+Args(open=none)' ;
# Open only input file handles
use App::Framework '+Args(open=in)' ;
# Open only output file handles
use App::Framework '+Args(open=out)' ;
# Open all file handles (the default)
use App::Framework '+Args(open=all)' ;
=head2 Variable Expansion
Argument values can contain variables, defined using the standard Perl format:
$<name>
${<name>}
When the argument 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 *
Argument names - the values of any other arguments may be used as variables in arguments
=item *
Option names - the values of any command line options may be used as variables in arguments
=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 argument HASH as the third parameter to the application subroutine B<app>. Alternatively,
the script can call the app object's alias to the args accessor, i.e. the B<args> method which returns the arguments value list. Yet another
alternative is to call the args accessor method directly. These alternatives are shown below:
sub app
{
my ($app, $opts_href, $args_href) = @_ ;
# use parameter
my $infile = $args_href->{infile}
# access alias
my @args = $app->args() ;
$infile = $args[0] ;
# access alias
@args = $app->Args() ;
$infile = $args[0] ;
($infile) = $app->args('infile') ;
# feature object
@args = $app->feature('Args')->args() ;
$infile = $args[0] ;
}
=head2 Examples
With the following script definition:
[ARGS]
* file=f Input file
A simple input file name (file must exist)
* dir=d Input directory
A simple input directory name (directory must exist)
* out=>f Output file (file will be created)
An output filename
* outdir=>d Output directory
An output directory name (path will be created)
* append=>>f Output file append
An output filename (an existing file will be appended; otherwise file will be created)
* array=<f* All other args are input files
Any other command line arguments will be pushced on to this array.
The following command line arguments:
( run in 0.496 second using v1.01-cache-2.11-cpan-e1769b4cff6 )