# Generated by Pod::WikiDoc version 0.17

=pod

=head1 NAME

Getopt::LL - Flexible argument processing.

=head1 VERSION

This document describes B<Getopt::LL> version 0.0.6

=head1 SYNOPSIS

     use Getopt:LL qw(getoptions);
 
     my $use_foo = 0;
 
     my $options = getoptions({
         '-t'            => 'string',
         '--verbose|-v'  => 'flag',
         '--debug|-d'    => 'digit',
         '--use-foo'     => sub {
             $use_foo = 1;
         },
         '-output|-o'    => sub {
             my ($getopt, $node) = @_;
             my $next_arg = $getopt->get_next_arg($node);
 
             if ($next_arg eq '-') {
                 $out_to_stdout = 1;
             }
 
             return $next_arg;
         };
     });

=head1 DESCRIPTION

Getopt::LL provides several ways for defining the arguments you want.
There is L<Getopt::LL::Simple> for defining arguments on the -use-line-,
L<Getopt::LL::Short> for abbreviated rules (that looks like L<Getopt::Long>).

=head2 RULES

-Rules- is the guidelines Getopt::LL follows when it meets new options.
The rules defines what options we want, which options are required,
and what to do with an option.

A simple rule-set could be written like this:

     my $rules = {
         '-foo'      => 'string',
         '-bar'      => 'string',
         '--verbose' => 'digit',
         '--debug'   => 'flag',
     };

=head3 Rule typesE<sol>actions.

The argument to an rule is what we call a rule type or rule action.
It can be one of the following:

=over

=item *

C<<< 'flag' >>>

=back

The option is a flag. The value of the option will be boolean true.

=over

=item *

C<<< 'string' >>>

=back

The option is a string. The value of the option will be the next argument
in the argument list.

=over

=item *

C<<< 'digit' >>>

=back

The option is a number. The value of the option will be the next argument
in the argument list. The value will be sent to C<<< is_digit($value) >>> to check
that it's really a number. If it's not a number and the C<<< die_on_type_mismatch >>>
option is set, the program will die with a type mismatch error message.

A digit can also be a hex value if it begins with -0x-, any hex value
will be converted to a decimal value.

=over

=item *

A regular expression: C<<< qr/ / >>>

=back

The next argument will be matched against the regular expression.
If it doesn't match the program will die with the message

     Argument [--arg] doesn't match [regular-expression].

=over

=item *

An anonymous subroutine. C<<< sub { } >>>

=back

The sub-routine will be called with the following arguments

=over

=item 1.

C<<< $_[0] >>> - The Getopt::LL object.

=item 2.

C<<< $_[1] >>> - The current argument node (A Getopt::LL::DLList::Node] object).

=item 3.

C<<< $_[2] >>> - The argument name.

=item 4.

C<<< $_[3] >>> - If an argument value was set by the user with C<<< --arg=value >>>, the value is in this variable.

=back

The return value of the anonymous subroutine will be the value of the option.

Here is an example of a rule sub that simply assigns the value of the next
argument to the option value:

     my $rules = {
 
         '-foo'  => sub {
             my ($getopt, $node, $arg_name, $arg_value) = @_;
                 return $arg_value if $arg_value;
 
                 my $next_arg = $getopt->get_next_arg($node);
 
                 return $next_arg;
         },
     };
 
     my $result = getoptions($rules);
 
     print 'FOO IS: [', $result->{'-foo'}, "]\n";

if this program is called with the arguments: C<<< -foo bar >>> or C<<< -foo=bar >>> it will
print out this message:

     FOO IS [bar]

=head3 Specifying required arguments.

There are two ways of specifying required arguments.

=over

=item *

Embedded in the rule name, by an exclamation point B<!>.

=back

     my $rules = {
         '-foo!' => 'string',
     };

=over

=item *

Or by adding the C<<< required >>> flag.

=back

     my $rules = {
         '-foo'  => {
             type        => 'string',
             required    => 1,
         },
     }

=head3 Adding default values to non-required arguments.

There are two ways of specifying default values.

=over

=item *

Embedded in the rule name, inside parens B<( .. )>

=back

     my $rules = {
         '-bar(defaultValue)' => 'string',
     };

=over

=item *

Or by adding a C<<< default >>> key to the spec.

=back

     my $rules = {
         '-bar'  => {
             type    => 'string',
             default => 'defaultValue',
         },
     };

=head2 DID YOU SAY SIMPLE?

With L<Getopt::LL::Simple> you can define the arguments you want on the
-use-line-:

     #!/usr/bin/perl
     use strict;
     use warnings;
 
     # we have three arguments:
     #   -f!          (our filename, which is a s(tring) the ! means that it's
     #               a required argument.
     #   --verbose   (print extra information about what we're doing, is a flag).
     #   --debug     (the level of debugging information to print. is a
     #                d(igit).
     #
     use Getopt::LL::Simple qw(
         -f!=s
         --verbose
         --debug|d=d
     );
 
     if ($ARGV{'--verbose'}) {
         print "In verbose mode...\n";
     }
 
     if ($ARGV{'--debug'}) {
         print 'Debugging level is set to: ', $ARGV{'--debug'}, "\n";
     }
 
     print "The contents of @ARGV is:\n";
     prnit "\t", join{q{, }, @ARGV), "\n";

The options that was found is placed into C<<< %ARGV >>>, the arguments that is not
an option is placed into C<<< @ARGV >>>. So say we have run the program with the
following arguments:

     ./myprogram -f tmp.log --verbose --debug=3 foo bar

or

     ./myprogram -f tmp.log --verbose --debug 3 foo bar

it will give this output:

     In verbose mode...
     Debugging level is set to: 3
     The contents of @ARGV is:
         foo, bar 

=head1 SUBROUTINESE<sol>METHODS

=head2 CONSTRUCTOR

=head3 C<<< new(\%rules, \%options, \@opt_argv ) >>>

Uses C<<< @ARGV >>> if no C<<< \@opt_argv >>> is present.

=head2 ATTRIBUTES

=head3 C<<< rules >>>

=head3 C<<< set_rules >>>

The list of rules passed to C<<< new >>>.

=head3 C<<< options >>>

=head3 C<<< set_options >>>

The options passed  to C<<< new >>>.

=head3 C<<< dll >>>

=head3 C<<< set_dll >>>

Our arguments converted to a doubly linked list.
(is a L<Getopt::LL::DLList> object).

=head3 C<<< result >>>

=head3 C<<< set_result >>>

The final argument hash.

=head3 C<<< leftovers >>>

=head3 C<<< set_leftovers >>>

Array of items in the argument list that does not start with B<-> or B<-->.

=head2 INSTANCE METHODS

=head3 C<<< parseoption($argument, $node) >>>

This method is called for each argument to decide what to do with it.

=head3 C<<< find_arg_type($argument) >>>

Find out what kind of argument this is.

If the argument starts with B<-> (a single dash) it returns C<<< short >>>,
but if it starts with B<--> (two dashes) it returns C<<< long >>>.

=head3 C<<< is_string($value, $option_name) >>>

Check if value is a proper string.

=head3 C<<< is_digit($value, $option_name) >>>

Check if value is a digit. (C<<< 0-9+ >>>)
If value starts with -0x-, it is treated as a hex value.

=head3 C<<< type_mismatch_error($type, $message) >>>

Called whenever a type does not match it's requirements.

=head3 C<<< unknown_argument_error($argument) >>>

Called when a argument that has no rule is found.
(turn off by setting the C<<< allow_unspecified >>> option to a true value).

=head3 C<<< handle_rule($option_name, $rule, $node) >>>

Called when C<<< parseoption() >>> finds an argument that we have an existing rule for.
This function decides what to do with the argument based on it's C<<< RULE_ACTION >>>.

=head3 C<<< get_next_arg($node) >>>

Get and delete the next argument.
(Gets the next node in our doubly linked list and deletes the current node)

=head3 C<<< peek_next_arg($node) >>>

Look at the next argument, but don't delete it.

=head3 C<<< get_prev_arg($node) >>>

Get and delete the previous argument.

=head3 C<<< peek_prev_arg($node) >>>

Look at the previous argument, but don't delete it.

=head2 C<<< delete_arg($node) >>>

Deletes the argument.

=head2 C<<< rules_prepare(\%rules) >>>

Find and prepare aliases in the rule set.

=head2 C<<< rules_postactions( ) >>>

Things to do with rules after argument processing is done.
Like adding default values for arguments with default values defined and
checking for required arguments.

=head2 C<<< show_help( ) >>>

Print help for arguments to standard error.
This is experimental and the implementation is not exactly complete.

=head2 C<<< show_usage( ) >>>

Print usage to standard error.
This is experimental and the implementation is not exactly complete.

=head2 CLASS METHODS 

=head3 C<<< getoptions(\%rules, \%options, \@opt_argv) >>>

Parses and gets arguments based on the rules in C<<< \%rules >>>.
Uses C<<< @ARGV >>> if C<<< \@opt_arg >>> is not specified.

Returns hash with the arguments it found.
C<<< @ARGV >>> is replaced with the arguments that does not start with B<-> or B<-->.

=head3 C<<< opt_String($help_for_option) >>> 

Shortcut for writing:

     {
         type => 'string',
         help => $help_for_option,
     }

=head3 C<<< opt_Digit($help_for_option) >>> 

Shortcut for writing:

     {
         type => 'digit',
         help => $help_for_option,
     }

=head3 C<<< opt_Flag($help_for_option) >>> 

Shortcut for writing:

     {
         type => 'flag',
         help => $help_for_option,
     }

=head2 PRIVATE INSTANCE METHODS

=head3 C<<< _init() >>>

Called by new to traverse and parse the doubly linked list of arguments.

=head3 C<<< _warn(@messages) >>>

Print a warning on the screen, but only if C<<< $options->{silent} >>> is not set.

=head2 PRIVATE CLASS METHODS

=head3 C<<< _regex_as_text($regex) >>>

Quoted regexes are not very user-friendly to print directly, so this
function turns a quoted regex like:

     (?xmsi:hello)

into:

     /hello/xmsii

=head1 DIAGNOSTICS

=head1 CONFIGURATION AND ENVIRONMENT

This module requires no configuration file or environment variables.

=head1 DEPENDENCIES

=over

=item *

L<Class::Dot>

=back

=over

=item *

L<version>

=back

=head1 INCOMPATIBILITIES

None known.

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
L<bug-getopt-ll@rt.cpan.org|mailto:bug-getopt-ll@rt.cpan.org>, or through the web interface at
L<CPAN Bugtracker|http://rt.cpan.org>.

=head1 SEE ALSO

=over

=item *

L<Getopt::Long>

=back

=over

=item *

L<Getopt::Euclid>

=back

=over

=item *

L<Getopt::Declare>

=back

=over

=item *

L<Getopt::Attribute>

=back

=head1 TEST SUITE CODE COVERAGE

     ---------------------------- ------ ------ ------ ------ ------ ------ ------
     File                           stmt   bran   cond    sub    pod   time  total
     ---------------------------- ------ ------ ------ ------ ------ ------ ------
     lib/Getopt/LL.pm              100.0   98.6   94.4  100.0  100.0   38.2   99.2
     lib/Getopt/LL/DLList.pm       100.0  100.0  100.0  100.0  100.0   15.1  100.0
     lib/Getopt/LL/DLList/Node.pm  100.0  100.0    n/a  100.0  100.0    9.9  100.0
     lib/Getopt/LL/Short.pm        100.0  100.0  100.0  100.0  100.0    1.4  100.0
     lib/Getopt/LL/Simple.pm       100.0  100.0  100.0  100.0    n/a    0.6  100.0
     ...topt/LL/SimpleExporter.pm  100.0  100.0    n/a  100.0  100.0    4.6  100.0
     lib/Getopt/LL/properties.pm   100.0  100.0    n/a  100.0    n/a   30.2  100.0
     Total                         100.0   99.0   96.2  100.0  100.0  100.0   99.6
     ---------------------------- ------ ------ ------ ------ ------ ------ ------

The summary was generated by L<Devel::Cover>.

=head1 AUTHOR

Ask Solem, CE<lt>E<lt> ask@0x61736b.net E<gt>E<gt>.

=head1 LICENSE AND COPYRIGHT

Copyright (c), 2007 Ask Solem CE<lt>E<lt> ask@0x61736b.net E<gt>E<gt>.

All rights reserved.

This library is free software; you can redistribute it andE<sol>or modify
it under the same terms as Perl itself, either Perl version 5.8.6 or,
at your option, any later version of Perl 5 you may have available.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS ANDE<sol>OR OTHER PARTIES PROVIDE THE
SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE,
YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY ANDE<sol>OR REDISTRIBUTE THE
SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER
SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.


# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 78
# End:
# vim: expandtab tabstop=4 shiftwidth=4 shiftround

