Example #1
0
 /**
  * After the parsing, finish the value assignment
  * @api
  * @param	string|array	$_argv The arguments. If it is not offered, will use $argv.
  * @return	Args			$this
  */
 public function done($_argv = null)
 {
     $this->done = true;
     global $argv;
     $args = $argv;
     if (!is_null($_argv)) {
         $args = is_string($_argv) ? Helper::getArgv($_argv) : $_argv;
     }
     $this->addHelpOpts();
     $this->addHelpCmdsIfNeeded();
     $progname = $this->parse($args);
     // adjust desc len
     // no command mode
     $help = $this->getHelpInfoWithoutCommand($progname);
     if (!empty($this->commands)) {
         $help .= $this->getHelpCmdList();
     }
     $help .= PHP_EOL;
     $helpopt = $this->getHelpOpt();
     if (!is_null($helpopt) and $helpopt->getValue() === true or $this->baldHelp and sizeof($args) == 1 or in_array($this->command, $this->helpCmds)) {
         fwrite(STDERR, $help);
         exit;
     }
     foreach ($this->options as $name => $option) {
         if ($option->getRequired() and is_null($option->getValue())) {
             $this->_addError("Option -{$name} is required.");
         }
         if (is_null($option->getValue())) {
             $option->setValue($option->getDefault());
         }
         $option->dumpValue($this->values);
     }
     // just show one error
     // multiple errors will be confused.
     foreach ($this->errors as $error) {
         fwrite(STDERR, $error);
         fwrite(STDERR, $help);
         exit;
     }
     if ($this->showWarns) {
         foreach ($this->warnings as $warning) {
             fwrite(STDERR, $warning);
         }
     }
     // dump command values
     if ($this->command != '') {
         $curcmd = $this->getCommand($this->command);
         $cmdvals = $curcmd->get();
         foreach ($cmdvals as $key => $value) {
             $this->values["{$curcmd->getName()}.{$key}"] = $value;
             foreach ($curcmd->getAlias() as $alias) {
                 $this->values["{$alias}.{$key}"] = $value;
             }
         }
     }
     return $this;
 }