NAME
Getopt::Type::Tiny - Clean Getopt::Long wrapper with Type::Tiny support
VERSION
version 0.02
SYNOPSIS
use Getopt::Type::Tiny qw(get_opts Str Int);
my %opts = get_opts(
foo => { isa => Str },
bar => { isa => Int, default => 42 },
'verbose|v', # defaults to Bool
);
# %opts now contains the parsed options:
# (
# foo => 'value of foo',
# bar => 42,
# verbose => 1,
# )
DESCRIPTION
This module is a thin wrapper around Getopt::Long that adds support for Type::Tiny type constraints. It is intended to be a clean and simple way to parse command line options with type constraints.
FUNCTIONS
get_opts
my %opts = get_opts(
foo => { isa => Str },
bar => { isa => Int, default => 42 },
'verbose|v', # defaults to Bool
);
Parses the command line options and returns a hash of the parsed options. The arguments to get_opts are a list of option specifications.
OPTION SPECIFICATIONS
Option specifications are passed to get_opts as a list of key/value pairs. If no option spec is passed, the option is assumed to be a boolean option:
my %options = get_opts( qw/
dryrun
enabled
verbose
/ );
- isa
-
The
isakey specifies the type constraint for the option. This can be any Types::Standard, Types::Common::Numeric, or Types::Common::String type. If more complex types are needed, you can create your own type constraints with Type::Tiny. - default
-
The
defaultkey specifies the default value for the option. If the option is not present on the command line, this value will be used. - rename
-
The
renamekey specifies a new name for the option. The value of the option will be stored in the hash under this new name. - required
-
The
requiredkey specifies that the option is required. If the option is not present on the command line, an error will be thrown.
HELP OPTIONS
By default, get_opts will add a --help|? and man options that use Pod::Usage to display the usage message and exit. The --help and -? options will display a brief usage message and exit. The --man option will display the full documentation and exit.
MULTI-VALUE OPTIONS
Multi-value options (arrays and hashes) are automatically detected from ArrayRef and HashRef types:
my %opts = get_opts(
servers => { isa => ArrayRef[Str] }, # --servers=a --servers=b
config => { isa => HashRef[Int] }, # --config=k1=1 --config=k2=2
);
Supported Types
The following type mappings are supported:
ArrayRef[Str] => =s@ ArrayRef[Int] => =i@ ArrayRef[Num] => =f@
HashRef[Str] => =s% HashRef[Int] => =i% HashRef[Num] => =f%
Subtypes are also supported (e.g., ArrayRef[PositiveInt] maps to =i@). ArrayRef[Any] and HashRef[Any] map to string modifiers.
Auto-Defaults
ArrayRef types default to [] and HashRef types default to {} when no default is specified. You can override this with an explicit default or use required => 1.
Explicit Syntax
You can still use explicit Getopt::Long syntax if needed:
'servers=s@' => { isa => ArrayRef[Str] }
If the explicit syntax differs from what would be inferred, a warning is issued. Use nowarn => 1 to suppress it:
'servers=s@' => { isa => ArrayRef[Int], nowarn => 1 }
AUTHOR
Curtis "Ovid" Poe <curtis.poe@gmail.com>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2025 by Curtis "Ovid" Poe.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)