NAME

Crane::Options - Command line options and arguments parser

SYNOPSIS

  use Crane::Options;
  
  my $option = options->{'version'};
  my $arg2   = args->[1];

DESCRIPTION

Parses command line options and arguments. Options are available as hash reference returned by options function and arguments are available as array reference returned by args function.

You can configure options by passing list of array references when first call options function (see description below).

By default two options are available: version and help.

EXPORTED CONSTANTS

Tag :opts - predefined options

$OPT_SEPARATOR

Not an option exaclty, just a separator in help output.

Equals to:

  []
$OPT_VERSION

Version information output.

Equals to:

  [ 'version!', 'Shows version information and exits.' ]
$OPT_HELP

Help output.

Equals to:

  [ 'help!', Shows this help and exits.' ]

EXPORTED FUNCTIONS

options (@options)

Returns hash reference to the command line options.

Can be configured when first called with the list of @options. To create an option you should pass a list of array references with one required and two mandatory items:

Specification

Scalar, required. Specification from Getopt::Long module.

Description

Scalar. Text description (what is this option does?).

Parameters

Hash reference. Additional parameters:

default

Default value for option if option does not exist.

required

Flag that option should be exists.

Separator is empty array reference.

args ()

Returns array reference to command line arguments.

FUNCTIONS

load_options (@options)

Parses command line arguments list @ARGV and return reference to hash.

See @options parameter description.

ERRORS

Invalid option specification: %s

Where %s is specification string.

Fires when required parameter of specification is not defined or incorrect.

DIAGNOSTICS

Option required: %s

Where %s is an option name.

Option does not exist but required.

EXAMPLES

Simple option in compare with defaults

Configuration:

  options(
      [ 'config|C=s', 'Path to configuration file.' ],
      $OPT_SEPARATOR,
      $OPT_VERSION,
      $OPT_HELP,
  );

Help output:

  example.pl <options> <args>
    -C --config             Path to configuration file.
  
       --version            Shows version information and exists.
    -? --help               Shows this help and exits.

Two required arguments, one with default value and default options

Configuration:

  options(
      [ 'daemon|M!', 'Run as daemon.',         { 'default'  => 1 } ],
      $OPT_SEPARATOR,
      [ 'from=s',    'Start of the interval.', { 'required' => 1 } ],
      [ 'to=s',      'End of the interval.',   { 'required' => 1 } ],
      $OPT_SEPARATOR,
      $OPT_VERSION,
      $OPT_HELP,
  );

Help output:

  example.pl <options> <args>
    -M --daemon             Run as daemon.
  
       --from               Start of the interval.
       --to                 End of the interval.
  
       --version            Shows version information and exists.
    -? --help               Shows this help and exits.

BUGS

Please report any bugs or feature requests to https://rt.cpan.org/Public/Bug/Report.html?Queue=Crane or to https://github.com/temoon/crane/issues.

AUTHOR

Tema Novikov, <novikov.tema@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2013-2014 Tema Novikov.

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0. For details, see the full text of the license in the file LICENSE.

SEE ALSO