Skip to content

Bug: CLI options not present in $params when run through spark via terminal #3400

@paulbalandan

Description

@paulbalandan

Describe the bug
I don't know if this is a bug or intentional design choice. if you will be accessing the $params array via direct to spark, you will only get the segments. For example:

<?php

namespace CodeIgniter\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class TestCmd extends BaseCommand
{
    protected $group        = "Test";
    protected $name         = "test";
    protected $usage        = "test param1 param2 -option1 value1 ";

    public function run(array $params)
    {
        CLI::write(print_r($params, true));
    }
}

If I will run php spark test as df -gh jk in the terminal, I am expecting result to be

Array
(
    [0] => as
    [1] => df
    [gh] => jk
)

but got

Array
(
    [0] => as
    [1] => df
)

This is important if we will be making commands and a developer relies only on $params to check for options. If he will write $option = $params['gh'] ?? null, overlooking the more comprehensive $options = $params['gh'] ?? CLI::getOption('gh'), he will always get null even if passing the gh option.

This is not a problem if we are making programmatic calls, like using $this->call() inside other commands as we can explicitly include the options in the $params.

CodeIgniter 4 version
develop

Affected module(s)
CodeIgniter::runController()

Context

  • OS: Win10
  • Web server Apache 2.4.41
  • PHP version 7.4.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions