kahlan\cli\CommandLine::parse PHP Method

parse() public method

Parses a command line argv.
public parse ( array $argv, boolean $override = true ) : array
$argv array An argv data.
$override boolean If set to `false` it doesn't override already setted data.
return array The parsed attributes
    public function parse($argv, $override = true)
    {
        $exists = [];
        $override ? $this->_values = $this->_defaults : ($exists = array_fill_keys(array_keys($this->_values), true));
        foreach ($argv as $arg) {
            if ($arg === '--') {
                break;
            }
            if ($arg[0] === '-') {
                list($name, $value) = $this->_parse(ltrim($arg, '-'));
                if ($override || !isset($exists[$name])) {
                    $this->add($name, $value, $override);
                }
            }
        }
        return $this->get();
    }

Usage Example

Example #1
0
 /**
  * Load the config.
  *
  * @param string $argv The command line string.
  */
 public function loadConfig($argv = [])
 {
     $commandLine = new CommandLine();
     $commandLine->option('config', ['default' => 'kahlan-config.php']);
     $commandLine->option('help', ['type' => 'boolean']);
     $commandLine->option('version', ['type' => 'boolean']);
     $commandLine->parse($argv);
     $run = function ($commandLine) {
         if (file_exists($commandLine->get('config'))) {
             require $commandLine->get('config');
         }
     };
     $run($commandLine);
     $this->_commandLine->parse($argv, false);
     if ($commandLine->get('help')) {
         return $this->_help();
     }
     if ($commandLine->get('version')) {
         return $this->_version();
     }
 }
All Usage Examples Of kahlan\cli\CommandLine::parse