Getopt::Pad

Declarative command line parsing for Perl. You describe every option and positional argument once, with its type, default, allowed values and help text. GetOptions parses the command line, checks every value, and returns an object with one method per option and argument (--log-level debug becomes $opt->logLevel).

Features

Synopsis

use v5.26;
use Getopt::Pad;

my $opt = GetOptions(
    description => 'Copy a directory to a backup location.',
    options     => {
        'target|t' => {
            type     => 'dir',
            required => 1,
            help     => 'Directory the backup is written to',
        },
        'keep' => {
            type    => 'int',
            default => 7,
            min     => 1,
            help    => 'Number of backups to keep',
        },
        'exclude|x' => {
            type     => 'string',
            multiple => 1,
            help     => 'Pattern of files to skip; repeat for more patterns',
        },
        'compress' => {
            type    => 'bool',
            default => 1,
            help    => 'Compress the backup; --no-compress turns it off',
        },
        'verbose|v' => {
            type => 'counter',
            help => 'Print more details; repeat for even more (-vv)',
        },
    },
    args => [
        { short => 'source', type => 'dir', required => 1, help => 'Directory to back up' },
    ],
);

# backup -t /mnt/backup -x '*.tmp' -x '*.log' -vv --no-compress photos
say $opt->source;                    # photos
say $opt->keep;                      # 7 (the default)
say join ', ', $opt->exclude->@*;    # *.tmp, *.log
say $opt->compress ? 'yes' : 'no';   # no
say $opt->verbose;                   # 2

Called with --help, this program prints:

# backup [options] source
# Copy a directory to a backup location.

## Arguments
   <source>                    [REQ] Directory to back up [Path]

## Completion
   --create-completions <>     Print a completion script for this shell to
                               STDOUT and exit
                                   Valid   = [ bash, zsh ]

## Options
   --[no-]compress             Compress the backup; --no-compress turns it
                               off
                                   Default = 1
   --exclude <>                Pattern of files to skip; repeat for more
                               patterns
   --keep <>                   Number of backups to keep
                                   Default = 7
   --target <>                 [REQ] Directory the backup is written to
                               [Path]
   --verbose                   Print more details; repeat for even more
                               (-vv)

Documentation

Once installed, read the documentation with perldoc:

The same documentation is on MetaCPAN.

Examples

Runnable examples live in examples/. Each one prints the values of its result object, so you can try different command lines; its header comment lists command lines to try:

Installation

From CPAN:

cpanm Getopt::Pad

From a checkout:

perl Makefile.PL
make
make test
make install

Requires Perl 5.26 or later, Object::Pad 0.818 or later, Getopt::Long 2.50 or later, Feature::Compat::Try and JSON::PP.

Optional: YAML::XS for YAML config files, and Term::ReadKey for wrapping the help output to the width of the terminal. The help is wrapped to the width in COLUMNS if it is set, else to the terminal width (with Term::ReadKey), else to 100 columns.

License

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Copyright 2026 davenonymous