免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2471 | 回复: 6
打印 上一主题 下一主题

Getopt::Long问题求教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-31 13:24 |只看该作者 |倒序浏览
我现在想通过Getopt::Long实现读入命令行输入,其中有两个参数,一个叫SearchIndex,一个叫Keyword,但是Keyword可能有多个单词组成,比如说Sony Cybershot T20,所以我想用数组:

$searchIndex;
@Keyword;
GetOptions ('SearchIndex=s' => \$searchIndex, 'Keyword=s@{1,}' => \@Keyword);

在windows下运行成功,但是在unix下运行的时候报错:Error in option spec: 'Keyword=s@{1,}'.
我想知道为什么这样不行,求达人指教,谢谢了!

论坛徽章:
0
2 [报告]
发表于 2008-01-31 14:45 |只看该作者
Options with multiple values

       Options sometimes take several values. For example, a program could use
       multiple directories to search for library files:

           --library lib/stdlib --library lib/extlib

       To accomplish this behaviour, simply specify an array reference as the
       destination for the option:

           GetOptions ("library=s" => \@libfiles);

       Alternatively, you can specify that the option can have multiple values
       by adding a "@", and pass a scalar reference as the destination:

           GetOptions ("library=s@" => \$libfiles);

论坛徽章:
0
3 [报告]
发表于 2008-01-31 14:48 |只看该作者

  1. use strict;
  2. use warnings;
  3. use Getopt::Long;
  4. my $search;
  5. my @keyword;
  6. GetOptions("search=s" => \$search,
  7.            "keyword=s" =>\@keyword,);
  8. print "\$search -->$search\n";
  9. print "\@keyword --->@keyword\n";
复制代码

运行结果
<lig@other-server:~/chinaunix>$ ./keyword.pl --search a --keyword a --keyword b $search -->a
@keyword --->a b

论坛徽章:
0
4 [报告]
发表于 2008-01-31 17:16 |只看该作者

回复 #3 churchmice 的帖子

谢谢,但是如果我想输入的命令行是keyword.pl --search a --keyword a  b
然后让keyword返回a b,应该怎么写呢?
为什么我那程序在windows下可以执行,在unix下就报错呢?

论坛徽章:
0
5 [报告]
发表于 2008-01-31 18:00 |只看该作者
原帖由 stale 于 2008-1-31 17:16 发表
谢谢,但是如果我想输入的命令行是keyword.pl --search a --keyword a  b
然后让keyword返回a b,应该怎么写呢?
为什么我那程序在windows下可以执行,在unix下就报错呢?

因为windows和unix是不同的系统
activeperl的模块和unix里面的perl模块实现是不一样的

论坛徽章:
0
6 [报告]
发表于 2008-01-31 19:22 |只看该作者
如果你的命令行参数里面只有--option argument1 argument2 这样的选项
那么你直接对@ARGV进行处理好了
没有必要使用Getopt::Long模块了

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $key;
  5. my %hash;
  6. for(@ARGV){
  7. if (/^--(.+)/){
  8. $key = $1;
  9. next;
  10. }
  11. $hash{$key}.= "$_ ";
  12. }
  13. my $search = $hash{"search"};
  14. my @keyword = split /\s+/,$hash{"keyword"};
  15. print "\$search = $search\n";
  16. print "\@keyword = @keyword\n";
复制代码


运行结果
<lig@other-server:~/chinaunix>$ ./modify  --search a --keyword a b sfs
$search = a
@keyword = a b sfs

<lig@other-server:~/chinaunix>$ ./modify  --search a --keyword a b sf
$search = a
@keyword = a b sf

[ 本帖最后由 churchmice 于 2008-1-31 19:24 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-02-01 04:05 |只看该作者
谢谢churchmice了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP