Getopt::Simple(3pm)
β¨ Getopt::Simple π β the Getopt::Simple manpage, bestie edition
okay so you found Getopt::Simple(3pm), and yes it's part of the User Contributed Perl Documentation, and yes it's iconic. Sit down, grab a strawberry π, and let's get into it.
πΈ NAME πΈ
"Getopt::Simple" β it's literally a simple wrapper around Getopt::Long. that's the whole vibe. no cap.
π SYNOPSIS π
here's the moment you've been waiting for, bestie. watch closely:
use Getopt::Simple;
# Or ...
# use Getopt::Simple qw($switch);
my($options) =
{
help =>
{
type => '',
env => '-',
default => '',
# verbose => '', # Not needed on every key.
order => 1,
},
username =>
{
type => '=s', # As per Getopt::Long.
env => '$USER', # Help text.
default => $ENV{'USER'} || 'RonSavage', # In case $USER is undef.
verbose => 'Specify the username on the remote machine',
order => 3, # Help text sort order.
},
password =>
{
type => '=s',
env => '-',
default => 'password',
verbose => 'Specify the password on the remote machine',
order => 4,
},
};
my($option) = Getopt::Simple -> new();
if (! $option -> getOptions($options, "Usage: testSimple.pl [options]") )
{
exit(-1); # Failure.
}
print "username: $$option{'switch'}{'username'}. \n";
print "password: $$option{'switch'}{'password'}. \n";
# Or, after 'use Getopt::Simple qw($switch);' ...
# print "username: $$switch{'username'}. \n";
# print "password: $$switch{'password'}. \n";
see how pretty that is? slay. π
π¦ DESCRIPTION π¦
"Getopt::Simple" is a pure Perl module. no XS nonsense, no compiling, just vibes.
so what does "Getopt::Simple" actually give you? it provides a simple way of specifying:
- Command line switches
- Type information for switch values
- Default values for the switches
- Help text per switch
basically it's your little bestie that handles the boring argument-parsing so you can be the main character. β¨
π¦ Distributions π¦
this module is available both as a Unix-style distro (*.tgz) and an ActiveState-style distro (*.ppd). the latter is shipped in a *.zip file. two flavors, pick your fighter. π
- See
http://savage.net.au/Perl-modules.htmlfor details. - See
http://savage.net.au/Perl-modules/html/installing-a-module.htmlfor help on unpacking and installing each type of distro.
π§ Constructor and initialization π§
new(...) returns a "Getopt::Simple" object.
this is the class's contructor. (yes that's spelled exactly how the original spells it, and honestly? we respect it.)
Usage: Getopt::Simple -> new().
this method does not take any parameters. not a single one. it's low maintenance like that. π
π The dumpOptions() function π
dumpOptions() prints all your option's keys and their current values.
dumpOptions() does not return anything. it just stands there, looking pretty, spilling your options to the screen. π
π₯ The getOptions() function π₯
the getOptions() function takes 4 parameters:
- A hash ref defining the command line switches
the structure of this hash ref is defined in the next section.
this parameter is mandatory.
- A string to display as a help text heading
this parameter is mandatory.
- A Boolean.
0= (Default) Use case-sensitive switch names.1= Ignore case
this parameter is optional.
- A Boolean.
0= Return after displaying help.1= (Default) Terminate withexit(0)after displaying help
this parameter is optional.
getOptions() returns 0 for failure and 1 for success.
failure =0, success =1. write it on your hand, bestie. π«Ά
π The hash ref of command line switches π
okay this is the big one. the structure. the anatomy. the blueprint. β¨
- Each key in the hash ref is the name of a command line switch
- Each key points to a hash ref which defines the nature of that command line switch
the keys and values of this nested hash ref are as follows.
default => 'Some value'
this key, value pair is mandatory.
this is the default value for this switch.
Examples:
default => '/users/home/dir'
default => $ENV{'REMOTEHOST'} || '127.0.0.1'
env => '-' || 'Some short help text'
this key, value pair is mandatory.
this is help test, to indicate that the calling program can use an environment variable to set the default value of this switch.
use '-' to indicate that no environment variable is used.
Examples:
env => '-'
env => '$REMOTEHOST'
note the use of ' to indicate we want the $ to appear in the output. that single quote is doing character work, honestly.
type => 'Types as per Getopt::Long'
this key, value pair is mandatory.
this is the type of the command line switch, as defined by Getopt::Long.
Examples:
type => '=s'
type => '=s@',
verbose => 'Some long help text'
this key, value pair is optional.
this is long, explanatory help text which is displayed below the help containing the three columns of text: switch name, env value, default value.
Examples:
verbose => 'Specify the username on the remote machine',
verbose => 'Specify the home directory on the remote machine'
order => \d+
this key, value pair is mandatory.
this is the sort order used to force the help text to display the switches in a specific order down the page.
Examples:
order => 1
order => 9
π
The helpOptions() function π
helpOptions() prints nicely formatted help text.
helpOptions() does not return anything. just pure, formatted, aesthetic output. β¨
π The $$classRef{'switch'} hash reference π
command line option values are accessed in your code by dereferencing the hash reference $$classRef{'switch'}. two examples are given above, under synopsis.
alternately, you can use the hash reference $switch. see below. π
π The $switch hash reference π
command line option values are accessed in your code by dereferencing the hash reference $switch. two examples are given above, under synopsis.
alternately, you can use the hash reference $$classRef{'switch'}. see above. (yes, it's the same situation, just mirrored. we love a symmetrical moment. π¦)
π WARNING re Perl bug π
not me having to tell you this, but here we are. as always, be aware that these 2 lines mean the same thing, sometimes:
$self -> {'thing'}$self->{'thing'}
the problem is the spaces around the ->. inside double quotes, "...", the first space stops the dereference taking place. outside double quotes the scanner correctly associates the $self token with the {'thing'} token.
I regard this as a bug.
bestie. watch your whitespace. it WILL humble you. π
π«Ά AUTHOR π«Ά
"Getopt::Simple" was written by Ron Savage <ron@savage.net.au> in 1997. a true icon, honestly. π
πΈ LICENCE πΈ
Australian copyright (c) 1997-2002 Ron Savage.
All Programs of mine are 'OSI Certified Open Source Software';
you can redistribute them and/or modify them under the terms of
The Artistic License, a copy of which is available at:
http://www.opensource.org/licenses/index.html
and that's the whole page, bestie! π this one's from perl v5.40.1, dated 2025-06-06, and it's the Getopt::Simple(3pm) page β go forth and parse your arguments, you gorgeous main character. periodt. πβ¨
π¦ where this page comes from
- ubuntu resolute: libgetopt-simple-perl 1.52-8 Β· Artistic-1.0-Perl
- ubuntu stonking: libgetopt-simple-perl 1.52-8 Β· Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms