我正在使用的Minion作业队列中的一项任务需要用户和密码。
我找到了有关如何将参数传递给Mojo应用here的很好的描述,所以我像这样进行操作:
package Minion::Command::minion::secure_worker;
use Mojo::Base 'Minion::Command::minion::worker';
use Mojo::Util 'getopt';
sub run {
my ( $self, @args ) = @_;
my $worker = $self->app->minion->worker;
my $status = $worker->status;
getopt \@args,
'U|username=s' => \my $username,
'P|password=s' => \my $password;
$self->app->credentials->{username} = $username;
$self->app->credentials->{password} = $password;
return $self->SUPER::run;
}
1
但是,当我尝试传递原始工作程序命令中的选项时,例如-j
,我得到:
Unknown option: j
那是为什么?子类化命令似乎无效,或者getopt吞噬了所有内容?
答案 0 :(得分:1)
对getopt
的调用是不可组合的。您可以尝试处理(然后传递)超类也处理的所有选项,或者使用pass_through
选项自己调用Getopt::Long。这将忽略所有未知选项:
package Minion::Command::minion::secure_worker;
use Mojo::Base 'Minion::Command::minion::worker';
use Getopt::Long 'GetOptionsFromArray';
use Getopt::Long ':config', 'pass_through';
sub run {
my ( $self, @args ) = @_;
my $worker = $self->app->minion->worker;
my $status = $worker->status;
GetOptionsFromArray \@args,
'U|username=s' => \my $username,
'P|password=s' => \my $password;
$self->app->credentials->{username} = $username;
$self->app->credentials->{password} = $password;
return $self->SUPER::run;
}
1