-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetOptions.php
More file actions
47 lines (42 loc) · 1.09 KB
/
GetOptions.php
File metadata and controls
47 lines (42 loc) · 1.09 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
<?php
declare(strict_types=1);
namespace Smeghead\PhpVariableHardUsage\Option;
final class GetOptions
{
/**
* @param array<string> $argv
*/
public function __construct(private readonly array $argv)
{
}
public function parse(): GetOptionsResult
{
$options = [];
$args = [];
$count = count($this->argv);
for ($i = 0; $i < $count; $i++) {
$arg = $this->argv[$i];
if (strpos($arg, '--') === 0) {
$key = substr($arg, 2);
$value = true;
if (strpos($key, '=') !== false) {
[$key, $value] = explode('=', $key, 2);
}
$options[$key] = $value;
} else {
$args[] = $arg;
}
}
return new GetOptionsResult($options, array_slice($args, 1));
}
}
final class GetOptionsResult {
/**
* @param array<string, string|bool> $options
* @param array<string> $paths
*/
public function __construct(
public array $options,
public array $paths,
) {}
}