Skip to content

Commit

Permalink
Optimize: input option
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlond committed Feb 22, 2023
1 parent 5cd5b58 commit 6c7e30c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 46 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ $cli->input('Hello world: ',array('Hello','world'));

$cli->input('Test2: ', 'test');


// This input requires any input

$cli->input('Free input: ', '');

// Question with default N
$answer = $cli->input('Do this? y/N', array('y','n','Y','N'), 'N');
echo $answer;
Expand Down
1 change: 1 addition & 0 deletions example/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$cli->error($string);
$cli->input('Hello world: ',array('Hello','world'));
$cli->input('Test2: ', 'test');
$cli->input('Free input: ', '');

$answer = $cli->input('Do this? y/N', array('y','n','Y','N'), 'N');
echo $answer;
102 changes: 56 additions & 46 deletions src/jblond/cli/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,63 @@
*/
class Cli {

/**
* @param string $prompt
* @param array|string $validInputs
* @param string $default
* @return string
*/
public function input(string $prompt, $validInputs, string $default = ''): string
/**
* @param string $prompt
* @param array|string $validInputs
* @param string $default
* @return string
*/
public function input(string $prompt, $validInputs, string $default = ''): string
{
echo $prompt;
$input = trim(fgets(fopen('php://stdin', 'rb')));
while(
!isset($input) ||
(
is_array($validInputs) &&
!in_array($input, $validInputs, true)
) ||
(
$validInputs === 'is_file' &&
!is_file($input)
)
){
echo $prompt;
$input = trim(fgets(fopen('php://stdin', 'rb')));
if(empty($input) && !empty($default)) {
$input = $default;
}
}
return $input;
}

/**
* @param string $output
*/
public function output(string $output): void
echo $prompt;

$input = trim(fgets(fopen('php://stdin', 'rb')));

while (true) {
if ($input === '' && $default !== '') {
return $default;
}

if ($validInputs === 'is_file' && !is_file($input)) {
echo $prompt;
$input = trim(fgets(STDIN));
continue;
}

if (is_array($validInputs) && !in_array($input, $validInputs, true)) {
echo $prompt;
$input = trim(fgets(STDIN));
continue;
}

if (is_string($validInputs) && !empty($validInputs) && ($input !== $validInputs)) {
echo $prompt;
$input = trim(fgets(STDIN));
continue;
}

return $input;
}
}


/**
* @param string $output
*/
public function output(string $output): void
{
$out = fopen('php://output', 'wb');
fwrite($out, $output);
fclose($out);
}

/**
* @param string $string
*/
public function error(string $string): void
$out = fopen('php://output', 'wb');
fwrite($out, $output);
fclose($out);
}

/**
* @param string $string
*/
public function error(string $string): void
{
$stdError = fopen('php://stderr', 'wb');
fwrite($stdError, $string);
fclose($stdError);
}
$stdError = fopen('php://stderr', 'wb');
fwrite($stdError, $string);
fclose($stdError);
}
}

0 comments on commit 6c7e30c

Please sign in to comment.