在Perl中使用Getopt::Long进行命令行解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Perl中使用Getopt::Long进行命令行解析相关的知识,希望对你有一定的参考价值。

This is a simplified example for cut and paste use for command line parsing arguments with the use of an options hash
  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 " 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} ";
  36. }

以上是关于在Perl中使用Getopt::Long进行命令行解析的主要内容,如果未能解决你的问题,请参考以下文章