aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/CliOption.php
blob: d0eace311918a7c9938d891c5454fc0c670d2d7e (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);

final class CliOption {
	public const VALUE_NONE = 'none';
	public const VALUE_REQUIRED = 'required';
	public const VALUE_OPTIONAL = 'optional';

	private string $longAlias;
	private ?string $shortAlias;
	private string $valueTaken = self::VALUE_REQUIRED;
	/** @var array{type:string,isArray:bool} $types */
	private array $types = ['type' => 'string', 'isArray' => false];
	private string $optionalValueDefault = '';
	private ?string $deprecatedAlias = null;

	public function __construct(string $longAlias, ?string $shortAlias = null) {
		$this->longAlias = $longAlias;
		$this->shortAlias = $shortAlias;
	}

	/** Sets this option to be treated as a flag. */
	public function withValueNone(): self {
		$this->valueTaken = static::VALUE_NONE;
		return $this;
	}

	/** Sets this option to always require a value when used. */
	public function withValueRequired(): self {
		$this->valueTaken = static::VALUE_REQUIRED;
		return $this;
	}

	/**
	 * Sets this option to accept both values and flag behavior.
	 * @param string $optionalValueDefault When this option is used as a flag it receives this value as input.
	 */
	public function withValueOptional(string $optionalValueDefault = ''): self {
		$this->valueTaken = static::VALUE_OPTIONAL;
		$this->optionalValueDefault = $optionalValueDefault;
		return $this;
	}

	public function typeOfString(): self {
		$this->types = ['type' => 'string', 'isArray' => false];
		return $this;
	}

	public function typeOfInt(): self {
		$this->types = ['type' => 'int', 'isArray' => false];
		return $this;
	}

	public function typeOfBool(): self {
		$this->types = ['type' => 'bool', 'isArray' => false];
		return $this;
	}

	public function typeOfArrayOfString(): self {
		$this->types = ['type' => 'string', 'isArray' => true];
		return $this;
	}

	public function deprecatedAs(string $deprecated): self {
		$this->deprecatedAlias = $deprecated;
		return $this;
	}

	public function getValueTaken(): string {
		return $this->valueTaken;
	}

	public function getOptionalValueDefault(): string {
		return $this->optionalValueDefault;
	}

	public function getDeprecatedAlias(): ?string {
		return $this->deprecatedAlias;
	}

	public function getLongAlias(): string {
		return $this->longAlias;
	}

	public function getShortAlias(): ?string {
		return $this->shortAlias;
	}

	/** @return array{type:string,isArray:bool} */
	public function getTypes(): array {
		return $this->types;
	}

	/** @return string[] */
	public function getAliases(): array {
		$aliases = [
			$this->longAlias,
			$this->shortAlias,
			$this->deprecatedAlias,
		];

		return array_filter($aliases);
	}
}