The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Filter::Arguments - Configure and read your command line arguments from @ARGV.

SYNOPSIS

 use Filter::Arguments;

 my $solo                : Argument(bool) = 1;
 my $bool_default        : Argument;
 my ($a,$b,$c)           : Arguments(bool);
 my ($d,$e,$f)           : Arguments(value);
 my ($x,$y,$z)           : Arguments(xbool);
 my ($three,$four,$five) : Arguments(value) = (3,4,5);
 my ($six,$seven,$eight) : Arguments(bool)  = ('SIX','SEVEN','EIGHT');

 my $multiline : Argument(value) = <<END_ML;
    my multi-line
    initial value
 END_ML

 my @result = (
     $solo,
     $bool_default,
     $a, $b, $c,
     $d, $e, $f,
     $x, $y, $z,
     $three, $four, $five,
     $six, $seven, $eight,
     $multiline,
 );

 print join ',', @result;

if invoked as: $ script.pl --solo --a --b --c --d A --e B --f C --x --y --z --six

will print:

 0,,1,1,1,A,B,C,0,0,1,3,4,5,0,SEVEN,EIGHT,my multi-line
 initial value

DESCRIPTION

Here is a simple way to configure and parse your command line arguments from @ARGV. If an unrecognized argument is given then a basic usage statement is printed and your program dies.

ARG TYPES

bool (default)

This type of argument is either 1 or 0. If it is initialized to 1, then it will flip-flop to 0 if the arg is given.

xbool

The 'x' as in XOR boolean. Only one of these booleans can be true. The flip-flop behavior also applies to these also. So you may initialize them however, but if one is set then the others are set to the opposite value of the one that is set.

value

This type takes on the value of the next argument presented.

Example:

 my $noodle : Argument(value) = 'egg';

The variable $noodle will be 'egg', unless the argument sequence:

 --noodle instant

Where $noodle will then be 'instant'.

TODO

regex type argument

This would allow arguments matching a pattern such as:

 my @words   : Argument(regex) = qr{\A \w+ \z}xms;
 my @numbers : Argument(regex) = qr{\A \d+ \z}xms;

The permitted argument sequence:

 program.pl 12345 4321 horse
multivalue arguments

This would allow:

 my @words : Argument(value);

Where the permitted argument sequence would be:

 program.pl --words horse cow pig
required argument
 my $required ! Argument(value);

Where the program will die with usage statement if this option is not provided.

DEPENDENCIES

Template
Filter::Simple

BUGS

Don't put comments at the end of an Argument line.

Example:

 my $a :Argument = 1; # comment

This will result in an 'Invalid SCALAR attribute' compile time error.

Version 0.06 intends to resolve the line numbering bug.

AUTHOR

Dylan Doxey <dylan.doxey@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2009 by Dylan Doxey

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