kahlan\cli\CommandLine::exists PHP Method

exists() public method

Checks if an option has been setted.
public exists ( string $name ) : boolean
$name string The name of the option.
return boolean
    public function exists($name)
    {
        list($key, $extra) = $this->_splitOptionName($name);
        if (isset($this->_values[$key]) && is_array($this->_values[$key]) && array_key_exists($extra, $this->_values[$key])) {
            return true;
        }
        if (isset($this->_options[$name])) {
            return isset($this->_options[$name]['default']);
        }
        return false;
    }

Usage Example

Example #1
0
 });
 describe("->exists()", function () {
     it("returns `true` if the option exists", function () {
         $commandLine = new CommandLine();
         $actual = $commandLine->parse(['command', '--option1', '--option2=true', '--option3=false', '--option4=0']);
         expect($commandLine->exists('option1'))->toBe(true);
         expect($commandLine->exists('option2'))->toBe(true);
         expect($commandLine->exists('option3'))->toBe(true);
         expect($commandLine->exists('option4'))->toBe(true);
         expect($commandLine->exists('option5'))->toBe(false);
     });
     it("returns `true` if the option as a default value", function () {
         $commandLine = new CommandLine();
         $commandLine->option('option1', ['type' => 'boolean']);
         $commandLine->option('option2', ['type' => 'boolean', 'default' => false]);
         expect($commandLine->exists('option1'))->toBe(false);
         expect($commandLine->exists('option2'))->toBe(true);
     });
 });
 describe("->cast()", function () {
     it("casts array", function () {
         $commandLine = new CommandLine();
         $cast = $commandLine->cast(["some", "string", "and", 10], "string");
         expect($cast)->toBeAn('array');
         foreach ($cast as $c) {
             expect($c)->toBeA('string');
         }
     });
     it("casts boolean", function () {
         $commandLine = new CommandLine();
         $cast = $commandLine->cast(["true", "false", "some_string", null, 10], "boolean");