I am trying to collect the values from command line using Getopt::Std in my Perl script.
我试图在我的Perl脚本中使用Getopt :: Std从命令行收集值。
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;
Here the first two variables ($inputfile,$outputfile) are mandatory but the last variable ($parameter_value) is optional and can be ignored.
这里前两个变量($ inputfile,$ outputfile)是必需的,但最后一个变量($ parameter_value)是可选的,可以忽略。
I am trying to set some value by default to the last variable ($parameter_value) when the -p
flag is ignored at the command line.
我试图在命令行忽略-p标志时默认将一些值设置为最后一个变量($ parameter_value)。
I tried using this:
我试过用这个:
my $parameter_value = our $opt_p || "20";
Here its passes the correct value when -p flag is ignored at command line. But the problem is when I am providing some value from the command line (for instance -p 58), the same value 20 is passed to the program instead of 58 which I passed from command line.
这里在命令行忽略-p标志时传递正确的值。但问题是当我从命令行提供一些值时(例如-p 58),相同的值20被传递给程序而不是我从命令行传递的58。
Can you please help me out by pointing the mistakes I am making here?
能否指出我在这里犯的错误,请你帮帮我吧?
Thank you.
谢谢。
4 个解决方案
#1
16
#/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 input output 55
C:\Temp> ks -iinput -ooutput input output 20
#2
17
The best thing is to use GetOpt::Long and use a hash instead of individual variables. Then you can pass default values by pre-populating the array
最好的方法是使用GetOpt :: Long并使用哈希而不是单个变量。然后,您可以通过预填充数组来传递默认值
use Getopt::Long;
my %opts = (parameter => 20);
GetOptions( \%opts,
'p|parameter=i',
'o|outputfile=s',
'i|inputfile=s'
) or die "Invalid parameters!";
# I didn't bother cloning STANDARD_HELP_VERSION = 1;
#3
1
I suggest setting the opt variables to defaults prior to calling getopts. Additionally, you can then use the $opt_ variables in your usage message to show the default values.
我建议在调用getopts之前将opt变量设置为默认值。此外,您可以在使用消息中使用$ opt_变量来显示默认值。
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
our $opt_p = 20;
sub HELP_MESSAGE { print " -p parameter value (default $opt_p)\n"; }
getopts('i:o:p:');
my $inputfile = our $opt_i;
my $outputfile = our $opt_o;
my $parameter_value = our $opt_p;
#4
0
#/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
my %opts = ();
getopts('i:o:p:', \%opts);
my $inputfile = $opts{i};
my $outputfile = $opts{o};
my $parameter_value = $opts{p} || "20";
print "$inputfile, $outputfile, $parameter_value\n";
#1
16
#/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 input output 55
C:\Temp> ks -iinput -ooutput input output 20
#2
17
The best thing is to use GetOpt::Long and use a hash instead of individual variables. Then you can pass default values by pre-populating the array
最好的方法是使用GetOpt :: Long并使用哈希而不是单个变量。然后,您可以通过预填充数组来传递默认值
use Getopt::Long;
my %opts = (parameter => 20);
GetOptions( \%opts,
'p|parameter=i',
'o|outputfile=s',
'i|inputfile=s'
) or die "Invalid parameters!";
# I didn't bother cloning STANDARD_HELP_VERSION = 1;
#3
1
I suggest setting the opt variables to defaults prior to calling getopts. Additionally, you can then use the $opt_ variables in your usage message to show the default values.
我建议在调用getopts之前将opt变量设置为默认值。此外,您可以在使用消息中使用$ opt_变量来显示默认值。
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
our $opt_p = 20;
sub HELP_MESSAGE { print " -p parameter value (default $opt_p)\n"; }
getopts('i:o:p:');
my $inputfile = our $opt_i;
my $outputfile = our $opt_o;
my $parameter_value = our $opt_p;
#4
0
#/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
my %opts = ();
getopts('i:o:p:', \%opts);
my $inputfile = $opts{i};
my $outputfile = $opts{o};
my $parameter_value = $opts{p} || "20";
print "$inputfile, $outputfile, $parameter_value\n";