Perl Getopt :: Long相关问题 - 互斥命令行参数 [英] Perl Getopt::Long Related Question - Mutually Exclusive Command-Line Arguments
本文介绍了Perl Getopt :: Long相关问题 - 互斥命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的perl脚本中有以下代码:
I have the following code in my perl script:
my $directory;
my @files;
my $help;
my $man;
my $verbose;
undef $directory;
undef @files;
undef $help;
undef $man;
undef $verbose;
GetOptions(
"dir=s" => \$directory, # optional variable with default value (false)
"files=s" => \@files, # optional variable that allows comma-separated
# list of file names as well as multiple
# occurrenceces of this option.
"help|?" => \$help, # optional variable with default value (false)
"man" => \$man, # optional variable with default value (false)
"verbose" => \$verbose # optional variable with default value (false)
);
if (@files) {
@files = split(/,/,join(',', @files));
}
处理互斥命令行参数的最好方法是什么?在我的脚本中,我只希望用户只输入--dir或--files命令行参数,但不能同时输入。是否还有配置Getopt这样做?
What is the best way to handle mutually exclusive command line arguments? In my script I only want the user to enter only the "--dir" or "--files" command line argument but not both. Is there anyway to configure Getopt to do this?
谢谢。
推荐答案
p>我不认为有一种方法在Getopt :: Long做到这一点,但它是很容易实现自己的(我假设有一个使用函数,返回一个字符串告诉用户如何调用程序):
I don't think there is a way in Getopt::Long to do that, but it is easy enough to implement on your own (I am assuming there is a usage function that returns a string that tells the user how to call the program):
die usage() if defined $directory and @files;
这篇关于Perl Getopt :: Long相关问题 - 互斥命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文