Example #1
0
 /**
  * Load the configuration from a file
  * @ignore
  * @param	string	$configfile	The configuration file
  * @return	Args				$this
  */
 private function loadFromConfigFile($configfile)
 {
     global $argv;
     if (is_null($configfile)) {
         $configfile = dirname($argv[0]) . DIRECTORY_SEPARATOR . pathinfo($argv[0], PATHINFO_FILENAME) . ".args.json";
     }
     if (!is_file($configfile)) {
         return;
     }
     $config = json_decode(file_get_contents($configfile), true);
     if (json_last_error() != JSON_ERROR_NONE) {
         Helper::throwEx("JSON parse error from {$configfile}: " . json_last_error_msg());
     }
     $this->loadFromArray($config);
     return $this;
 }
Example #2
0
 /**
  * Set the alias of the command
  * @api
  * @param	string	$aka	The alias of the command
  * @return	Command			$this
  */
 public function aka($aka)
 {
     $check = $this->args->_checkCmdName($aka);
     if (is_string($check)) {
         Helper::throwEx($check);
     }
     $this->alias[] = $aka;
     $names = $this->name . (empty($this->alias) ? "" : "|" . implode("|", $this->alias));
     $this->args->_adjustLen(0, strlen($names) - 2);
     return $this;
 }
Example #3
0
 /**
  * The callback for the option.
  * The value can be changed.
  * Value can also be validated/checked in this callback.
  * @api
  * @alias must, follow, callback
  * @param	callable	$rule	The callback
  * @example
  *  ```php
  *		function ($option) {
  *			$option->setValue (intval('1')); // change the value to int type
  *		}
  *		function ($option) {
  *			$value = $option->getValue();
  *			// array option
  *			// will print the error, e.g.: Need at least 2 values for option xx.
  *			if (sizeof($value) < 2) return false; 
  *			return true;
  *		}
  *  ```
  * @param	string		$error	The error to show if the callback returns false
  * @return	Option				$this
  */
 public function obey($rule, $error = "{option} is not in right format.")
 {
     if (!is_callable($rule)) {
         Helper::throwEx('Callback must be callable.');
     }
     $option = $this->matched == "-{$this->name}" ? "" : "(-{$this->name})";
     $option = $this->matched ? $this->matched . $option : "-{$this->name}";
     $this->ruleError = str_replace("{option}", $option, $error);
     $this->rule = $rule;
     return $this;
 }