gpt4 book ai didi

perl - 如何使用 Getopt::Std 设置默认值?

转载 作者:行者123 更新时间:2023-12-04 02:36:30 25 4
gpt4 key购买 nike

我正在尝试使用 Getopt::Std 从命令行收集值在我的 Perl 脚本中。

use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
getopts('i:o:p:');
my $inputfile = our $opt_i;
my $outputfile = our $opt_o;
my $parameter_value = our $opt_p;

这里前两个变量 ($inputfile,$outputfile) 是必需的,但最后一个变量 ($parameter_value) 是可选的,可以忽略。

我试图在 -p 时默认为最后一个变量($parameter_value)设置一些值在命令行中忽略标志。

我尝试使用这个:
my $parameter_value = our $opt_p || "20";

当在命令行中忽略 -p 标志时,它会传递正确的值。但问题是当我从命令行提供一些值时(例如 -p 58),相同的值 20 被传递给程序,而不是我从命令行传递的 58。

你能帮我指出我在这里犯的错误吗?

谢谢你。

最佳答案

#/usr/bin/perl

use strict;
use warnings;

use Getopt::Std;

getopts('i:o:p:');
our($opt_i, $opt_o, $opt_p);

my $inputfile = $opt_i;
my $outputfile = $opt_o;
my $parameter_value = $opt_p || "20";

print "$_\n" for $inputfile, $outputfile, $parameter_value;

C:\Temp> ks -iinput -ooutput -p55
输入
输出
55

C:\Temp> ks -iinput -ooutput
输入
输出
20

关于perl - 如何使用 Getopt::Std 设置默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1609060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com