JN-Jones / web-artisan

A package for Laravel 4 top interact with the CLI
54 stars 11 forks source link

Custom command: arguments not retreived properly #5

Closed nbertal closed 11 years ago

nbertal commented 11 years ago

Me again, When making a custom command that uses arguments, they are not taken properly into considerations:

In my command "example" I set

protected function getArguments()
    {
        return array(
            array('userid', InputArgument::REQUIRED, 'The id of the user in the DB.'),
            array('role', InputArgument::REQUIRED, 'The role of the user : buy, sell, none.'),
        );
    }

Then when launching in regular artisan I can do: => artisan command:example 12 sell it works fine

but in web-artisan it looks like there is a confusion between arguments and options, launching the same command raises: Error (InvalidArgumentException): The "12" argument does not exist. in ....\vendor\symfony\console\Symfony\Component\Console\Input\ArrayInput.php (204)

If instead I type the name of the argument, it raises no error, but obsviously I cannot proceed since values need to be passed.

JN-Jones commented 11 years ago

I think it's the same as #3 so you need to type artisan command:example userid=12 role=sell. I have to look into it whether I can fix this or not, as it's the way Artisan:call works

nbertal commented 11 years ago

Indeed, it looks like a Laravel issue / limitation on Artisan::call(). http://forums.laravel.io/viewtopic.php?id=8307 Not sure whether an issue is opened there, but meanwhile I am going to use the command you recommended.

A possible workaround might be to assign the keys to each values based on what's in getArguments (first making sure it's not option) ? [Cmd.php foreach loop line 65]

JN-Jones commented 11 years ago

Yeah that would be the workaround I'll use but I need to implement it first

nbertal commented 11 years ago

Thanks !

JN-Jones commented 11 years ago

Ok I have it nor to a point where I have an instance of the command itself:

$app = app();
$app->loadDeferredProviders();
$artisan = ConsoleApplication::start($app);
$command = $artisan->find($cmd);

But the problem is now that I need to have the arguments/options to check the input against it, but the functions getArguments and getOptions are protected. Any thoughts on this?

JN-Jones commented 11 years ago

Ok found a (dirty) way:

$def = $command->getDefinition();
$arguments = $def->getArguments();
$fix = array();
foreach($arguments as $argument) {
    $fix[] = $argument->getName();
}
$arguments = $fix;

$options = $def->getOptions();
$fix = array();
foreach($options as $option) {
    $fix[] = $option->getName();
}
$options = $fix;

Now working on the workaround itself...

JN-Jones commented 11 years ago

Ok fixed it, the way the new function works isn't the best but I hadn't any other thoughts on this. If someone finds a better way, feel free to make a Pull Request