lithium\console\Command::in()

public method

Handles input. Will continue to loop until $options['quit'] or result is part of $options['choices'].

Parameters

  • string $prompt
  • array $options

Returns

string|boolean

Returns the result of the input data. If the input is equal to the quit option boolean false is returned.

Source

	public function in($prompt = null, array $options = []) {
		$defaults = ['choices' => null, 'default' => null, 'quit' => 'q'];
		$options += $defaults;
		$choices = null;

		if (is_array($options['choices'])) {
			$choices = '(' . implode('/', $options['choices']) . ')';
		}
		$default = $options['default'] ? "[{$options['default']}] " : '';

		do {
			$this->out("{$prompt} {$choices} \n {$default}> ", 0);
			$result = trim($this->request->input());
		} while (
			!empty($options['choices']) &&
			!in_array($result, $options['choices'], true) &&
			(empty($options['quit']) || $result !== $options['quit']) &&
			(!$options['default'] || $result !== '')
		);

		if ($result == $options['quit']) {
			return false;
		}

		if ($options['default'] !== null && $result == '') {
			return $options['default'];
		}
		return $result;
	}