如何将 Getopt::Long 选项传递给也是一个选项的子例程?

发布于 2024-12-20 21:49:26 字数 864 浏览 1 评论 0原文

我正在尝试设置 Getopt::Long 来处理配置脚本中的参数。

这是我的开场白:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

my $config_file = '';

GetOptions (    
    'config|c=s' => \$config_file,
    'add|a' => \&add_server,
    'del|d' => \&del_server,        
);

sub add_server {    
    print "$config_file\n";    
}

sub del_server {    
    # Left blank for now.    
}

奇怪的是,当我使用如下命令运行脚本时,我遇到了问题:

./config.pl -a -c config.xml

它不会打印 < code>-c 选项,但如果我像这样运行它,

./config.pl -c config.xml -a

它会像它应该的那样工作。

我想我明白其中的原因;这与订单执行有关吗?

我该如何修复它?我应该将 Getopt::Long@ARGV 结合使用吗?

最终,我试图将命令行参数传递到我正在调用的子例程中。因此,如果 -a--add,我希望传递 -c--config 的选项调用时进入子程序。

有什么想法吗?

I am trying to setup Getopt::Long to handle the arguments from a configuration script.

Here is my starter:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

my $config_file = '';

GetOptions (    
    'config|c=s' => \$config_file,
    'add|a' => \&add_server,
    'del|d' => \&del_server,        
);

sub add_server {    
    print "$config_file\n";    
}

sub del_server {    
    # Left blank for now.    
}

The odd thing is I am running into a problem when I run my script with something like this:

./config.pl -a -c config.xml

It does NOT print the -c option, but if I run it like this,

./config.pl -c config.xml -a

it works like it should.

I think I understand the reason why; it has to do with the order execution right?

How can I fix it? Should I use Getopt::Long in conjunction with @ARGV?

Ultimately, I am trying to make the command line args pass into the subroutine that I am calling. So if -a or --add, I want the options of -c or --config to pass into the subroutine when it is called.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

为你拒绝所有暧昧 2024-12-27 21:49:27

启用 Getopt::Long 上的 pass_through 选项,以便它将忽略未知选项,然后为您的选项调用一次 GetOptions,再次禁用它,然后再次为您的命令使用 GetOptions。

Enable the pass_through option on Getopt::Long so that it will ignore unknown options, then call GetOptions once for your options, disable it again, and then use GetOptions again for your command.

木緿 2024-12-27 21:49:27
GetOptions(
        'arg=s' => sub { print "$_[1]\n"; },
);
GetOptions(
        'arg=s' => sub { print "$_[1]\n"; },
);
月亮坠入山谷 2024-12-27 21:49:26

我认为没有必要直接从 GetOptions 调用中调用子例程。像这样控制顺序:

use strict;
use warnings;
use Getopt::Long;

my %opts = (config => '');

GetOptions(\%opts, qw(
   config|c=s
   add|a
   del|d
));

add_server() if $opts{add};
del_server() if $opts{del};

sub add_server {    
    print "$opts{config}\n";
}

sub del_server {}

I don't see the need to call the subroutine directly from the GetOptions call. Control the order like this:

use strict;
use warnings;
use Getopt::Long;

my %opts = (config => '');

GetOptions(\%opts, qw(
   config|c=s
   add|a
   del|d
));

add_server() if $opts{add};
del_server() if $opts{del};

sub add_server {    
    print "$opts{config}\n";
}

sub del_server {}
做个少女永远怀春 2024-12-27 21:49:26

将示例稍微简化一下......

use strict;
use warnings;
use Getopt::Long;

my $config_file = '';

GetOptions (

    'config|c=s' => \$config_file,
    'add|a' => sub{add_server($config_file);}
);

sub add_server
{

    my $config=shift;

    if(defined($config))
    {
        print "Got this for a config file: $config\n";
    }
    else
    {
        print "No argument supplied to add_server\n";
    }

}

并运行config.pl -c Blug -a返回输出Got this for a config file:blurg,并且运行 config.pl -a -c blurg 会返回 Got this for a config file:

所以,我怀疑正在发生的事情是选项是按照给定的顺序分配的。因此,在第一种情况下,$config_file 被分配给 -c 参数,然后调用 add_server 子例程(使用正确的参数),而在第二种情况下,add_server 立即被触发,不带任何参数,然后分配 $config_file

除此之外,我建议将 -a 设置为布尔值,并在启用它的情况下执行您想做的任何操作(并且如果提供了 -c 的参数)。

Boiling the example down a little bit...

use strict;
use warnings;
use Getopt::Long;

my $config_file = '';

GetOptions (

    'config|c=s' => \$config_file,
    'add|a' => sub{add_server($config_file);}
);

sub add_server
{

    my $config=shift;

    if(defined($config))
    {
        print "Got this for a config file: $config\n";
    }
    else
    {
        print "No argument supplied to add_server\n";
    }

}

... and running config.pl -c blurg -a returns the output Got this for a config file: blurg, and running config.pl -a -c blurg returns Got this for a config file:.

So, what I suspect is happening is that the options are assigned in the order given. So in the first case $config_file is assigned to the -c argument and then the add_server subroutine is called (with the correct argument), whereas in the second case, add_server is immediately fired off with no argument and then $config_file is assigned.

All this aside, I'd recommend making -a a boolean and do whatever you want to do if it's enabled (and if an argument for -c is supplied).

牵你手 2024-12-27 21:49:26

遇到选项时会调用回调,因此当您执行操作时,会在遇到 -c 之前调用 add_server

./config.pl -a -c config.xml

根据最新信息,您现在需要:

use Getopt::Long qw( GetOptions );

GetOptions(
   'a=s' => \my $opt_a,
   'd=s' => \my $opt_d,
   'h=s' => \my $opt_h,
   'p=s' => \my $opt_p,
) or usage();

The callbacks are called as the options are encountered, so add_server is being called before -c has been encountered when you do

./config.pl -a -c config.xml

Based on the latest info, you now want:

use Getopt::Long qw( GetOptions );

GetOptions(
   'a=s' => \my $opt_a,
   'd=s' => \my $opt_d,
   'h=s' => \my $opt_h,
   'p=s' => \my $opt_p,
) or usage();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文