加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 如何解析命令行参数?

发布时间:2020-12-16 06:11:29 所属栏目:大数据 来源:网络整理
导读:我想解析perl脚本中的参数列表,例如我遇到这种情况: script.pl -h 127.0.0.1 -u user -p pass arg1 arg2 arg3 我该如何解析数组中不是选项的参数列表,以及标量值中的选项参数? 谢谢. 解决方法 好吧,如果它们是命令行中唯一没有作为选项提供的东西,那么它们
我想解析perl脚本中的参数列表,例如我遇到这种情况:

script.pl -h 127.0.0.1 -u user -p pass arg1 arg2 arg3

我该如何解析数组中不是选项的参数列表,以及标量值中的选项参数?

谢谢.

解决方法

好吧,如果它们是命令行中唯一没有作为选项提供的东西,那么它们仍然应该在@ARGV中.所以只需使用@ARGV.

use Getopt::Long;

# save arguments following -h or --host in the scalar $host
# the '=s' means that an argument follows the option
# they can follow by a space or '=' ( --host=127.0.0.1 )
GetOptions( 'host=s' => my $host,'user=s' => my $user  # same for --user or -u,'pass=s' => my $pass  # same for --pass or -p
          );

# @ARGV: [ qw<arg1 arg2 arg3> ]

Getopt::Long

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读