Using Getopt::Long for Command Line Parsing in Perl


/ Published in: Perl
Save to your folder(s)

This is a simplified example for cut and paste use for command line parsing arguments with the use of an options hash


Copy this code and paste it in your HTML
  1. #!/usr/bin/perl -w
  2.  
  3. ##Simplified example of using Getopt:Long moudule with an Options hash
  4.  
  5. use Getopt::Long;
  6. use strict;
  7.  
  8. my %Opt=();
  9.  
  10.  
  11. (GetOptions( \%Opt,
  12. "h|help",
  13. "s|string=s",
  14. "l|list=s@",
  15. "i|int=i",
  16. "f|float=f",
  17. "o|octal=o",
  18. )) || die "ERROR: Illegal arguments or parameters: @ARGV\n" unless ($#ARGV < 0);
  19.  
  20.  
  21. ## Or as OO
  22. #
  23. #my $parser = new Getopt::Long::Parser;
  24. #$parser->configure("no_ignore_case");
  25. #if ($parser->getoptions(\%Opt,
  26. # "h|help",
  27. # "s|string=s",
  28. # "l|list=s@",
  29. # "i|int=i",
  30. # "f|float=f",
  31. # "o|octal=o",
  32. #)) {};
  33. #
  34.  
  35. ## Just print out the options for each collected to see what there is:
  36. foreach my $key (keys %Opt) {
  37. print "$key is $Opt{$key}\n";
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.