-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
74 lines (66 loc) · 1.83 KB
/
Input.php
File metadata and controls
74 lines (66 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Effectra\Core\Console;
/**
* Class Input
*
* A utility class for capturing and processing command line inputs, flags, and options.
*/
class Input
{
/**
* @var array An array containing the command line arguments.
*/
private array $arguments;
/**
* Input constructor.
*
* Initializes the Input class by capturing and storing command line arguments.
*/
public function __construct()
{
$this->arguments = array_slice($_SERVER['argv'], 1);
}
/**
* Get the full command as a string.
*
* @return string The full command including arguments, flags, and options.
*/
public function getFullCommand(): string
{
return implode(' ', $this->arguments);
}
/**
* Get the array of command line arguments.
*
* @return array The array of command line arguments.
*/
public function getArguments(): array
{
return $this->arguments;
}
/**
* Check if a specific flag exists in the command line inputs.
*
* @param string $flag The flag to check for.
* @return bool True if the flag exists, false otherwise.
*/
public function hasFlag(string $flag): bool
{
return in_array($flag, $this->arguments);
}
/**
* Get the value of a specific option from the command line inputs.
*
* @param string $option The option to retrieve the value for.
* @return string|null The value of the option if found, or null if not found.
*/
public function getOption(string $option): ?string
{
$optionIndex = array_search($option, $this->arguments);
if ($optionIndex !== false && isset($this->arguments[$optionIndex + 1])) {
return $this->arguments[$optionIndex + 1];
}
return null;
}
}