问题
I need some help using perls Getopt::Lazy module. I tried the example from the cpan page:
#!/usr/bin/perl
#
#use warnings;
#use strict;
use Getopt::Lazy
'help|h' => 'Show this help screen',
'verbose|v' => 'Show verbose output',
'output|o=s' => ["[FILE] Send the output to FILE", 'getopt.out'],
'output-encoding=s' => ['[ENCODING] Specify the output encoding', 'utf8'],
-summary => 'a simple example usage of Getopt::Lazy',
-usage => '%c %o file1 [file2 ..]',
;
getopt;
print usage and exit 1 unless @ARGV;
When I put it in a file and execute it like ./mygetopt.pl -h
, I would expect the help message to be printed, but nothing happens. When I call it without the -h
argument, I would expect it to print the usage message. Nothing like this happens. Also, when I use strict and warnings, I get messages like
Unquoted string "usage" may clash with future reserved word at ./mygetopt.pl line 14.
Bareword "getopt" not allowed while "strict subs" in use at ./mygetopt.pl line 13.
Execution of ./mygetopt.pl aborted due to compilation errors.
What am I doing wrong?
回答1:
The SYNOPSIS for that module is completely out of step with the actual code. There are no functions called getopt
or usage
in the code. They are actually called GetOptions
and Getopt::Lazy::show_help
(yes, one is exported, the other is not - who knows why). Rewriting the example like this will work:
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Lazy
'help|h' => 'Show this help screen',
'verbose|v' => 'Show verbose output',
'output|o=s' => ["[FILE] Send the output to FILE", 'getopt.out'],
'output-encoding=s' => ['[ENCODING] Specify the output encoding', 'utf8'],
-summary => 'a simple example usage of Getopt::Lazy',
-usage => '%c %o file1 [file2 ..]',
;
GetOptions;
Getopt::Lazy::show_help and exit 1 unless @ARGV;
But given the quality of the documentation and given that the module was apparently abandoned by its author seven and a half years ago, I wouldn't let it anywhere near any of my projects. You would be far better advised to use one of the solutions suggested by other people in the comments above.
来源:https://stackoverflow.com/questions/33038501/getoptlazy-does-not-print-usage-message-or-anything-else