4

One of the tasks in a Minion job queue I am using needs user and password.

I found a good description on how to pass parameters to a Mojo app here so I went about it like this:

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

However when I try to pass options that were in the original worker command - such as -j I get:

Unknown option: j

Why is that? It looks like subclassing a command doesn't work, or that getopt slurps everything?

Grinnz
  • 9,093
  • 11
  • 18
simone
  • 4,667
  • 4
  • 25
  • 47

1 Answers1

1

The calls to getopt are not composable. You can try to handle (and then pass on) all options that your superclass handles as well, or call Getopt::Long yourself with the pass_through option. This will ignore all unknown options:

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
Corion
  • 3,855
  • 1
  • 17
  • 27