如何在 Perl 中使用 Getopt::Long 验证读取了哪些标志?
myscript.pl
my $R;
my $f1 = "f1.log";
my $f2 = "f2.log";
my $f3 = "f3.log";
sub checkflags {
GetOptions('a=s' => \$f1,
'b=s' => \$f2,
'c=s' => \$f3,
);
open $R, '>', $f1 or die "Cannot open file\n"; # Line a
}
所有标志都是可选的。
如果我将脚本称为
perl myscript.pl -a=文件名
我需要在文件名中附加一个
.log
,然后才能在Line a
处打开它。为此,我需要知道
GetOptions
是否将某些内容读入$f1
中。
这怎么能做到呢?
myscript.pl
my $R;
my $f1 = "f1.log";
my $f2 = "f2.log";
my $f3 = "f3.log";
sub checkflags {
GetOptions('a=s' => \$f1,
'b=s' => \$f2,
'c=s' => \$f3,
);
open $R, '>', $f1 or die "Cannot open file\n"; # Line a
}
All the flags are optional.
If I call the script as
perl myscript.pl -a=filename
I need to append a
.log
to the filename before opening it atLine a
.For that I need to know whether
GetOptions
read something into$f1
or not.
How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的解决方案是在
$f1
中查找/[.]log$/
,如果不存在则添加它。不幸的是,这意味着当用户传入"foo.log"
并希望它成为"foo.log.log"
时,它不会,但我认为我们可以同意用户是个混蛋。一个更好的选择是:
如果你想在顶部定义所有默认名称,请使用不同的变量来做到这一点(无论如何,阅读代码可能会更好):
The simplest solution is to look for
/[.]log$/
in$f1
and add it if it isn't present. Unfortunately that means that when the user passes in"foo.log"
and wanted it to become"foo.log.log"
it won't, but I think we can agree that user is a jerk.A better option, that will make the jerk happy, is:
If you want to define all of default names at the top, use a different variable to do that (it is probably better reading code anyway):
但我个人更喜欢 将选项读入哈希,然后使用exists()。
But personally I'd prefer to read the options into a hash and then use exists().
如果文件名尚无扩展名,则附加 log 扩展名。由于默认值以 log 结尾,因此不会发生任何情况。如果用户在命令行上输入log,它就会起作用。
Appends the log extension if the file name does not already have one. Since the default value ends in log, nothing happens. And it works if the user types the log on the command line.
实现此目的的一种方法是使用 Moose 和 MooseX::Getopt:
One way to achieve this is to use Moose and MooseX::Getopt: