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. "i|int=i",
  15. "f|float=f",
  16. "o|octal=o",
  17. )) || die "ERROR: Illegal arguments or parameters: @ARGV\n" unless ($#ARGV < 0);
  18.  
  19.  
  20. ## Or as OO
  21. #
  22. #my $parser = new Getopt::Long::Parser;
  23. #$parser->configure("no_ignore_case");
  24. #if ($parser->getoptions(\%Opt,
  25. # "h|help",
  26. # "s|string=s",
  27. # "i|int=i",
  28. # "f|float=f",
  29. # "o|octal=o",
  30. #)) {};
  31. #
  32.  
  33. ## Just print out the options for each collected to see what there is:
  34. foreach my $key (keys %Opt) {
  35. print "$key is $Opt{$key}\n";
  36. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.