-2

(Please before flagging the question, read it first! I know that similar questions have been asked, yet in different and incomplete forms).

I'm trying to find what the natural and normal way is to handle command line arguments or script options. Assume that script may take any possible type of arguments or options. For example:

./myscript.pl runabnormal --help /sbin/proc -f filename CONFIG=set.config LOG=me.log

The best way that I'm aware of so far is by using Dumper as follows:

use strict;
use warnings;
use Data::Dumper qw(Dumper);

print Dumper \@ARGV;

output:

$VAR1 = [
      'runabnormal',
      '--help',
      '/sbin/proc',
      '-f',
      'filename',
      'CONFIG=set.config',
      'LOG=me.log'
    ];

Any general advice on this? How do perl programmers usually handle the complicated arguments input to their scripts?

Ali Abbasinasab
  • 382
  • 3
  • 13
  • 3
    You say that your question is special and different from other questions, but it seems to me that all you are asking is how to use `@ARGV`? Using `Data::Dumper` is hardly a "best" way, as it only prints the values of your arguments. – TLP Aug 28 '14 at 18:11

1 Answers1

2

Use Getopt::Long:

use strict;
use warnings;

use Getopt::Long;

GetOptions(
    "log=s"    => \( my $log ),                  # string
    "file=s"   => \( my $file = "file.dat" ),    # string with default
    "help"     => \( my $help ),                 # boolean
    "config=s" => \( my $config ),               # string
) or die "Error in command line arguments";

# Pulls in: ("runabnormal", "/sbin/proc")
my ($state, $proc) = @ARGV;

Two notes about command line args common practice:

  1. Options are typically lowercase. So change CONFIG=set.config to config=set.config
  2. Options with parameters are prefixed with two dashes. --log=me.log

Therefore the following would work:

./myscript.pl runabnormal --help /sbin/proc -f filename --config=set.config --log=me.log
Miller
  • 34,962
  • 4
  • 39
  • 60
  • That actually requires `--config=set.config --log=me.log`, but that's that more conventional anyway, and surely better unless those `X=Y` arguments are arbitrary. // What's `my ($state, $proc) = @ARGV;`? – ikegami Aug 28 '14 at 18:16
  • Good eye. missed that. Updated in answer. – Miller Aug 28 '14 at 18:21
  • @ARGV was for the two non options included in his example. Decided to not put any more thought into deciphering his intent and just slurp them. – Miller Aug 28 '14 at 18:25
  • I'm not getting how `-f filename` is handled. Is `file=s` handling `-f`? What if I have `-f1 file1 -f2 file2`? – Ali Abbasinasab Aug 28 '14 at 18:50
  • `-f1` and `-f2` would be separate options in that case and need code to define them. However, it's possible to pass multiple values to the same option: [Getopt::Long - Options with multiple values](http://perldoc.perl.org/Getopt/Long.html#Options-with-multiple-values) – Miller Aug 28 '14 at 18:56
  • Thanks but as I asked before, I'm not getting how `-f filename` is handled. Is `file=s` handling `-f filename` or something else? Isn't `file=s` supposed to handle only `--file=filename`? :confused – Ali Abbasinasab Aug 28 '14 at 19:06