我是Perl的新手.我正在尝试理解该Getopt::Long模块,我在网上阅读了材料以及书籍.因此,这个问题对我来说没有什么是明确的.
我将以下列格式调用命令行
script -m file
Run Code Online (Sandbox Code Playgroud)
要么
script -v hostname -m file
Run Code Online (Sandbox Code Playgroud)
我怎么能用Getopt::Std或完成这个Getopt::Long?
你可以使用其中之一.
但是,我建议你使用Getopt::Long和Pod::Usage
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Pod::Usage qw(pod2usage);
GetOptions(
'm=s' => \my $filename,
'v=s' => \my $hostname,
'help|?' => \my $help,
'man' => \my $man,
) or pod2usage(-verbose => 0);
pod2usage(-verbose => 1) if $help;
pod2usage(-verbose => 2) if $man;
# Fake Parameter validation
pod2usage("$0: No filename specified.\n") unless $filename;
print "Hostname = '$hostname'\n";
print "Filename = '$filename'\n";
1;
__END__
=head1 NAME
yourscript.pl - does something
=head1 SYNOPSIS
./yourscript.pl
=head1 OPTIONS
=over 4
=item --m
Filename, for some reason.
=item --v
Hostname, maybe make this -h so it's not confused with version flags?
=item --help
Print this summary.
=item --man
Print the complete manpage
=back
=head1 DESCRIPTION
This does stuff.... and other stuff
And this is all there is to say about it
=head1 AUTHOR
Written by You
Run Code Online (Sandbox Code Playgroud)
我会建议反对你的国旗选择.将文件名作为松散参数传递给脚本而不是需要标志通常就足够了.然后@ARGV在调用GetOptions之后将其拉出来.至于-v主机名,通常用于检查脚本的版本.相反,我会使用--hostname或-h.