arguments = $argv; } /** * @param array $shortOpt * @param array $longOpt * @return false|false[]|string[] */ public function option(array $shortOpt = [], array $longOpt = []) { $shortOpt = implode('', $shortOpt); return getopt($shortOpt, $longOpt); } public function arguments(): array { return $this->arguments; } public function line(): string { return trim(fgets(STDIN)); } /** * @return string * @todo Implement multiline input from pipe ('|') */ public function multiLine(): string { $input = ''; // While an input is coming, keep going until a break is passed while ($line = $this->line()) { // If the line does not end with '\', save the input and break the multiline if (substr(rtrim($line), -2, 2) != ' \\') { $input .= $line; break; } // If the line ends with '\', save the line and ask for the next one; else { $input .= substr(rtrim($line), 0 , -1); echo "\r" . ' > '; } } // And finally output the input return $input; } /** * Asks the user a question to which the user responds. Input will be of type multiline * * @param string $question * @param string $response_type * @param bool $optional * @return string */ public function dialog(string $question, string $response_type, bool $optional = false): string { // First ask the dialog question echo $question . " > "; // Retrieve the input using a multiline $input = $this->multiLine(); // If the input is empty and not optional, ask again if (empty($input) && !$optional) { echo "Input may is not optional!\n"; $input = $this->dialog($question, $response_type, $optional); } // Verify whether the input matches the requested type // @todo return $input; } /** * Can be used for things like pipe. Might need some rework though... * @todo Rework, implement safe_feof() * @see https://www.php.net/manual/en/function.feof.php * * @return string */ public function stdin(): string { $lines = $this->stdinArray(); return implode("\n", $lines); } public function stdinArray(): array { $lines = []; while(!feof(STDIN)) $lines[] = fgets(STDIN); return $lines; } }