Skip to content

Instantly share code, notes, and snippets.

@yunga
Forked from weibeld/getopt-std.pl
Created October 10, 2019 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunga/0b67c85f438e1c382235cc89815edf5b to your computer and use it in GitHub Desktop.
Save yunga/0b67c85f438e1c382235cc89815edf5b to your computer and use it in GitHub Desktop.
Reference example for using the Getopt::Std Perl module
use strict;
use warnings;
use feature qw(say);
use Getopt::Std;
# If set to true, exit script after processing --help or --version flags
$Getopt::Std::STANDARD_HELP_VERSION = 1;
our $VERSION = "0.1";
#------------------------------------------------------------------------------#
# Options
#------------------------------------------------------------------------------#
my %opts;
# -f requires an argument, -l does not take an argument
getopts('f:l', \%opts) or abort();
say "Supplied options:";
if (defined $opts{f}) {
say "-f $opts{f}";
}
if (defined $opts{l}) {
say "-l $opts{l}";
}
#------------------------------------------------------------------------------#
# Positional Arguments
#------------------------------------------------------------------------------#
# Note: positional arguments must be specified AFTER all options on the command
# line. There may be a double-dash (--) between options and arguments.
say "Trailing positional arguments (" . scalar(@ARGV) . "):";
for my $i (0 .. $#ARGV) {
say $ARGV[$i];
}
#------------------------------------------------------------------------------#
# Subroutines
#------------------------------------------------------------------------------#
# Called by Getopt::Std when supplying --help option
sub HELP_MESSAGE {
say get_help_message();
}
# Called by Getopt::Std when supplying --version or --help option
sub VERSION_MESSAGE {
say "Version $VERSION";
}
# Abort script due to error (e.g. invalid command line options)
sub abort {
say get_help_message();
exit 1;
}
# Central help message
sub get_help_message {
return "Hi, I'm the help message :)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment