Bootstrap FreeKB - Perl (Scripting) - Getting Started with command line options and flags
Perl (Scripting) - Getting Started with command line options and flags

Updated:   |  Perl (Scripting) articles

Let's say you want to be able to use options and flags with one of your Perl scripts. In this example, the --name option is followed by a value of Jeremy.

perl example.pl --name Jeremy

 

In this example, the --help flag is used.

perl example.pl --help

 


The Options module can be used for command line options and flags.

use Options;

 

Here is an example of how to create the options and flags that you want to be able to use on the command line. The following script has two options (name and occuption) and one flag (help).

my $options = new Options(
  params => [
    ["name",       "n", "", "Name"],
    ["occupation", "o", "", "Occupation"]
   ],
  flags =>  [
    ["help", "h", "Looks like you need some help"],
   ]
);

my %argv       = $options->get_options();
my $name       = $options->get_result('name');
my $occupation = $options->get_result('occupation');

 

You can now use if statements to do something when an option or flag is used.

if (not $options->get_result('name')) {
  print "The -n or --name option was not used on the command line \n";
}

 

Likewise, you can check to see if an option or flag is defined.

if ($help) { 
  print "the help flag is defined\n"; 
}

 

This is a common if statement for the --help flag.

if ($options->get_result('help')){
  $options->print_usage();
  exit 0;
}

 

Here is how I set command line options as required and exit if the option does not contain a valid value.

my @validEnvironments = ("development", "staging", "production");

my $options = new Options(
  params => [
    ["environment", "e", "", "@validEnvironments"]
   ],
  flags =>  [
    ["help", "h", "help menu"]
   ]
);

my %argv = $options->get_options();

if ($options->get_result('help')){
  $options->print_usage();
  exit;
}

my $environment = $options->get_result('environment');

if (($environment eq "") or (not grep(/$environment/, @validEnvironments))) {
  print("The -e or --environment option must contain one of these values: @validEnvironments\n");
  exit;
}

 


Fancy / curley / smart double quotes

Be aware that when curly / fancy / smart double quotes are used on the command line, the curly double quotes will be included in the value.

perl example.pl --name “Jeremy“

 

When straight double quotes are used on the command line, the double quotes will not be included in the value.

perl example.pl --name "Jeremy"

 

One option is to remove curly double quotes.

$options->get_result('name') =~ s/“|//g;

 


ARGV

Another option is to use the @ARGV array. In this example, the @ARGV array contains two variables, $first and $second.

my ($first, $second) = @ARGV;

 

When running the Perl script, the first string following the command will be loaded into the $first variable, nd the second string into the $second variable.

perl example.pl Hello World

 

Both variables can be printed using the @ARGV array. In this example, the @ARGV array will contain "Hello World".

print "@ARGV\n";

 

Or, each variable can be printed using $first and $second. In this example, the $first variable contains "Hello" and the $second variable contains "World".

print "$first\n";
print "$second\n";

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 197599 in the box below so that we can be sure you are a human.